Generic Unit of Work & (Extensible) Repositories Framework with lazyloading

Dec 26, 2014 at 2:39 PM
  1. Can I have an example Code or simple project that use this Framework on Lazy Loading
2.Normaly with entity framework (lazy loading mode is active) when we retrieve an object there related object not retrived unitl we call some properties of his related object. But in my project with this framework and on lazy loading mode i dont have this result the related is automaticlay loaded when retrieve my parrent object ? i need a help ?

this is the constructor of my data context :

public DataContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
        _instanceId = Guid.NewGuid();
        //Configuration.LazyLoadingEnabled = false;
        //Configuration.ProxyCreationEnabled = false;

And this is an Example of my Controller :
              //Loading agencies only (but not working because i get all the related objects)
        IList<Agency> Agence = AgencyService.Query().Select().ToList();

        Agency Ag = Agence[0];

        //Loads Agency company for particular Agency only (seperate SQL query) => also not working 
        Company company = Ag.Company;
Dec 29, 2014 at 10:18 AM
Marked as answer by lelong37 on 12/29/2014 at 2:19 AM
Dec 29, 2014 at 2:04 PM
Thanks For you reply.
1.Can You show me the best approch used for the CRUD Operations (ADD, Delete , Update).
  1. I have a problem with my dbset it doesn't refresh , the probelm occur when i do this scenario :
       Note : in my model an agency has a related object which is Company 
        Scenario :
        1. Adding a Company 
        2. Adding Agency with the last comany added 
        3.Retrieve the list of agencies 
        4. the exception occurs the last ageny added has a company object null but in the database all the data added successfully i don't know why ?
Can you help me please , thanks a lot
Jan 11, 2015 at 5:09 PM
Edited Jan 13, 2015 at 6:31 PM
The reason you couldn't lazy load was that the Configuration.ProxyCreationEnabled setting is false. Set that and the LazyLoadingEnabled property to true and then lazy loading will work (if your property is virtual) outside the service even though the framework returns an IEnumerable (all it's actually doing is using the AsEnumerable method to cast the the IQueryable result to an IEnumerable).

The danger of this is that a caller of the service can actually start adding eager loading to the result set by simple casting back the IEnumerable to an IQueryable and adding the EF eager loading include extension method or the AsNoTracking extension method. If you want to stop this happening you need to fetch the data in the service itself by calling the ToArray or ToList method rather than using AsEnumerable.