Using two repositories with the same adapter.

Posts   
 
    
methodman
User
Posts: 194
Joined: 24-Aug-2009
# Posted on: 05-Mar-2010 11:20:57   

I have implemented two (customer,order) repositories. Each repository creates its own adapter instance in the ctor or get passed an instance in ctor.


public class CustomerRepository : IDisposable
{
      private IDataAccessAdapter adapter;

      public CustomerRepository(): this(new DataAccessAdapter())  {}

     public CustomerRepository(IDataAccessAdapter adapter)
     {
         this.adapter = adapter;
     }

     public EntityCollection<CustomerEntity> FetchAllCustomers(int page)
     {
         ....some code
         return entityCollection;
     }
}

public class OrderRepository : IDisposable
{
      private IDataAccessAdapter adapter;

      public OrderRepository(): this(new DataAccessAdapter())   {}

     public OrderRepository(IDataAccessAdapter adapter)
     {
         this.adapter = adapter;
     }

     public EntityCollection<OrderEntity> FetchAllOrders(int page)
     {
         ....some code
         return entityCollection;
     }
}

And i use the repository like this:


using (var repository= new CustomerRepository())
{
      var coll = repository.FetchAllCustomers(1);
}

The scenario: I need to fetch from both repositories in one service method.

I guess, one adapter instance should be used for both repositories.

I was thinking it could be something like this:


         public void SomeMethod()
        {
                ....

                using (var adapter = new DataAccessAdapter())
               {
                       var customerRepository = new CustomerRepository(adapter);
                       var orderRepository = new orderRepository(adapter);

                       var customers = customerRepository.FetchAllCustomers(1);
                       var orders = orderRepository.FetchAllOrders(1);
               }
            
               ....
}

What I dont like on this approach is the passing around of an active database connections and exposing of the infrastructure details.

Is there any better alternative to this approach ?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 05-Mar-2010 20:47:20   

I think that's ok. I don't understand your concern. Also, the connection is opened in the first Fetch.

David Elizondo | LLBLGen Support Team