Nathan J Pledger

Program.X musings from the Isle of Man concerning ASP.NET, in particular accessibility, web standards and neat ideas.

Telerik OpenAccess ORM Mapper : Lessons Learnt

When looking at developing new applications I always end up developing my own data layer. Largely because I do not like the dictatorial design principles spouted by the likes of ActiveRecord, nHibernate, et al. I write them according to real life situations where database design isn't as clean as it perhaps should be and there is no opportunity to make key changes, etc. And you know what, rolling your own is so much easier and much less hassle.

For my current project, though, I thought I'd try something "new" ...

I have been looking at Telerik's new OpenAccess ORM application recently. Telerik acquired the software a while back and have introduced it into their excellent value Premium RadControls product suite. The Express version is a good way to investigate if it is for you, so long as you are using it on a "free" database server such as SQLExpress, SQLLite, etc.

I thought I'd jot down some quick lessons I have learnt along the way.

Templates

My coding style does not match Telerik's code style as generated from the OpenAccess Reverse Engineering option. Whereas Telerik's generated form of a private member is in the format:

private string siteName;

My preferred style is:

private string _siteName;

There are few things more frustrating than adopting someone elses coding style, so I set about investigating how to get around this.

There are a number of templates that define how the classes are built when you Reverse Engineer, which are stored in the C:\Program Files\Telerik\OpenAccess ORM\sdk\IDEIntegrations\templates\PCClassGeneration folder (or your equivalent).

To change the private member format, you need to open all the files and look for a reference to the "$fieldName" variable. I changed this to "_$fieldName" and it generated the correct code.

It did not, however, generate the correct mappings, which are stored in the reversemapping.config and App.config files, which I had to tweak manually. I still have a forum post outstanding on this one.

Deferred Execution

If you get an "IObjectScope is closed" exception, you'll probably be doing something intelligent like creating a nice Factory Design Pattern, whereby you get your object within the IObjectScope and return it out for use by your BIZ objects. OpenAccess execution is deferred, however, which means if you access any of the properties outside of the IObjectScope context, you get the exception. So, for my factory method that Gets a Site object:

 

        public object Get(Guid id)
        {
            Site site = null;
            using (IObjectScope scope = Database.Get(_session.ConnectionId).GetObjectScope())
            {
                scope.Transaction.Begin();
                using (IQueryResult result = scope.GetOqlQuery("SELECT * FROM SiteExtent").Execute())
                {
                    if (result.Count > 0) site = (Site) result[0];
                    string s=site.SiteName;
                }
                scope.Transaction.Commit();
            }
            return site;
        }

Note that I access the site.SiteName property and discard it. This just forces OpenAccess to perform the Get and put the Site object into memory so when I drop out of the method, I don't lose the object.

Using LINQ

Telerik say their Open Access solution is a mid point between LINQ to SQL and Entity Framework. Seems good to me, but there are a few gotchas you need to know about.

As with anything in the OpenAccess ORM (unless you are working with SQL directly), you don't work with tables, you work with Extents. The non-LINQ way of doing things int he code sample above shows the OQL query "SELECT * FROM SectionExtent". The table name is Section, but OpenAccess maps it into an Extent. The same model works when using LINQ. Before you can start using Extents in LINQ, you need to add the Telerik.OpenAccess.Query namespace.

 

 

If you're using of the Telerik products, you might want to follow @telerikbuzz on Twitter for useful information and updates.

Comments

Mark said:

Might you have a "helper" class for Telerik's OA at this point?  I'm hoping to simplify CRUD operations as much as possible, even using generics if possible.

Also, my understanding is that one has to create a "factory" (singleton though is all which is necessary) prior to creating scopes.  This true?  App_start was recommended, but unsure how to do exactly.  Example?

Lastly, any thoughts on comparing NHibernate to OpenAccess?  Trying to get unbiased opinion on which might be the best choice from performance, scalability, and ease-of-use standpoints.

Thanks for any assistance!

# January 18, 2009 11:01 PM

Nathan Pledger said:

My Factory pattern does provide a helper class function, though I think you might be referring to a class that just implements CRUD operations. This is all good. I don't know about the feasibility of using Generics, though, as the OQL syntax "SELECT * FROM SiteExtent" won't be able to benefit from the Generic applied to, say, class CRUDHelper<T> - though you could always use reflection to generate the OQL syntax.

