我即将学习IoC和依赖注入。而且我想知道我是否理解这个模式是正确的。
我尝试实现UnitOfWork / Repository Pattern。我想要两个用于单元测试的存储库类的实现,我希望UnitOfWork“决定”实例化哪个具体实现(使用Unity的帮助)。
IUserRepository接口
public interface IUserRepository
{
List<User> getAll();
}
使用实际数据的存储库实施
public class UserRepository : IUserRepository
{
private MyDbContext db;
public UserRepository(MyDbContext db)
{
this.db = db;
}
public List<DomainModel.User> getAll()
{
return db.Users.ToList();
}
}
FakeRepository实施
public class FakeUserRepository : IUserRepository
{
private List<User> userSet;
public FakeUserRepository()
{
// Fake Data
userSet = new List<User>();
userSet.Add(new User { Username = "john", Active = true, EMail = "john@ann.net", Password = "supersecret" });
userSet.Add(new User { Username = "ashley", Active = true, EMail = "ashley@ann.net", Password = "supersecret" });
userSet.Add(new User { Username = "kaidan", Active = true, EMail = "kaidan@ann.net", Password = "supersecret" });
userSet.Add(new User { Username = "tali", Active = true, EMail = "tali@ann.net", Password = "supersecret" });
}
public List<DomainModel.User> getAll()
{
return userSet;
}
}
使用Unity进行我的UnitOfWork实施
// To Keep it simple, i skipped the IDisposable part ;)
public class UnitOfWork
{
MyDbContext db;
private IUserRepository userRepository;
UnityContainer container = new UnityContainer();
public UnitOfWork(bool fake = false)
{
if (fake)
{
container.RegisterType<IUserRepository, FakeUserRepository>();
}
else
{
db = = new MyDbContext();
container.RegisterType<IUserRepository, UserRepository>(new InjectionConstructor(db));
}
}
public IUserRepository UserRepository
{
get
{
if (userRepository == null)
{
userRepository = container.Resolve<IUserRepository>();
}
return userRepository;
}
}
public void Save()
{
db.SaveChanges();
}
}
现在当我打电话给new UnitOfWork()
时,它会给我“UnitOfWork with RealData”实现。如果我打电话给new UnitOfWork(fake: true)
,它会给我“UnitOfWork with Fake Data”。到现在为止还挺好。但这是Unity和DI应该使用的方式吗?如果我的应用程序增长到说30个存储库,我最终定义了大的“If / else”块,对吧?想象一下,想要添加更多数据存储,如XML或WCF作为数据源。如果我继续像上面那样使用它,我最终会得到一个非常复杂且非常复杂的UnitOfWork类。
首先:我不确定我是否理解DI和Unity的使用方法。如果我理解正确的话:使用能给我正确类型的UnitOfWork的工厂会更好吗?
非常欢迎任何帮助或提示。
谢谢,
Matze
答案 0 :(得分:1)
我会像使用存储库那样分离工作单元:IUnitOfWork接口和伪造和实体框架工作单元的具体类。您现在的工作单元违反了Single Responsibility Principle,因为它有多重责任:
如果您有一个单独的工作单元实体框架,您将不需要容器来解析存储库,但您可以使它们成为您在构造函数中初始化的成员变量。您只需在容器上注册正确的工作单元。