samedi 7 février 2015

Explanation


The implementation of both interfaces goes for UnitOfWork and Repository<T>. Since repository is generic, I'm struggling whether to keep multiple refereces of Repository inside my UnitOfWork (for each entity), or a single generic method that returns the correct one (currently it is this way).


The issue is in my said method:



// In the controller, this method is called like this
// Student student = unitOfWork.Repository<Student>().GetByID(id);

// UnitOfWork.cs
public IRepository<TEntity> Repository<TEntity>() where TEntity : class {
return new Repository<TEntity>(dbcontext);
}

// MockUnitOfWork.cs
public IRepository<TEntity> Repository<TEntity>() where TEntity : class {
return new MockRepository<TEntity>();
}


The problem is MockRepository, as it works with a List. Since I'm returning a new instance, I can't keep track if the new inserted object is on the list (in case of POST CREATE operation).



// MockRepository.cs
public IList<TEntity> Entities { get; private set; }


Questions


Up to this point I see two possible reasons of this problem:




  1. I should not be testing if the created entity is on the list, since that's a mock. Which means MockRepository.Add() would have an empty body.




  2. I should keep a reference for every repository in my UnitOfWork instead of returning a new instance each time. Which means every time I add a new entity to my domain model, I have to update UnitOfWork code with a reference and initialization for the new entity.




Conclusion


If the issue is No. 1, how should I test it worked, beside asserting viewResult.ViewName == "Index" or similar?


Aucun commentaire:

Enregistrer un commentaire