Repository Pattern中UnitOfWork的正确使用方式
The correct way of using UnitOfWork in Repository Pattern
我正在尝试按照 MSDN 上有关使用工作单元和存储库模式的教程进行操作,但我无意中发现了以下内容:
private UnitOfWork unitOfWork = new UnitOfWork();
"This code adds a class variable for the UnitOfWork class. (If you were using interfaces here, you wouldn't initialize the variable here; instead, you'd implement a pattern of two constructors"
基本上,我需要在我的 LogController 中调用我的 UnitOfWork class 而不是实际使用上面的代码,因为我正在使用一个接口?这怎么可能?我不确定 'two constructors'.
是什么意思
如有任何建议,我们将不胜感激
在你的class中你定义
Private IUnitOfWork _unitOfWork = null;
并且您有一个接受 IUnitOfWork 的构造函数,因此调用者可以传入一个实现:
Public MyClass (IUnitOfWork unitOfWork) {
_unitOfWork = unitOfWork;
}
你还有另一个没有,但知道如何找到要创建的实现。它可以使用默认实现,也可以转到您编写的某个配置文件来定义要使用的类型等。
在 MyClass 中你仍然调用 _unitOfWork.whateverMethod()
我正在尝试按照 MSDN 上有关使用工作单元和存储库模式的教程进行操作,但我无意中发现了以下内容:
private UnitOfWork unitOfWork = new UnitOfWork();
"This code adds a class variable for the UnitOfWork class. (If you were using interfaces here, you wouldn't initialize the variable here; instead, you'd implement a pattern of two constructors"
基本上,我需要在我的 LogController 中调用我的 UnitOfWork class 而不是实际使用上面的代码,因为我正在使用一个接口?这怎么可能?我不确定 'two constructors'.
是什么意思如有任何建议,我们将不胜感激
在你的class中你定义
Private IUnitOfWork _unitOfWork = null;
并且您有一个接受 IUnitOfWork 的构造函数,因此调用者可以传入一个实现:
Public MyClass (IUnitOfWork unitOfWork) {
_unitOfWork = unitOfWork;
}
你还有另一个没有,但知道如何找到要创建的实现。它可以使用默认实现,也可以转到您编写的某个配置文件来定义要使用的类型等。
在 MyClass 中你仍然调用 _unitOfWork.whateverMethod()