When I started learning Repository Pattern with Unity few days ago I was under impression that the main benefit of this pattern is the separation of data layer from the business layer.
In other words, if there is a need to change the way, how application stores the data, it's very easy as only one main model takes care of the communication.
This means, that if application currently saves data into a serialized XML files, it would not be very difficult to change this logic to connect to database instead.
I have found few nice demos that are also using Unit Of Work
layer, which seemed very handy. Let me show you a bit of the code I have.
public class UnitOfWork : IUnitOfWork
{
private readonly RepositoryContext _context;
public IEmployeeRepository Employees { get; set; }
public UnitOfWork(RepositoryContext context)
{
_context = context;
Employees = new EmployeeRepository(_context);
}
public int Complete()
{
return _context.SaveChanges();
}
public void Dispose()
{
_context.Dispose();
}
}
Main Repository Context:
public class RepositoryContext : DbContext
{
public RepositoryContext() : base("name=RepositoryContext")
{
}
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<Equipment> Furniture { get; set; }
}
And here is the demo EmployeeRepository:
public class EmployeeRepository:Repository<Employee>, IEmployeeRepository
{
public EmployeeRepository(RepositoryContext context) : base(context) { }
public Employee GetEmployeeByName(string sName)
{
return MyContext.Employees.FirstOrDefault(n => n.Name == sName);
}
public RepositoryContext MyContext
{
get { return Context as RepositoryContext; }
}
}
Employee Repository derives from a generic Repository
which looks like this:
public class Repository<T> : Interfaces.Repositories.IRepository<T> where T : class
{
protected readonly DbContext Context;
public Repository(DbContext context)
{
Context = context;
}
public void Add(T item)
{
Context.Set<T>().Add(item);
}
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
return Context.Set<T>().Where(predicate);
}
public T Get(int ID)
{
return Context.Set<T>().Find(ID);
}
public IEnumerable<T> GetAll()
{
return Context.Set<T>().ToList();
}
public void Remove(T item)
{
Context.Set<T>().Remove(item);
}
}
Here is the question:
As far as my understanding goes, we are directly declaring, that under our Repository
expects in it's constructor DbContext
, which is afterwards used under all Add / Remove / Find
functions under that particular class.
Currently this model is communicating with the database, but if I wanted (for whatever reason) to change this model to save data in the XML file, I would have to completely rewrite all my Repository
classes? Or am I missing something here?
If I am wrong and it is easily doable, could anyone show me how to change the code so that we are serializing values into the XML files, please? I am trying to better understand this Repository Pattern, yet for now it's one big chaos for me.
Any help / suggestions regarding this matter would be highly appreciated.