Doubts about implementation of Lazy Load and Dependency Injection

Asked

Viewed 256 times

3

The scenario is simple, it has a Personal Physical class that has linked to it a list of Contacts (instances of physical persons), Telephones (instances of an entity Telephone) and a list of Addresses (instances of an entity Address).

I have in the current project the standard layers: DAO, Businesslayer and Gui, only that Gui accesses the layers below by a Facade. Use Singleton to instantiate Facade, as it maintains instances of business layers and repositories.

I am planning a refactoring because currently the repository of the class Personal Physique loads all information when I have a person uploaded/located in the database, even if I am not going to use the data of contacts, addresses, etc.

So, I plan to use Lazy load to improve performance, but, I’m having doubts on how to do dependency injection in the best way.

Since I already have an interface for the repository, I’m thinking of using a Factory that delivers the proper implementations of the repositories and Singleton to avoid having more than one instance, whatever the part of the application.

The repositories for the addresses would be passed via constructor to the Personal Physical class, but I would like to not force the user of the Personal Physical class to instill the Repositions and abstract this responsibility without compromising the project standards, some idea?

I already have the unit tests for the methods of the Positorios, which makes refactoring much easier.

The project is web, with Asp.net and c# and ADO.NET usage.

Do you think this is the best way? Suggestions?

  • What exactly are you using in the data layer? Any framework? Have you implemented the layer using your code? There are frameworks that already perform this work for you.

  • @Ciganomorrisonmendez I am implementing everything in hand, I use no framework (Entity, etc). It was a matter of conceptual integrity, the project already had legacy code (.net 2.0) and I chose to continue maintaining the standard so as not to have to refactor everything and to avoid staying with the same layer components implemented with different tools.

1 answer

3


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

Since you already have the interfaces, the best way to load the object is when it is effectively accessed. It is not the Facade that has to determine this. So what can be done is to put in place of the 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 initiator goes to the data layer and exchanges the proxy object for the true object, which contains the actual data.

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

  • Thanks for the reply, I will give a search on the dynamic proxy and give a feedback. In fact, I did not plan to use the knife to manage this, it just encapsulates the calls with polymorphism and ensures that I have no more instances than I need with Singleton, as soon as I finish the search on the proxy I give a return. Thanks

  • 1

    Old man, I took a look at the link you gave me and I went after my good old GOF and I was there the answer, I’m gonna have to make some more changes to the charging methods so I can encapsulate everything I need and control via proxy, but, will be structural corrections that will improve the code, summarizing, I will apply the same proxy :) (answer checked) worth

Browser other questions tagged

You are not signed in. Login or sign up in order to post.