在 DDD 中,通过引用具体类,您在这里错过了更大的图景。通过最佳实践,您没有在存储库和“服务层”之间进行交互。如果您必须将 DataContext 注入存储库,我建议重构为:
public interface IRepository
{
IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
private IDataContext _dataContext;
public Repository(IDataContext dataContext)
{
_dataContext = dataContext;
}
public IList<Orders> GetNewOrders()
{
// perform your actions on _dataContext here
}
}
更好的解决方案是让 Repository 自己处理 DataContext - 通过掩盖底层要求来保持 Concert 的分离有效:
public interface IRepository
{
IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
private IDataContext _dataContext;
public Repository(String connectionString)
{
_dataContext = new DataContext(connectionString);
}
public IList<Orders> GetNewOrders()
{
// perform your actions on _dataContext here
}
}
如果您必须自己控制 DataContext(或其他类)(也许您想保留一个静态引用,或者基于 WebRequest 更改设置等),您将需要使用“工厂”。
工厂看起来像这样:
public static class DataContextFactory
{
public static IDataContext GetInstance()
{
// return either a static instance,
// or threaded instance, a GlobalContext instance
// or whatever your preference is here
//
}
}
这样,您就可以完全控制如何在“服务”层之外和之外控制 DataContext 的实例。因此,您可以像下面这样使用这个 DataContextFactory:
public interface IRepository
{
IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
public IList<Orders> GetNewOrders()
{
using (var dataContext = DataContextFactory.GetInstance())
{
// dataContext is now your IDataContext to work with
}
}
}
“如何访问 IRepository?” 你可能会问?
您的服务层将执行以下操作:
public void GetNewOrdersForServices()
{
// Not recommended!
// IRepository repo = new Repository()
//
// The following is recommended instead; because, it removes the
// the Concret reference from your Services layer completely!
//
IRepository repo = ServiceLocator.InstanceOf<IRepository>();
IList myList = repo.GetNewOrders();
}
或者,您可以使用您最喜欢的 Inversion of Control 容器将其注入到服务的构造函数中,如下所示:
public class OrderService
{
private IRepository _repo;
public OrderService(IRepository repo)
{
_repo = repo;
}
public void GetNewOrdersForServices()
{
IList myList = _repo.GetNewOrders();
}
如果您不熟悉服务定位器的概念,请查看 Castle Windsor,因为它几乎囊括了您的所有需求。