patterncsharpMinor
Generic Repository: Am I over-complicating?
Viewed 0 times
genericoverrepositorycomplicating
Problem
I have created a generic repository for my MVC 3 page. I am using interfaces and a UnitOfWork. But before I set out to do this? Is this the right way to separate my databaseaccess from my viewmodels and views?
WHY I DID IT:
I want to seperate my databaseaccess from my viewmodels and my views, I also want the option to have specific methods for inserting some of my data hence the UserRepository. I also want a controlled environment using the UnitOfWork.
FEARS:
Am I over-complicating everything using these interfaces? Should I ditch the generic repository, I will use it for "basic" inserts so it's not useless by any means. I do understand that I might save some time having the generic repository, only to be creating specific repositories for some of my inserts/update/deletes.
Summary:
It consists of 6 files at the moment of which 2 are:
Code
IRepository:
Repository:
IUnitOfWork:
Before you kick my butt, I am to implement the save,rollback etc. methods this is just to show you what I'm doing.
``
WHY I DID IT:
I want to seperate my databaseaccess from my viewmodels and my views, I also want the option to have specific methods for inserting some of my data hence the UserRepository. I also want a controlled environment using the UnitOfWork.
FEARS:
Am I over-complicating everything using these interfaces? Should I ditch the generic repository, I will use it for "basic" inserts so it's not useless by any means. I do understand that I might save some time having the generic repository, only to be creating specific repositories for some of my inserts/update/deletes.
Summary:
It consists of 6 files at the moment of which 2 are:
IRepository
GenericRepository
IUnitOfWork
UnitOfWork
IUserRepository
UserRepository
Code
IRepository:
public interface IRepository where T : class
{
IEnumerable BaseFetchAll();
void BaseAdd(T entity);
void BaseDelete(T Entity);
}Repository:
public abstract class GenericRepository : IRepository where T : class
{
private IObjectSet _dbContext;
private Entities _context;
protected DbSet dbSet;
internal GenericRepository(Entities context)
{
this._context = context;
dbSet = context.Set();
}
public void BaseDelete(T entity)
{
dbSet.Remove(entity);
}
public IEnumerable BaseFetchAll()
{
return (IEnumerable)(from x in dbSet select x).ToList();
}
public void BaseAdd(T entity)
{
dbSet.Add(entity);
}
}IUnitOfWork:
Before you kick my butt, I am to implement the save,rollback etc. methods this is just to show you what I'm doing.
``
public interface IUnitOfWork
{
IUserRepository UserRepository { get; }
IUserFileRepository UserFileRepository { get; }
}
`Solution
I think if you are going to have only one Repository, then yeah this is over complicated! But if you will have many Repositories and you will be adding more and more, then you are on the right track. But couple of points:
I am not sure how your are using your UoW. From my point of view UoW should look like something like:
And UoW should be passed to Repositories. So basically you have a UoW that couple of Repositories may use to get their job done (get/insert records for instance). From where I look at this a UoW is not specific to a single Repository and can span multiple Repositories.
Imagine for instance that your UoW involves creating an Order entity and updating the Customer entity together.
Also I would suggest to tighten
This basically is saying that repositories are not supposed to be created for any sort of class. Your Domain classes can then implement IDomainObject to satisfy this condition. IDomainObject can be empty or depending on your Domain it can have some Domain related properties.
I am not sure how your are using your UoW. From my point of view UoW should look like something like:
interface IUnitOfWork
{
Begin();
End();
Rollback();
}And UoW should be passed to Repositories. So basically you have a UoW that couple of Repositories may use to get their job done (get/insert records for instance). From where I look at this a UoW is not specific to a single Repository and can span multiple Repositories.
Imagine for instance that your UoW involves creating an Order entity and updating the Customer entity together.
Also I would suggest to tighten
IRepository definition to:public interface IRepository where T : IDomainObjectThis basically is saying that repositories are not supposed to be created for any sort of class. Your Domain classes can then implement IDomainObject to satisfy this condition. IDomainObject can be empty or depending on your Domain it can have some Domain related properties.
Code Snippets
interface IUnitOfWork
{
Begin();
End();
Rollback();
}public interface IRepository<T> where T : IDomainObjectContext
StackExchange Code Review Q#37554, answer score: 3
Revisions (0)
No revisions yet.