为延迟加载注入数据访问依赖项的正确方法是什么?

时间:2008-09-24 22:18:00

标签: inversion-of-control lazy-loading

在延迟加载时注入数据访问依赖项的正确方法是什么?

例如,我有以下类结构

class CustomerDao : ICustomerDao
  public Customer GetById(int id) {...}

class Transaction {
  int customer_id; //Transaction always knows this value
  Customer _customer = null;
  ICustomerDao _customer_dao;
  Customer GetCustomer() {
    if(_customer == null)
      _customer = _customer_dao.GetById(_customer_id);
    return _customer
  }

如何将_customer_dao的引用添加到事务对象中?如果我希望事务至少看起来像POCO,那么对构造函数的要求似乎就没有意义。是否可以让Transaction对象直接引用Inversion of Control Container?这也似乎很尴尬。

像NHibernate这样的框架如何处理这个?

3 个答案:

答案 0 :(得分:7)

我建议不同的东西...... 使用延迟加载类:

public class Lazy<T>
{
   T value;
   Func<T> loader;

   public Lazy(T value) { this.value = value; }
   public Lazy(Func<T> loader { this.loader = loader; }

   T Value
   {
     get 
    {
       if (loader != null)
       {
         value = loader();
         loader = null;
       }

       return value;
    }

    public static implicit operator T(Lazy<T> lazy)
    {
        return lazy.Value;
    }

    public static implicit operator Lazy<T>(T value)
    {
        return new Lazy<T>(value);
    }
}

一旦你得到它,你就不需要在你的对象中注入dao了:

public class Transaction
{
    private static readonly Lazy<Customer> customer;

    public Transaction(Lazy<Customer> customer)
    {
      this.customer = customer;
    }

    public Customer Customer
    {
       get { return customer; } // implicit cast happen here
    }
}

创建未绑定到数据库的Transcation对象时:

new Transaction(new Customer(..)) // implicite cast 
                                  //from Customer to Lazy<Customer>..

从存储库中的数据库重新生成事务时:

public Transaction GetTransaction(Guid id)
{
   custmerId = ... // find the customer id 
   return new Transaction(() => dao.GetCustomer(customerId));
}

发生了两件有趣的事: - 您的域对象可以使用或不使用数据访问,它变得无关数据访问。唯一的小麻烦就是能够传递给对象而不是对象本身的函数。 - Lazy类是内部可变的,但可以用作不可变值。 readonly关键字保持其语义,因为其内容不能在外部更改。

如果希望字段可写,只需删除readonly关键字即可。在分配新值时,由于隐式转换,将使用新值创建新的Lazy。

编辑: 我在这里写了博客:

http://www.thinkbeforecoding.com/post/2009/02/07/Lazy-load-and-persistence-ignorance

答案 1 :(得分:1)

我通常在构造函数中执行依赖注入,就像你上面所做的那样,但是只有在调用“get”时才采取进一步的延迟加载。不确定这是否是您正在寻找的纯方法,但它确实消除了“脏”构造函数DI / Lazy Loading in 1 step;)

public class Product
{
    private int mProductID;
    private Supplier mSupplier;
    private ISupplierService mSupplierService;

    public Product()
    {
      //if you want your object to remain POCO you can use dual constr
      //this constr will be for app use, the next will be for testing
    } 

    public Product(ISupplierService SupplierService)
    {
        mSupplierService = SupplierService;
    }

    public Supplier Supplier {
        get {
            if (mSupplier == null) {
                if (mSupplierService == null) {
                    mSupplierService = new SupplierService();
                }
                mSupplier = mSupplierService.GetSupplierByProductID(mProductID);
            }
            return mSupplier;
        }
        set { mSupplier = value; }
    }
}

答案 2 :(得分:1)

我对POCO这个术语并不十分熟悉,但我读过的定义似乎通常都遵循对象的精神,而不依赖于某个更大的框架。

也就是说,无论你如何分割它,如果你正在执行依赖注入,你将会与那些注入了功能的类进行协作,并且依赖于对象的东西,无论它是否为完整的注入框架或只是一些汇编类。

对于我来说,将IOC容器的引用注入类中似乎很奇怪。我更喜欢我的注入发生在构造函数中,代码看起来像这样:

public interface IDao<T>
{
    public T GetById(int id);
}


public interface ICustomerDao : IDao<Customer>
{
}

public class CustomerDao : ICustomerDao
{
    public Customer GetById(int id) 
    {...}
}

public class Transaction<T> where T : class
{

    int _id; //Transaction always knows this value
    T _dataObject;
    IDao<T> _dao;

    public Transaction(IDao<T> myDao, int id)
    {
        _id = id;
        _dao = myDao;
    }

    public T Get()
    {
        if (_dataObject == null)
            _dataObject = _dao.GetById(_id);
        return _dataObject;
    }
}