resolving dependency in entity repository

Sep 23, 2012 at 3:14 PM
Edited Sep 23, 2012 at 3:16 PM

1 .I have a service which gets entity to operate from repository and perform some function on it.

2. service class, entity and repository class all need a ITrace to write trace messages.

3. repository uses stored procedure to get data from DB to build entity and does not uses Entity Framework

3. Repository is responsible for returning concreate instance of Entity type. How can I inject iTrace in entity from repository. All samples I have seen do a resolve at service class which builds the complete object graph and inject dependencies.

   
class ServiceClass
    {
        public ServiceClass(ITracer tracer)
        {

        }

        public void DoOperation()
        {
            EntityRepository repository = new EntityRepository();
            var entity = repository.GetRecord();
            entity.DoSomething();
        }
    }

    class Entity
    {
        // ??? should i use Property injection here for ITracer. Id field value come from DB
        public Entity(ITracer tracer, int id)
        {
        }

        public DoSomething()
        {
            //
        }
    }

    class EntityRepository
    {
        public Entity GetRecord(int id)
        {
            //Get data from DB
            DataReader reader = SqlDataReader;
            Entity item = new Entity(); //create new instance based on data from DB

            //???Should i do a container.Resolve here, how will i pass other constructor received from DB
        }

        public void SaveRecord(Entity item)
        {
        }
    }
Sep 24, 2012 at 5:29 AM

The recommended approach is the one that you have seen: perform a resolve at the composition root and create the object graph, injecting all dependencies.  You are certainly able to use the Container as a singleton and use it as a service locator.  However, many people consider that an anti-pattern (e.g. your code is now tightly coupled to a Container).

In my experience, entities are not usually injected since they are objects created by the repository that hold the data.  So, IMO, it would be perfectly acceptable to have the Repository constructor accept an ITracer and have the Repository new up an Entity: var item = new Entity(tracer, (int)reader["id"]); // or something similar.

Another approach to address this situation is to use an abstract factory.  Basically inject an abstract factory into the repository with an ITracer and the factory would then know internally how to create the Entity.

Just a quick comment on the posted code: the service is instantiating a new repository directly in the code.  Since the Service depends on having a repository this would usually be injected into the service along with the ITracer.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Sep 24, 2012 at 8:36 AM
so repository should take all the dependency that entity needs and service will have repository as injection. So that container will inject repository (after injecting all dependency needed by entity) to service.
Thanks