I also have a base Repository for re-use all things that you think it can shared for all Repository used.
public abstract class BaseRepository<T> : IDisposable
{
private DbContext _dbContext;
/// <summary>
/// Initializes a new instance of the <see cref="BaseRepository<T>"/> class.
/// </summary>
/// <param name="databaseFactory">The database factory.</param>
public BaseRepository(IDatabaseFactory databaseFactory)
{
Contract.Assert(databaseFactory != null, "DatabaseFactory is null");
DatabaseFactory = databaseFactory;
}
/// <summary>
/// Gets or sets the database factory.
/// </summary>
/// <value>The database factory.</value>
protected IDatabaseFactory DatabaseFactory
{
get;
private set;
}
/// <summary>
/// Gets the db context.
/// </summary>
/// <value>The db context.</value>
protected DbContext DbContext
{
[DebuggerStepThrough]
get
{
return _dbContext ?? (_dbContext = DatabaseFactory.Get());
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
if (DbContext != null)
GC.SuppressFinalize(DbContext);
if (DatabaseFactory != null)
GC.SuppressFinalize(DatabaseFactory);
}
}
Now we can implement the CategoryRepository as below:
public interface ICategoryRepository : ICommandRepository<Category>, IQueryRepository<Category>
{
}
public class CategoryRepository : BaseRepository<Category>, ICategoryRepository
{
/// <summary>
/// Initializes a new instance of the <see cref="CategoryRepository"/> class.
/// </summary>
/// <param name="databaseFactory">The database factory.</param>
public CategoryRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)
{
}
/// <summary>
/// Units the of work.
/// </summary>
/// <returns></returns>
public IUnitOfWork UnitOfWork()
{
return DbContext as CSPDbContext;
}
/// <summary>
/// Gets the by id.
/// </summary>
/// <param name="id">The id.</param>
/// <returns></returns>
public Category GetById(int id)
{
Contract.Assert(DbContext != null, "DbContext is null");
var temp = DbContext as CSPDbContext;
return (temp).Categories.Where<Category>(x => x.Id == id).FirstOrDefault<Category>();
}
/// <summary>
/// Adds the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
public Category Add(Category entity)
{
Contract.Assert(DbContext != null, "DbContext is null");
var temp = DbContext as CSPDbContext;
temp.Categories.Add(entity);
return entity;
}
...
}
This is a hard pattern in EF4, so I will don’t explain about it. I just gave you a reference to read it, and I also implemented according to this link
This is my code:
public interface IUnitOfWork
{
/// <summary>
/// Saves this instance.
/// </summary>
void Save();
}
public partial class CSPDbContext : DbContext, IUnitOfWork
{
...
/// <summary>
/// Saves this instance.
/// </summary>
public void Save()
{
SaveChanges();
}
}
I gave the CSPDbContext implementing the IUnitOfWork contract, and
public class CategoryRepository : BaseRepository<Category>, ICategoryRepository
{
/// <summary>
/// Initializes a new instance of the <see cref="CategoryRepository"/> class.
/// </summary>
/// <param name="databaseFactory">The database factory.</param>
public CategoryRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)
{
}
/// <summary>
/// Units the of work.
/// </summary>
/// <returns></returns>
public IUnitOfWork UnitOfWork()
{
return DbContext as CSPDbContext;
}
...
}
Now I do unit testing for CategoryRepository as:
[TestMethod()]
public void GetAllTesting()
{
var result = CategoryRepository.GetAll();
Assert.IsNotNull(result);
}
[TestMethod()]
public void AddCategoryTesting()
{
var entity = CategoryRepository.Add(new CallStoreProc.Data.Entity.Category()
{
Name = "test",
IsDelete = false,
CreatedDate = DateTime.Now,
Description = "test"
});
Assert.IsNotNull(entity);
Assert.AreEqual(entity.Name, "test");
Assert.AreEqual(entity.IsDelete, false);
}
Unit testing for UnitOfWork:
[TestMethod()]
public void UnitOfWorkTesting()
{
var uow = CategoryRepository.UnitOfWork();
var entity1 = CategoryRepository.Add(new CallStoreProc.Data.Entity.Category()
{
Name = "test3",
IsDelete = false,
CreatedDate = DateTime.Now,
Description = "test3"
});
var entity2 = CategoryRepository.Add(new CallStoreProc.Data.Entity.Category()
{
Name = "test4",
IsDelete = false,
CreatedDate = DateTime.Now,
Description = "test4"
});
uow.Save();
}