我正在设置一个 asp.Net Mvc 4 应用程序并希望使用 Onion Architecture 配置它图案。 过去我曾这样使用工作单元模式
public class UnitOfWork : IUnitOfWork, IDisposable
{
private IRepository<CallModel> _callRepo;
private IRepository<UserModel> _userRepo;
public IRepository<CallModel> CallRepo
{
get
{
if (_callRepo == null)
{
_callRepo = new Repository<CallModel>();
}
return _callRepo;
}
}
public IRepository<UserModel> UserRepo
{
get
{
if (_userRepo == null)
{
_userRepo = new Repository<UserModel>();
}
return _userRepo;
}
}
}
然后我会将 UnitOfWork 类的实例传递给 Controller 来执行像这样的简单 CRUD 操作。
public class QuestionsController : Controller
{
private IUnitOfWork _unitOfWork;
[Inject]
public QuestionsController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
我已将该应用分成三个项目。
- 核心
- 基础设施
- 网络
我的接口(interface)都在核心项目中,IRepository 接口(interface)的实现在基础设施项目中。 如果我将 UnitOfWork 类放在核心项目中,那么由于它在基础设施项目中需要一个新的存储库,因此我正在创建从核心到基础设施的依赖关系。 如果我将它包含在基础架构中,那么 Web 项目(具有 Controller )将依赖于基础架构,并且整个解决方案最终看起来不像洋葱,而更像意大利面条。
最佳答案
I have my Interfaces all in the Core project and the implementation of the IRepository interface in the Infrastructure project. If I put the UnitOfWork Class in the Core Project then since it calls for a new Repository in the Infrastructure project I am creating a dependency from the Core to the Infrastructure.
嗯,不是真的。您的工作单元类应该依赖于 IRepository,而不是 Repository 实现本身。如果您正在使用依赖注入(inject),这应该不会造成问题,因为它应该找到正确的类型并在运行时提供它。我不确定在不使用 DI 的情况下洋葱架构是否可行。
另请参阅 david.s 的回答,因为这正是我的设置方式——有一个项目的唯一目的是连接依赖项。
关于asp.net-mvc - 如何在不引入依赖的情况下用 Onion Architecture 实现 UnitOfWork?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14858536/