[英]How to separate Service layer with Repository
I want to improve the architecture of my ASP.NET MVC application. 我想改善ASP.NET MVC应用程序的体系结构。 In my controller i use service layer with ViewModel. 在我的控制器中,我将服务层与ViewModel一起使用。 Example Service Layer: 服务层示例:
public interface ICashRegisterManager
{
void CreateCashRegister(CashRegisterTransactionModel model, int? programId);
}
Example Controller: 控制器示例:
public class CashRegisterTransactionController : PersonContextController<CashRegisterTransactionModel, CashRegisterTransactionFilter>
{
public CashRegisterTransactionController(IPersonContextProvider personContextProvider, ICashRegisterManager cashRegisterManager)
: base(personContextProvider)
{
ExceptionUtil.NotNull(cashRegisterManager, "cashRegisterManager");
this.cashRegisterManager = cashRegisterManager;
}
public override ActionResult Create(DataSourceRequest request, CashRegisterTransactionModel contract)
{
cashRegisterManager.CreateCashRegister(contract, contract.ProgramId);
return base.Create(request, contract);
}
But in Service layer i should to create instance of IRepository and every time map TContract into TEntity. 但是在服务层中,我应该创建IRepository的实例,并且每次将TContract映射到TEntity中。
My idea is in Service layer to use intermediate class , how make this. 我的想法是在Service层中使用中间类,如何做到这一点。
Example: 例:
public class CashRegisterManager : ICashRegisterManager
{
public void CreateCashRegister(CashRegisterTransactionModel model, int? programId)
{
var persister = Persistence.GetPersister<CashRegisterTransactionModel>();
persister.Add(model);
}
}
public interface IPersister<TContrct>
{
void Add(TContrct model);
}
I don't know how to implement the Add method, which to use IRepository. 我不知道如何实现使用IRepository的Add方法。 I guess that should comply with the naming conventions of TContract and TEnitity(CashRegisterTransactionModel/CashRegisterTransaction) and how can I return a instance of IPersister. 我猜应该符合TContract和TEnitity(CashRegisterTransactionModel / CashRegisterTransaction)的命名约定,以及如何返回IPersister实例。 I apologize for my English. 我为我的英语道歉。 Thank you for your time!!! 感谢您的时间!!!
Personally I think creating your own generic repositories is a waste of time. 我个人认为创建自己的通用存储库是浪费时间。 however, perhaps you could try something like this : 但是,也许您可以尝试这样的事情:
public interface IPersister<T>
{
void Add(T model);
}
public class Persister<T> : IPersister<T>
{
protected List<T> data;
public Persister()
{
this.data = new List<T>();
}
public void Add(T model)
{
this.data.Add(model);
}
}
This is simple implementation for IPersister : 这是IPersister的简单实现:
public class Persister<TContract, TEntity> : IPersister<TContract, TEntity> where TEntity : class,IDeletableEntity
{
public Persister(IDeletableEntityRepository<TEntity> repository)
{
this.repository = repository;
}
public void Add(TContract model)
{
var entity = Mapper.Map<TContract, TEntity>(model);
repository.Add(entity);
repository.SaveChanges();
}
public IQueryable<TContract> All<TContract>()
{
return repository.All().Where(x => !x.IsDeleted).Project().To<TContract>();
}
#region Private Members
private IDeletableEntityRepository<TEntity> repository;
#endregion
}
But I don't want to use TEnitity when creating IPersister, because the mvc project doesn't have a reference to Database Entities. 但是我不想在创建IPersister时使用TEnitity,因为mvc项目没有对数据库实体的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.