11 Answers, 1 is accepted
the tool can be used in two principal ways: forward or reverse. Reverse engineering is described in this blog post.
Forward engineering can be as simple as the following steps:
Create a new project (VB or C#), preferrably a class library.
Enable the project to use the Telerik OpenAccess ORM. During the setup process you will be answered a few questions like which backend to use or which database server name etc. This enabling adds a reference to our API assembly and puts an additional compilation step into the project.
Add a new class, let's say Person with a non-public string field like name.
Over that class, put a Telerik.OpenAccess.Persistent attribute.
Build the assembly which transparently enhances the assembly and generates a new database also.
To use the database, you need to open it. Please refer to the reference docu, topic Database.
static void CRUD(IObjectScope scope) |
{ // Make an object in the normal way |
Person p = new Person(); |
p.Name = "Kevin"; |
// CREATE the persistent image from the memory instance |
scope.Transaction.Begin(); |
scope.Add(p); // now the scope knows about the instance |
scope.Transaction.Commit(); // now the database is modified |
// OBTAINING the database identity of the instance |
IObjectId oid = scope.GetObjectId(p); // either app visible or database internal |
// READ the instance by asking the database to resolve the identity |
using (IObjectScope scope2 = scope.Database.GetObjectScope()) |
{ // just to show how to retrieve an object by is identity, otherwise not necc. |
// the person could also be in the result of a query |
Person p2 = (Person)scope2.GetObjectById(oid); |
Console.WriteLine(p.Name); // reads the value from the database |
} |
// UPDATING (here without extra scope bracket) |
scope.Transaction.Begin(); // all modifications must be done inside a transaction |
p.Name = "Thomas"; // changes the object in memory, still managed by the scope |
scope.Transaction.Commit(); // changes the database content |
// DELETE the instance from the database |
scope.Transaction.Begin(); // all modifications must be done inside a transaction |
scope.Remove(p); |
scope.Transaction.Commit(); // changes the database content |
} |
public static void Main(string[] args) |
{ |
using(IObjectScope scope = Database.Get("DatabaseConnection1") |
.GetObjectScope()) |
{ |
CRUD(scope); |
} |
} |
Please find above the CRUD method, which shows you how to use OpenAccess to create, retrieve, update and delete a simple persistent instance from the database.
Kind regards,
Thomas
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
I'm new to OpenAccess as well and I have a question regarding you're loading of a single record. I was shown this way (continuing your Person class example)
string personId = "500";
Person p = new Person()
IObjectId oid = scope.GetObjectId(p.GetType(),personId);
Person p2 = (Person)scope.GetObjectById(oid);
I don't see a way to provide the Identifier in the way you demonstrated - that's why I'm asking what is the way to best do it?
Thanks in advance,
Al
you should be able to obtain the oid instance for a Person with 500 as identified by using
IObjectId oid = Database.OID.ParseObjectId(typeof(Person),"500");
That will create an instance which can be used later to obtain the Person object back from the object scope.
Hope this helps,
Thomas
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
im vignesh,
im new in open access ORM
i have one doubt how to connect stored procedures in ORM using Reverse mapping engineering
pls help me.
i waiting for u reply,
this is my mail id m.vigneshbabu@gmail.com
thanks,,
You have several choices here:
Use already existing stored procedures from your sql server or you can define such in the app.config file. More information regarding those two approaches can be found here.
When comes to stored procedures you can easily let OpenAccess generate them for you You will first need to actually tell OpenAccess to generate those stored procedures for you. The functionality is available in the reverse mapping wizard, just open it and click on the Advanced View (TreeView) tab.
You can choose the class for which you want to create a stored procedure. When you select class mark the Use stored procedure check box and on the insert procedure(or any other procedure) from the drop down list instead of Use Dynamic SQL choose Create Stored Procedures. Click generate and save the configuration. Check if the Update Database property of your class library project is set to true and if so rebuild your project. When you rebuild your project the stored procedure will be created on your sql server. Now you need to open the reverse mapping wizard again and do a merge to reflect the changes in the sql server. Merge the new procedures. When you do so you will be able to find those procedures in the Stored Procedures node in the Advanced View(TreeView) tab. Choose the procedure you want to generate and set the property Generate method to true. Click generate and save the configuration. Now your stored procedure will be available as a static method into StoredProcedure.cs class inside your class library. To invoke them you will need to call [ClassLibraryName].StoredProcedures.[ProcedureName].
Note that the newly created stored procedure will require all the fields from your table plus an instance of the IObjectScope.
Regards,
PetarP
the Telerik team
Check out Telerik Trainer , the state of the art learning tool for Telerik products.
I'm very new to this OpenAccess ORM. And right now I found a difficulty updating a record.
Is it really true in order to update the record, the persistence object has to be wrapped in the scope transaction.
Does this mean that we have to keep the scope object stay alive along with the persistence object?
so we can begin the transaction and update the data of the persistence object and then the transaction is committed to update the record to the database.
How about if the current system is a disconnected environment, let's say multi-tiered web site.
What is the best approach to comply with this situation? because I don't think it is a good idea to pass the scope object until the presentation layer.
Thank you for advance.
Should I misunderstand the concept please do accept my apologize.
Regards,
Teguh
It is true that you need an active scope with which to make updates to the persistent objects. One thing you can do in order to avoid keeping a database context (such as the Entity Diagrams context) alive for a long time is get an instance of the context at the time you need to make the update, retrieve the persistent object you will change through the context, modify the object properties, save the changes and dispose of the context. With the IObjectScope you should start a transaction before the modification takes place and commit it. If you do not have a direct reference to your scope you can obtain it with the GetContext method of the Database class where you need to pass the persistent object for which you will introduce changes.Creating instances of the scope or the context does not cause additional overhead as they are designed to be lightweight. There are some recommendations on using the context and the scope when developing different applications. For example, it is best considered to have one context per thread in Windows Forms and one context per request in Web applications. A request here means loading a web page.
There are some very useful knowledge-base articles which elaborate on the best practices in development with OpenAccess. I strongly advise you to read them.
Best practices in web development with OpenAccess
Best practices in web development with OpenAccess – Part 2
Northwind N-Tier Web Demo Application
Best practices for scope management in Windows Forms
We also have a disconnected API which is supported only with the classic wizards. The disconnected API provides means for serializing an ObjectContainer with the persistent objects inside it.
I have provided links to our blog posts for the ObjectContainer in another thread where you have asked for the difference between an object container and a scope.
Using the ObjectContainer in disconnected scenarios
Using the ObjectContainer in disconnected scenarios – Part 2
I do hope the resources I have pointed here will give you a feel of the development process with OpenAccess. Should you have more questions or you need us to dig into further details, do not hesitate to contact us.
Greetings,
Petko_I
the Telerik team
Thank you for the detailed response.
However we have some concerns in the implementation in the WCF service. Currently we are using Telerik Sitefinity for our UI and using WCF service to communicate with the business layer.
We have successful to retrieve/get the data from database but not for editing the record.
Is there any working sample in regards to this type of implementation?
Please advice.
Warm regards,
Teguh Suryadi
There is a nice Northwind WCF N-Tier Demo Application (different from the other Northwind N-Tier Web Demo Application) which shows how to implement a WCF service with OpenAccess as a DAL. The application logic is spread across multiple layers but this provides immense flexibility with regard to defining the service contract. The DataContracts project repeats the definition of the generated classes for the persistent objects in the OAModel project, decorates the classes with the appropriate attributes and therefore manages the serialization issues emerging from transporting custom objects through the services. The message contracts, service contract and service implementation are also distributed across separate projects. The OADataProvider class in the OABLL project communicates directly with the DAL and also translates a generated persistent object into one that has a contract for the service method invocations.
This solution is complicated but it demonstrates a best practice in WCF development. I sincerely hope it will give you ideas on how to proceed with the updates (the OADataProvider handles such logic). Do not hesitate to contact us again, should you need more information.
Kind regards,
Petko_I
the Telerik team
Thank you for the detailed explanation and sample project.
Based on the provided example, if I'm not mistaken it requires to manually (by code) translate the persistence object to the WCF Data Contract (NSV.NW.OABLL.Assemblers) and in the OADataProvider translate back the Data Contract Object to the corresponding persistence class.
In my opinion it will consume development effort. My questions is this translation steps are required still if the WCF service which bridges the Web (Sitefinity) to the DAL (OpenAccess ORM)?
And is there any suggestion on the best practice that maximize the productivity when we want to use Sitefinity and OpenAccess ORM as its back-end ? or sample another sample would be great :)
Sorry to bother you back and forth with all of these questions :)
Many thanks,
Teguh
Your observations regarding the development effort for WCF services are correct – extra coding is required to make the service work smoothly. Currently we do not have specialized examples for OpenAccess ORM and Sitefinity used together. However, the best practices for web development are also applicable when using Sitefinity.
As I understood, you want to automate the process of creating the WCF service. This can be done with our Data Services Wizard. I suggest you have a look at the following links:
Getting Started With the Data Services Wizard
If you need more information or a specific task you want to accomplish, do not hesitate to contact us back for further guidance.
Sincerely yours,Petko_I
the Telerik team