What would be the most elegant way to use EF with Generic Repository, in Service Stack, and to write Unit Test for service?
At the moment, my core library looks like this for example:
Generic repository layer:
public class GenericRepository<TEntity> where TEntity : class
{
internal DbContext Context;
//...
//CRUD Operations, etc.
}
Unit of work layer:
public partial class UnitOfWork : IDisposable
{
private readonly DbContext _context = new MyDBContext(); //<- SMELLY PART (to me), I think this needs to be changed.
private bool _disposed;
private GenericRepository<User> _userRepository;
public GenericRepository<User> UserRepository
{
get { return _userRepository ?? (_userRepository = new GenericRepository<User>(_context)); }
}
//...
}
Business layer:
public class User
{
private static readonly UnitOfWork UoW = new UnitOfWork();
public static void AddUser(Models.User user)
{
//Map from domain model to entity model
Mapper.CreateMap<Models.User, DAL.Repository.User>();
var u = Mapper.Map<Models.User, DAL.Repository.User>(user);
UoW.UserRepository.Insert(u);
UoW.Save();
}
}
API project:
public class AppHost : AppHostBase
{
public AppHost() : base("Users Service", typeof(UsersService).Assembly) { }
//...
}
public class UsersService : Service
{
public object Post(User u)
{
AddUser(u);
//...
}
}
This will work anyway, but in order to have unit tests, and to isolate them from real DB data.
Do I need to add some kind of IDbConnectionFactory, and what would be a best way to do that?
I see that using "effort" is popular approach for that but I have never used it before, so I would like to hear all options before I start anything.
Thanks
Aucun commentaire:
Enregistrer un commentaire