Doubts about implementation of Lazy Load and Dependency Injection

3

The scenario is simple, it has a PersonPass class that has linked to it a list of Contacts (instances of physical persons), Telephones (instances of a Phone entity), and a list of Addresses (instances of an Address entity) / p>

I have in the current project the default layers: DAO, BusinessLayer and Gui, but Gui accesses the layers below by a Facade. Use Singleton to instantiate the Facade because it maintains instances of business layers and repositories.

I am planning a refactoring because currently the repository of the PersonPassword class loads all the information when I command to load / locate a personPass in the bank, even if I do not go to use contact data, address, etc.

So, I want to use lazy load to improve performance, but I'm not sure how to do dependency injection in the best way.

As I already have an interface to the repository, I'm thinking of using a factory that delivers proper implementations of the repositories and singleton to avoid having more than one instance, whatever the application part.

Repositories for addresses would be passed via constructor to the PersonPass class, but would you like to not force the user of the PersonPass class to instantiate the repositories and abstract that responsibility without compromising the design patterns, any ideas?

I already have the unit tests for the methods of the repositories, which greatly facilitates the refactoring.

The project is web, with ASP.NET and C # and use ADO.NET.

Do you think it's the best way? Suggestions?

    
asked by anonymous 01.05.2015 / 17:35

1 answer

3

If the idea is dependency injection, the ideal implementation is to use a Dynamic Proxy. The best link I know as introduction is this .

Since you already have the interfaces, the best way to load the object is when it is actually accessed. It is not the Façade that has to determine this. So what can be done is to put in place of related objects an object that pretends to be another.

And how is that?

Dynamic Proxy pretends to be the object that holds the data. When accessed, an interceptor goes to the data layer and exchanges the proxy object with the actual object, which actually contains the data.

This form is used by the Entity Framework and Active Record, two of the most popular object-relational mapping frameworks used so far.

    
02.05.2015 / 02:29