[英]Unit of work, generic repository and business layer
First sorry if this was asked already but i cannot find an answer for this 'particular case'. 首先对不起,如果已经有人问过这个问题,但我找不到这种“特殊情况”的答案。
I've a Interface of Unit of Work: 我有一个工作单位接口:
public interface IUnitOfWork
{
DbContext Context { get; set; }
void Dispose();
void Save();
}
And use a Generic Repository class: 并使用通用存储库类:
public class GenericRepository<TEntity> where TEntity : class
{
private DbSet<TEntity> dbSet;
private IUnitOfWork UnitOfWork { get; set; }
private DbContext context { get { return UnitOfWork.Context; } }
public GenericRepository(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
I don't want to do my logic in my MVC controler, so I added a businesslayer. 我不想在我的MVC控制器中执行逻辑,所以我添加了一个业务层。 My question is, where should I instantiate (and dispote) my IUnitOfWork, in my controler and pass it to my business layer?
我的问题是,我应该在我的控件中实例化(并分配)我的IUnitOfWork并将其传递给我的业务层吗? Example:
例:
public static class CircleLogic
{
public static void DeleteCircle(IUnitOfWork uow, int id)
{
try
{
var circleRep = new GenericRepository<Circle>(uow);
var circle = circleRep.GetByID(id);
......
circleRep.Delete(id);
uow.Save();
}
catch (Exception ex)
{
throw;
}
}
}
I've seen this but I don't want to instantiate it in my business layer. 我已经看到了这一点,但是我不想在我的业务层中实例化它。 What is the best approach?
最好的方法是什么?
Thanks! 谢谢!
I see no harm in passing it into your Business Layer like you have suggested. 我认为像您建议的那样将其传递到您的业务层中没有什么害处。 However, if you want to keep your Business Layer completely persistence ignorant I would suggest introducing an
IRepository<T>
interface and passing that in instead. 但是,如果您想让业务层完全不了解持久性,我建议您引入
IRepository<T>
接口并将其传入。
In terms of disposing of the objects, I would make both your IUnitOfWork
/Repository classes implement IDisposable
so you can make use of the using
statement eg 就对象的处置而言,我会让您的
IUnitOfWork
/ Repository类都实现IDisposable
以便您可以使用using
语句,例如
public ActionResult DeleteCircle(int id)
{
using (IUnitOfWork uow = new UnitOfWork())
{
using (IRepository<Circle> repo = new GenericRepository<Circle>(uow))
{
CircleLogic.DeleteCircle(repo, id);
}
uow.Save();
}
}
...
public static class CircleLogic
{
public static void DeleteCircle(IRepository<Circle> repo, int id)
{
var circle = repo.GetById(id);
...
repo.Delete(id);
}
}
Because your concrete UnitOfWork
implementation would most likely live in your persistence layer, it'd be sensible to instantiate it in either the persistence layer or 1 layer above in the business layer. 因为您的具体
UnitOfWork
实现很可能存在于您的持久性层中,所以在持久性层或业务层中的上一层进行实例化是明智的。 Your UI should have no knowledge of what technology you're using to persist your entities/data. 您的UI应该不知道您要使用哪种技术来持久化实体/数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.