views:

134

answers:

2

I am writing a T4 template for repositories.

Say we have Customers/Orders/Products tables. We then have CustomerRepo, OrdersRepo, and ProductsRepo.

Is it a good practice to have a generic repo for all of them?

public partial class Repository

{

private IContext context;
public Repository()
{
    _Product = new ProductRepo();
    _Customer = new CustomerRepo();
}

public Repository(IContext context)
{
    this.context = context;
    _Product = new ProductRepo(context);
    _Customer = new CustomerRepo(context);
}

private ProductRepo _Product;
public ProductRepo Product {
    get { return _Product; }
}
// Product

private CustomerRepo _Customer;
public CustomerRepo Customer {
    get { return _Customer; }
}
// Customer

}

+1  A: 

I find that it is best to make a repository for each type rather than one single repository. Otherwise the repository tends to get large as all sorts of functionality is added.

stimms
I do have a repository for each type, but this "generic" repository would be an entry point for all of them.
Rick Ratayczak
+1  A: 

You can even look at implementing your repository like this post. You can even extend the concept and implement generic and have IRepository<T> where T:IEntity and then have a base implementation called BaseRepository<T>. Based on your additions to the question you might want to use Factory Pattern or Facade Pattern or Dependency Injection (using Unity/StructureMap container). It depends on how complex it is and how flexible your solution needs to be.

Perpetualcoder