How to use Telerik ORM in business layer using interfaces?

4 posts, 0 answers
  1. peter joel
    peter joel avatar
    10 posts
    Member since:
    Feb 2010

    Posted 29 Jun 2012 Link to this post


    I am new to MVC and totally new to Telerik ORM. Just wanted to know:

    1) Should I create interfaces for each for .generated.cs and then use those interfaces in my business logic using Ninject?  
    2) If I don't want to have dependencies on classes then I will have to create an interface for each class? or is there a way to auto generate them? 
    3) If I am using dbContext just in my business layer and not directly in the controllers, should I still have to Dispose dbContext myself?

    Telerik help shows using dbContext directly in controllers but I am not sure if that's the best way?

    Please let me know if I am on the right path. I am thinking to create interfaces for all .generated.cs files and use (dbContext & interfaces with Ninject) only in business layer. My controllers would not know about ORM. Is this the best way?

    Thanks for your help in advance!
  2. Serge
    Serge avatar
    375 posts

    Posted 03 Jul 2012 Link to this post

    Hello Peter,

     First of all I will suggest that you have a look at the Sofia Car Rental example that we have in our SDK. It is basically a small MVC example, that shows how we create repositories and handle the context. The best practice is to have a single context object per request. It does not have to be instantiated (or even referenced) from the controller. It can easily be done by using Ninject and specifying the context life-cycle. 

    As to interfaces, yes you can modify the T4 templates that are for code generation to output interfaces for your classes (however it is not trivial), or create them on your own. However there isn't a reason to start using interfaces. How would Ninject retrieve such an object, it is not a dependency but rather a data object. You need Ninject (or any other dependency injection container for that matter) to handle the life-cycle of your context, and you need your context to retrieve data objects (entities). I would suggest not to go for interfaces. 

     I hope this helps and I would like to again recommend having a look at the SDK examples. We have spent quite a lot of time preparing the Sofia Car Rental series showing off the way we think OpenAccess should be used. 
    Kind regards,
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
  3. DevCraft banner
  4. peter joel
    peter joel avatar
    10 posts
    Member since:
    Feb 2010

    Posted 10 Aug 2012 Link to this post

    I have been doing as you suggested but got one problem. I am unable to find an example where someone has used it with Ninject. You CarRental sample does not use ninject either. I have generated a MyContext class using OpenAccess ORM Q2 2012.

    Here is what i am doing....


    By doing this, i get an exception..

    The ObjectScope has already been disposed and it's managed persistent objects can no longer be accessed. The ObjectScope should be disposed at the end of the life cycle of your business logic instance. This can also be done in the Dispose of your ASP page or MVC controller.
    Object name: 'OpenAccessRuntime.EnlistableObjectScope'.

    If I change the scope to kernel.Bind<MyContext>().To<MyContext>(); and set ConnectionPool to ADO.NET (in .rlinq properties) then there are almost 100 connections running in SQL SERVER.

    Please guide! Is there a sample that generates OpenAccessContext class and uses Ninject?

  5. Damyan Bogoev
    Damyan Bogoev avatar
    581 posts

    Posted 13 Aug 2012 Link to this post

    Hi Peter Joel,

    You could find a sample application from the Product SDK which demonstrates how to use OpenAccessContext with Ninject under the Samples tab ASP.NET MVC category section. The examples that are using Ninject are “Sofia Car Rental MVC 3 Razor and Fluent Mapping” and “Sofia Car Rental MVC 3 WebForms and Domain Model”.
    Please have a look at the attached screenshot.

    Hope that helps.

    All the best,
    Damyan Bogoev
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
Back to Top