Creating Objects
Creating objects and persisting them with Telerik OpenAccess ORM is as easy as working with non-persisting ones.The scope object is an object that should be disposed after accomplishing the required operations. This can be done by calling the scope.Dispose() method or implementing the using clause.
| VB .NET |
Copy Code |
|
Dim scope As IObjectScope = ObjectScopeProvider1.GetNewObjectScope() Using scope scope.Transaction.Begin() Dim o As New Order("John Smith", New DateTime(2008, 12, 12), "Telerik's Address") o.Details.Add(New OrderDetail("RadControls for WinForms", 799.9f, 3)) o.Details.Add(New OrderDetail("OpenAccess ORM", 399.9f, 3)) o.Details.Add(New OrderDetail("RadControls for ASP .NET", 799.9f, 5)) scope.Add(o) scope.Transaction.Commit() End Using |
| C# |
Copy Code |
|
IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope(); //you could see in some expamples that this can also be done with //Database.Get("DatabaseConnection1").GetObjectScope(); //there is absolutely no diference using (scope) { scope.Transaction.Begin(); Order o = new Order("John Smith", new DateTime(2008, 12, 12), "Telerik's Address"); o.Details.Add(new OrderDetail("RadControls for WinForms", 799.9f, 3)); o.Details.Add(new OrderDetail("OpenAccess ORM", 399.9f, 3)); o.Details.Add(new OrderDetail("RadControls for ASP .NET", 799.9f, 5)); scope.Add(o); scope.Transaction.Commit(); } |
So making an object and its children to persist is very easy. It is required to add only the root object of the object graph to have all persistent entities handled automatically. Telerik OpenAccess ORM will care about the rest like adding the records in the ObjectDetail table and in the join table. That is called persistence by reachability. You should notice that your classes must be marked as [Telerik.OpenAccess.Persistent] so the ORM can traverse through the three of objects and persist all of them. If it finds any non-persistent class it will throw an exception.
In case you want to exclude a field from being persisted, e.g. a callback delegate, mark it with the Telerik.OpenAccess.Transient attribute:
| VB .NET |
Copy Code |
|
Public Delegate Sub OnProgressDelegate(pct As Double) <OpenAccess.Persistent> _ Class Order <Telerik.OpenAccess.Transient> _ Public onProgress As OnProgressDelegate = Nothing End Class |
| C# |
Copy Code |
|
public delegate void OnProgressDelegate(double pct); [OpenAccess.Persistent] class Order { ... [Telerik.OpenAccess.Transient] public OnProgressDelegate onProgress=null;
} |
Updating Objects
Updating an object state is the easiest of them all. You only need to extract the specified object, modify it and commit the transaction. You must not forget that inserting, updating and deleting objects MUST be done within a scope of a transaction. If there is no transaction open and a field changes an exception will be thrown. Here’s a simple example:
| C# |
Copy Code |
|
var result = from o in scope.Extent<Order>() where o.Customer.Contains("John") select o;
scope.Transaction.Begin(); Order firstJohnOrder = (Order)result.First(); firstJohnOrder.Customer = "Sir John The First"; scope.Transaction.Commit(); |
| VB .NET |
Copy Code |
|
Dim result = From o In scope.Extent(Of Order)() _ Where o.Customer.Contains("John") _ Select o scope.Transaction.Begin() Dim firstJohnOrder As Order = CType(result.First(), Order) firstJohnOrder.Customer = "Sir John The First" scope.Transaction.Commit() |
You noticed we extracted more than one object with the query. Here the feature called change tracking comes in. Each object has it’s own change tracker and after the transaction commit was called the ORM checks which objects are marked as dirty and stores only those that are changed. That is a great performance booster when working with large databases.
Deleting Objects
The O/R Mapping differs from the ADO .NET because you need to load your data first and then delete it. You CAN NOT write any “DELETE” statements for deleting thousands of rows in one move here. That is because O/R Mapping is all about working with single objects. SQL on the other hand is for batch operations. But what OpenAccess ORM offers is a Remove() method which could take as an argument an enumerable collection as it takes a single object so you don’t need to remove objects one by one.
Here is an example of the scope.Remove() method:
| C# |
Copy Code |
|
var result = from o in scope.Extent<Order>() where o.Customer.Contains("John") select o; scope.Transaction.Begin(); scope.Remove(result.ToList()); scope.Transaction.Commit(); scope.Dispose(); |
| VB .NET |
Copy Code |
|
Dim result = From o In scope.Extent(Of Order)() _ Where o.Customer.Contains("John") _ Select o scope.Transaction.Begin() scope.Remove(result.ToList()) scope.Transaction.Commit() scope.Dispose() |
If the object we are removing contains other objects or collections related to him(this is where the depend attribute makes advantage), they are all removed.However these objects also need to be loaded before being removed and that the OpenAccess ORM engine does for you.
 |
If you want to delete some extremely large amount of data be carefull and considering the performance-instead you may execute directly few DELETE statements inside a transaction for the purpose. |