Quantcast
Channel: Geekswithblogs.net
Viewing all articles
Browse latest Browse all 6441

Building a Repository Pattern against an EF 5 EDMX Model - Part 2

$
0
0

I know it has been a couple months since I first wrote part 1 of building a repository pattern with an EF model, but here we are.  I am finally getting around to writing part 2.  If you remember, in part 1 I talked about creating an EF model (EDMX) connected to an existing SQL server database and mapping the stored procedures to the entities and creating the generic repository classes.  This post will dive into how to create the concrete implementation to the repository that will be used by a WCF service.

Using the example of a Customer entity, we need to create the customer repository interface and class:

   1:  publicinterface ICustomerRepository : IRepository<Customer>
   2:  {
   3:  }
 
   1:  publicclass CustomerRepository : DbContextRepository<Customer>, ICustomerRepository
   2:  {
   3:  public CustomerRepository(DbContext context)
   4:              : base(context)
   5:          {
   6:          }
   7:  }

After implementing this class, we will have a CustomerRepository object that we can use to Select, Insert, Update, and Delete Customer objects from the DbContext.  I created a separate interface and class for each entity.  Since I am using the repository from a WCF service I wanted a way to group my repositories and easily call the Select and Update methods from the service.  I did some research on the Unit of Work pattern and I decide on implementing a variation like so: 

   1:  publicinterface IUnitOfWork : IDisposable
   2:  {
   3:  // Save pending changes to the data store.
   4:  void Commit();
   5:   
   6:  // Repositories
   7:          ICustomerRepository Customers { get; }
   8:  }
 
   1:  publicclass UnitOfWork : IUnitOfWork
   2:  {
   3:  privatereadonly DbContext ctx;
   4:   
   5:  public UnitOfWork(DbContext dataContext)
   6:          {
   7:  if (dataContext == null)
   8:  thrownew ArgumentException("context");
   9:   
  10:              ctx = dataContext;
  11:          }
  12:   
  13:  publicvoid Commit()
  14:          {
  15:              ctx.SaveChanges();
  16:          }
  17:   
  18:  private ICustomerRepository _customerRepository;
  19:   
  20:  public ICustomerRepository Customers
  21:          {
  22:              get { return _customersRepository ?? (_customerRepository = new CustomerRepository(ctx)); }
  23:          }
  24:  }

This allowed me to access the repository from the WCF service like so:

   1:  public MyService(IUnitOfWork unitOfWork)
   2:  {
   3:              uow = unitOfWork;
   4:  }
 
   1:  public Customer GetCustomer(int id)
   2:  {
   3:         var cust = uow.Customers.GetById(id);
   4:  return cust;
   5:  }

I hope you find both posts helpful and I would love to hear any comments or suggestions on making this implementation better.


Viewing all articles
Browse latest Browse all 6441

Trending Articles