I am using a Factory pattern, but I don't think this is necassary, per se. My use of a Factory is not singleton (each "Session" generated its own Factories,  to provide user-specific secured Factories), and I have found no requirement to initialise the OpenAccess Framework in App_Start or anywhere. I guess this initialisation would be of your Factory, not necassarily the Framework. If so, in a web app. Application_Start would be a good place. You'll find this in your Global.asax file. Have a look at articles.techrepublic.com.com/5100-10878_11-5771721.html about using this file, but your ApplicationStart event looks like:

protected void Application_Start(Object sender, EventArgs e) {

Application["Title"] = "Builder.com Sample";

}

My thoughts on nHibernate are that although it is a capable framework, my experience was cut short due to frustration with the "support" offered by the "community". Once again, its another Open Source project that is full of politics and certain individuals who design software around purist principles instead of real-world requirements, such as Composite Keys. I won't use it again, but I don't see why anyone else shouldn't. OpenAccess seems to be a solid product and you would benefit from having Telerik behind you for support - their support options and service is impressive and they have wide respect for their after-sales service. You're not going to rely on politics and developers in their bedroom to give you support when you need it.

For my major projects, I have chosen not to use OpenAccess at this point, largely because it isn't a natural fit with our framework. I am currently looking at Entity Framework, which is looking very sweet.

# January 19, 2009 7:26 AM

Mark said:

Thanks so much for your feedback.  I found it very useful.

I had done some research on Microsoft's EF and came to the opinion that it is rather immature and missed the boat on several things right from the start, even after the long history of "competing" products / approaches on the market.  I heard its next update could be 6-8 months away and even then, not exactly what is as helpful as the ORMs on the market.

Lastly, I read an article where the EF's developers basically stopped and made a statement of how they didn't think the EF was on the right track and that it would work as intended.

Should I be looking again at the EF in your opinion?  Was I mislead?

# January 19, 2009 9:49 PM

Nathan Pledger said:

I think when you look at EF and the politics around it you probably need to consider that an awful lot of developers who jumped on LINQ-to-SQL feel a bit jilted because MS decided to move on to EF. There is a lot of bad vibe around at the moment. I'm quite pleased that my initial trepidation regarding LINQ-to-SQL was right.

EF so far has been good to me. I have struggled setting up a 1..* relationship in the designer, but that is more because the manner the designer uses is a little odd. I'm not going to be able to immerse myself entirely within EF because despite its claims to map well to an OO environment, it requires your objects to inherit from base classes defined my MS, which I want to avoid. Therefore, my implementation isn't going to be as rich as it perhaps could be, in EF 2.0, for example which promises POCO support from all accounts. EF is looking very strong for me, particularly because it is aligned somewhat with WCF.

Check out this crib sheet to have a look at some code: www.simple-talk.com/.../entity-framework-the-cribsheet

MS' docs are also as good as usual, but can be quite abstract at times - but I guess that's the point!

Btw to add to the confusion, @shanselman (twitter.com/shanselman) was saying about nHibernate today that docs for it are spread thinly and widely, which I would agree with.

# January 19, 2009 10:14 PM

Mark said:

I experienced similar frustration with NH per the lack of support.  I was simply trying to get started and I was disappointed in getting NO response on some boards after several days and limited ones on others.

There is definite concern there for me and you're right; with Telerik, paid support makes a huge difference.  OA will likely tie well/better with the Telerik controls per having the same supplier.

Will keep the EF in mind and await the next release, kind of like I'm doing with Silverlight.  A "read-only" designer for XAML apps in VS is just silly.  It seems an obvious case of MS releasing a technology before they were prepared for it themselves.  Now I have to purchase and maintain a separate environment, Expression XXXX, in order to build a Silverlight app?  I'll just stick to ASP.NET apps in one IDE.

# January 20, 2009 1:25 AM

DGDev said:

Excellent post mate. The past few days I've been gathering as much info I can regarding OpenAccess, nHibernate, and EF.

I'm currently reading "O'Reilly - Programming Entity Framework" which so far has provided some informative material regarding this solution. However, like most of you have stated above, I think I'll wait till EF matures a bit further before implementing it in large projects.

Telerik's OpenAccess seems to be a solid product; much like their ASP.NET Components.

# February 17, 2009 10:31 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 4 and 5 and type the answer here: