GitHub    Download    Forum
Overview
Tutorial
Database setup
Preparing the project
Establish the connection
Shut down the connection
DTDL types
Creating the schema
DADL classes
Entry point object
Write and read attributes
Write and read lists
Extending the schema
Modified entry point object
Write and read objects
Write and read object lists
C++ API
C# API
DTDL
DADL
Setup
C++
C#

Write and read objects

When working with objects, we will always go through a few crucial steps detailed here.

Create, link and store

First, we use the DADL-generated "Create" function. While the second parameter we pass is called "parent", it is not a parent in a classic sense. Instead, it is a hint for the storage to let it know which objects will likely be loaded or stored at the same time (objects with the same parent form a group, not the objects with their parents). This is then used to optimize disk access. We can enhance performance significantly that way.
WResponsible* pResponsible;
WResponsible::Create(&pResponsible, pInventory);
WResponsible pResponsible;
WResponsible.Create(out pResponsible, pInventory);
After that, we create the link from pInventory to pResponsible, which also preserves the object.
In a real-world application, the person responsible would probably be an object that we link to with a lazy link, because its lifetime will be controlled differently.
pInventory->LinkResponsible(pResponsible);
pInventory.LinkResponsible(pResponsible);
Now, you can set your data.
pResponsible->SetFullName(L"a name");
pResponsible->SetComment(L"an interesting comment");
pResponsible.SetFullName("a name");
pResponsible.SetComment("an interesting comment");
Next, we have to tell both pResponsible and pInventory to store the changes we made to them. The store function queues changes into the transaction, waiting for it to be executed.
pResponsible->Store();
pInventory->Store();
pResponsible.Store(Transaction.Store);
pInventory.Store(Transaction.Store);
Execute then changes the database.
pInventory->GetDomain()->Execute(Transaction::Store);
pInventory.GetDomain().Execute(Transaction.Store);
And finally, of course, we can release pResponsible, because we don't need that object anymore.
pResponsible->Release();

Open and load

Existing objects just have to be opened and loaded. To do that, we can use the DADL-generated "Open" function.
WResponsible* pResponsible;
pInventory->OpenResponsible(&pResponsible);

pResponsible->Load();

pResponsible->GetDomain()->Execute(Transaction::Load);
WResponsible pResponsible;
pInventory.OpenResponsible(out pResponsible, Transaction.Load);

pResponsible.Load(Transaction.Load);

pResponsible.GetDomain().Execute(Transaction.Load);
Now, we can read your data (or perform any other operation).
const wchar_t* strFullName;
pResponsible->GetFullName(&strFullName);

const wchar_t* strComment;
pResponsible->GetComment(&strComment);
string strFullName;
pResponsible.GetFullName(out strFullName);

string strComment;
pResponsible.GetFullName(out strComment);
If we don't need the object anymore, we finally release it.
pResponsible->Release();

Final code

Our files might ultimately look similar to these:
writeObject.cpp
writeObject.cs
#include "pch.h"

void writeObject(WInventory* pInventory)
{
	// create object
	WResponsible* pResponsible;
	WResponsible::Create(&pResponsible, pInventory);

	// FullName
	wprintf(L"Full name:\n");
	std::wstring input1;
	getline(std::wcin, input1);
	pResponsible->SetFullName(input1.c_str());

	// Comment
	wprintf(L"Comment:\n");
	std::wstring input2;
	getline(std::wcin, input2);
	pResponsible->SetComment(input2.c_str());

	// set objectlink
	pInventory->LinkManager(pResponsible);

	// Store
	pResponsible->Store();	// new object
	pInventory->Store();

	// Execute
	HRESULT hRes;
	if(FAILED(hRes = pInventory->GetDomain()->Execute(Transaction::Store)))
	{
		wprintf(L"Domain failed to execute the transaction (0x%x)", hRes);
	}

	pResponsible->Release();
}
using System;
using DataFoundationAccess;

namespace TutorialCs
{
	partial class Tutorial
	{
		public static void writeObject(WInventory pInventory)
		{
			// create object
			WResponsible pResponsible;
			WResponsible.Create(out pResponsible, pInventory);

			// FullName
			Console.WriteLine("Full name:");
			string input1 = Console.ReadLine();
			pResponsible.SetFullName(input1);

			// Comment
			Console.WriteLine("Comment:");
			string input2 = Console.ReadLine();
			pResponsible.SetComment(input2);

			// set objectlink
			pInventory.LinkManager(pResponsible);

			// Store
			pResponsible.Store(Transaction.Store);  // new object
			pInventory.Store(Transaction.Store);

			// Execute
			int hRes;
			if(0 > (hRes = pInventory.GetDomain().Execute(Transaction.Store)))
			{
				Console.WriteLine("Domain failed to execute the transaction (0x{0:x})", hRes);
			}

			pResponsible.Dispose();
		}
	}
}

readObject.cpp
readObject.cs
#include "pch.h"

void readObject(WInventory* pInventory)
{
	HRESULT hRes;

	// Open
	WResponsible* pResponsible;
	if(S_OK == (hRes = pInventory->OpenManager(&pResponsible)))
	{
		// Load
		pResponsible->Load();

		// Execute
		if(FAILED(hRes = pResponsible->GetDomain()->Execute(Transaction::Load)))
		{
			wprintf(L"Domain failed to execute the transaction (0x%x)\n", hRes);
		}
		else
		{
			// FullName
			const wchar_t* strFullName;
			pResponsible->GetFullName(&strFullName);
			wprintf(L"Responsible full name: %s\n", strFullName);

			// Comment
			const wchar_t* strComment;
			pResponsible->GetComment(&strComment);
			wprintf(L"Responsible comment: %s\n", strComment);
		}

		pResponsible->Release();
	}
	else
		wprintf(L"Cannot open responsible (0x%x)\n", hRes);
}
using System;
using DataFoundationAccess;

namespace TutorialCs
{
	partial class Tutorial
	{
		public static void readObject(WInventory pInventory)
		{
			int hRes;

			// Open
			WResponsible pResponsible;
			if (0 > (hRes = pInventory.OpenManager(out pResponsible, Transaction.Load)))
			{
				// Load
				pResponsible.Load(_WResponsible.ALL_ATTRIBUTES, Transaction.Load);

				// Execute
				if (0 > (hRes = pResponsible.GetDomain().Execute(Transaction.Load)))
				{
					Console.WriteLine("Domain failed to execute the transaction (0x{0:x})", hRes);
				}
				else
				{
					// FullName
					string strFullName;
					pResponsible.GetFullName(out strFullName);
					Console.WriteLine("Responsible full name: {0}", strFullName);

					// Comment
					string strComment;
					pResponsible.GetFullName(out strComment);
					Console.WriteLine("Responsible comment: {0}", strComment);
				}
			}
			else
				Console.WriteLine("Cannot open responsible (0x{0:x})", hRes);
		}
	}
}
© 2022 Mobiland AG