Sample code for DADO Solution®
Following page shows some sample code on how to query, update your business objects with DADO Solution®. Due to the larger use of VB.NET, the sample code will be in this language. C# samples will be available soon.
- Open a context to a defined configuration file
- Simple object query of "Address" objects
- Getting object meta information
- Advanced object query
- Simple object update
- Multiple object update
- Simple object delete
- Load the configuration from an embedded resource or a stream
- Set the connection string in code
- How to implement a lazy loaded property
- Open a context connected to a remote application server or web service
1. Open a context to a defined configuration file
This sample shows how to use an overloaded version of "Context.FromConfiguration". A version to load it from an embedded resource or a network location are also available. The configuration file can be the one created with DADO Database Mapper®, DADO Xml Mapper® or DADO Broker System®.
Imports DADO.PersistencyDim
MyContext As Context = Context.FromConfiguration("C:MyConfiguration.xml")
2. Simple object query of "Address" objects
This sample returns all objects of type "Address" or an inheriting type
Imports DADO.Persistency
Dim MyContext As Context = Context.FromConfiguration("C:MyConfiguration.xml")
Dim MyAddresses As Address() = MyContext.GetObjects(GetType(Address))
3. Getting object meta information
This sample show how to get object meta information, like Save status, Date/Time stamp, Change Sequence, Expiration flag, etc.. You can get obje
Imports DADO.Persistency
Dim MyContext As Context = Context.FromConfiguration("C:MyConfiguration.xml")
Dim MyAddress As Address = MyContext.GetObject(GetType(Address), 123) 'Retrieve address with ID=123 Dim MyEntityInfo As EntityInfo = MyContext.GetEntityInfo(MyAddress) 'Get meta information about MyAddress
Console.WriteLine("Status: " + MyEntityInfo.Status.ToString()) Console.WriteLine("Date/Time stamp: " + MyEntityInfo.DateTimeStamp.ToString()) Console.WriteLine("Change sequence: " + MyEntityInfo.ChangeSequence.ToString()) Console.WriteLine("Reference: " + MyEntityInfo.Reference.ToString()) Console.WriteLine("Expired: " + MyEntityInfo.Expired.ToString())
4. Advanced object query
This sample shows how to define complex criteria on properties and property of properties, limit the amount of returned objects, defined types to be refreshed from cache, etc..
Imports DADO.Persistency
Dim MyContext As Context = Context.FromConfiguration("C:MyConfiguration.xml")
Dim MyQueryExpression = new MyQueryExpression(GetType(Address)) MyQueryExpression.Criteria.Criterion.Add("Street",Operator.LIKE,"A*") 'Street must begin with A MyQueryExpression.Criteria.Criterion.Add("Number",Operator.BETWEEN,1,5) 'Number must be between 1 and 5 MyQueryExpression.RefreshTypes.Add(GetType(Country)) 'Country objects must be refreshed (not from cache) MyQueryExpression.AddPath("Country.Continent.Name", Operator.EQUAL, "Europe") 'Address must be of an european country MyQueryExpression.TopCount = 100 'Only the first 100 objects are returned
Dim MyAddresses As Address() = MyContext.GetObjects(MyQueryExpression) 'Retrieve query addresses
5. Simple object update
This sample shows how to retrieve a defined object, register it (getting a clone), update some properties and update changes
Imports DADO.Persistency
Dim MyContext As Context = Context.FromConfiguration("C:MyConfiguration.xml")
Dim MyAddress As Address = MyContext.GetObject(GetType(Address), 123) 'Retrieve address with ID=123 MyAddress = CType(MyContext.GetEditableObject(MyAddress),Address) MyAddress.Street = "New street" MyContext.Save(MyContext)
6. Multi object update
This sample shows how to retrieve some objects, register it, and update them in a single unit of work. Note that objects are only updated (in a single transaction) when the UnitOfWork is committed. Until they are not committed, they are only validated. Note that in the UnitOfWork you can also delete objects.
Imports DADO.Persistency
Dim MyContext As Context = Context.FromConfiguration("C:MyConfiguration.xml")
Dim MyQueryExpression = MyContext.NewQueryExpression(GetType(Address)) ...
Dim MyOldAddresses As Address() = MyContext.GetObjects(MyQueryExpression) 'Retrieve some addresses Dim MyUnitOfWork As UnitOfWork = MyContext.GetUnitOfWork() Dim MyOldAddress As Address For Each MyOldAddress In MyOldAddresses MyOldAddress = CType(MyContext.GetEditableObject(MyOldAddress),Address) MyOldAddress.Street = "New street" MyUnitOfWork.Save(MyOldAddress) Next MyUnitOfWork.Commit() 'Commit all changes
7. Simple object delete
This sample shows how to retrieve a defined object and delete it
Imports DADO.Persistency
Dim MyContext As Context = Context.FromConfiguration("C:MyConfiguration.xml")
Dim MyAddress As Address = MyContext.GetObject(GetType(Address), 123) 'Retrieve address with ID=123 MyAddress = CType(MyContext.GetEditableObject(MyAddress),Address) MyContext.Delete(MyContext)
8. Load the configuration from an embedded resource or a stream
This sample shows how to load a mapping configuration (created with DADO Database Mapper®, DADO Xml Mapper® or DADO Broker System®) from an embedded resource.
Imports DADO.Persistency
Dim MyContext As Context = Context.FromConfiguration(MyAssembly, "MyEmbeddedResource.xml")
This sample shows how to load a mapping configuration (created with DADO Database Mapper®, DADO Xml Mapper® or DADO Broker System®) from a stream (object inheriting from System.IO.Stream)
Imports DADO.Persistency
Dim MyContext As Context = Context.FromConfiguration(MyStream)
9. Set the connection string in code
This sample shows how to set the connection string of your context via code.
Imports DADO.Persistency
Dim MyContext As Context = Context.FromConfiguration("MyConfiguration.xml") Dim MyDatabaseStorage As DatabaseStorage = CType(MyContext.StorageManager.Get("MyDatabaseName"),DatabaseStorage) MyDatabaseStorage.ConnectionPool.ConnectionString = "...."
NOTE: The whole configuration can be defined in code. See the Developer SDK documentation for more details
10. How to implement a lazy loaded property
DADO Solution® allows object members to be lazy loaded according to your configuration or query expression settings.
This may be done internally (resulting transparent to the users of your objects, but being intrusive) or externally (requiring no particular attentions at object design time, but requiring your object users to ensure the member value is loaded)
Following code shows the implementation of a typical lazy-loadable property wrapping a private field. The object "ContextRef" represents a private reference (field) to your context (you can configure in DADO Database Mapper® to be setted automatically at object creation). Since the user can call the setter method before lazy loading the member value, you must unregister this value from the lazy loaded member register
Public Property MyLazyLoadedProperty As Object Get If (Not m_ContextRef Is Nothing) Then m_MyLazyLoadedProperty = m_ContextRef.LazyLoad(Me, "MyLazyLoadedProperty", m_MyLazyLoadedProperty) End If Return m_MyLazyLoadedProperty 'Return private field value End Get Set If (Not m_ContextRef Is Nothing) Then m_ContextRef.LazyClear(Me, "MyLazyLoadedProperty") m_MyLazyLoadedProperty = Value 'Set private field value End Set End Property
The following sample shows ho to do the same in a non intrusive way
MyObject.MyLazyLoadedProperty = MyContext.LazyLoad(Me, "MyLazyLoadedProperty", MyObject.MyLazyLoadedProperty)
MyLazyLoadValue = MyObject.MyLazyLoadedProperty
NOTE: The "Context.LazyLoad" loads the member only once per object and then unregister the lazy load information. Any successive call to this method on the same object and member returns the default value (last argument of the function)
11. Open a context connected to a remote application server or web service
These samples shows how create a context connected to remote server, hosted by DADO Application Server®, Microsoft IIS® or as a Web Service. Note the overloaded versions of the different "create" methods, depending of the type of proxy, security settings, etc.
After you have opened a context from a proxy, you can execute on it all operations as if it would be local.
Following sample connects to a context hosted by DADO Application Server®. Depending of the security configuration of the server you may need another version of the "create" method
Imports DADO.Persistency Imports DADO.Persistency.Net
Dim MyProxy As SecuredProxy = SecuredProxy.Create("tcp://myserver:120/MyService") Dim MyContext As Context = ClientContext.FromProxy(MyProxy)
Following sample connects to a context via remoting hosted by Microsoft IIS® server or any other remoting host application. Depending of the security configuration of the server you may need another version of the "create" method
Imports DADO.Persistency Imports DADO.Persistency.Net
Dim MyProxy As RemoteProxy = RemoteProxy.Create("http://myserver/MyService") Dim MyContext As Context = ClientContext.FromProxy(MyProxy)
Following sample connects to a context published as web service and hosted by Microsoft IIS® server or any other web service host application. Depending of the security configuration of the server you may need another version of the "create" method
Imports DADO.Persistency Imports DADO.Persistency.Net
Dim MyProxy As WebServiceProxy = WebServiceProxy.Create("http://myserver/MyWebService.asmx") Dim MyContext As Context = ClientContext.FromProxy(MyProxy)
|