I am testing my generic repository that I'm trying to use to store various entities using Entity Framework. I have a test to see if inserting an employee works:
[TestMethod]
public void TestInsertEmployee1()
{
InitializeDatabase();
EFEmployee testEmployee1 = initializeEmployee1();
EFRepo.Insert<EFEmployee>(testEmployee1);
// what goes here to check if testEmployee1 has been inserted correctly?
// I was thinking of a asserting that a count aggregation method of the
// database is 1, but I don't know how to do that.
}
This is how my generic repository looks:
public class EFEmployeeEntityRepository : IEFEmployeeEntityRepository
{
private readonly IBusinessEntityContext _context;
public EFEmployeeEntityRepository(IBusinessEntityContext context)
{
this._context = context;
}
public virtual IQueryable<TEntity> BeginQuery<TEntity>() where TEntity : EFBusinessEntity
{
return _context.Set<TEntity>();
}
public void Delete<TEntity>(TEntity entity) where TEntity : class, IBusinessEntity
{
try
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
_context.Set<TEntity>().Remove(entity);
this._context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
var msg = string.Empty;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
msg += Environment.NewLine + string.Format("Property: {0} Error: {1}",
validationError.PropertyName, validationError.ErrorMessage);
}
}
var fail = new Exception(msg, dbEx);
throw fail;
}
}
public TEntity GetById<TEntity>(Guid id) where TEntity : class, IBusinessEntity
{
return _context.Set<TEntity>().Find(id);
}
public void Insert<TEntity>(TEntity entity) where TEntity : class, IBusinessEntity
{
try
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
_context.Set<TEntity>().Add(entity);
this._context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
var msg = string.Empty;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
msg += string.Format("Property: {0} Error: {1}",
validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;
}
}
var fail = new Exception(msg, dbEx);
throw fail;
}
}
public void Update<TEntity>(TEntity entity) where TEntity : class, IBusinessEntity
{
try
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
TEntity existing = _context.Set<TEntity>().Find(entity.Id);
if (existing != null)
{
_context.Entry(existing).CurrentValues.SetValues(entity);
this._context.SaveChanges();
}
}
catch (DbEntityValidationException dbEx)
{
var msg = string.Empty;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
msg += Environment.NewLine + string.Format("Property: {0} Error: {1}",
validationError.PropertyName, validationError.ErrorMessage);
}
}
var fail = new Exception(msg, dbEx);
throw fail;
}
}
}
This is what my context class looks like:
public class EFEmployeeEntityModelContext : DbContext, IBusinessEntityContext
{
public EFEmployeeEntityModelContext()
: base("EFEmployeeEntityModelContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType &&
type.BaseType.GetGenericTypeDefinition() == typeof (EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
modelBuilder.Entity<EFEmployee>().HasRequired(t => t.Office).WithMany(u => u.Employees).HasForeignKey(d => d.OfficeID);
modelBuilder.Entity<EFEmployee>().HasMany(t => t.Managing).WithRequired(t => t.ReportingTo);
modelBuilder.Entity<EFEmployee>().HasRequired(t => t.Identity).WithOptional(u => u.Employee);
base.OnModelCreating(modelBuilder);
}
DbSet<TEntity> IBusinessEntityContext.Set<TEntity>()
{
return this.Set<TEntity>();
}
}
Based on these classes, what can I use to check that insert works? Do I need a unit of work class? And do I need a service layer? Everything's so confusing. I don't even understand what's the difference between a DbContext
and IDbSet<T>
, or what a DbContext
's .Set<T>
is about, I've just been following tutorials to create my repositories and stuff.