Hello,
I am trying to build the following relationship (this is a
simplification but the idea is the same) and I don't understand
a) why castle does what it does and
b) how to get it to resolve all dependencies correctly

So the relationship is simple.  A Customer can have many products
which it should be able to lazy-load so it needs a reference to a
product data-access object and a Product needs to be able to lazy-load
its customer so it needs a dependency on the customer data access
object.  Obviously these can't be constructor dependencies but they
can be property dependencies with setters that Windsor should be able
to fill out.  I include below the full text of my sample program.

So the problem that I am facing is that when I resolve ProductDao
first and CustomerDao second, ProductDao's dependency on the latter is
filled out but CustomerDao's dependency on ProductDao is set to null.
If the order of resolution is reversed then so are the effects.
On the surface that almost makes sense, but thinking through what I
imagine castle to be doing I'm not so sure.
1) Suppose example I call container.Resolve<ProductDao>()
2) First thing that should happen is that its constructor should run
and complete, at this point it is instantiated.
3) Then it should attempt to fill the properties which would require
it to resolve CustomerDao
4) Which has an empty constructor and should get instantiated with no
problem
5) Castle should then attempt to fill out its property dependency on
CustomerDao which is already instantiated so it shouldn't be a problem
In my experiments, Windsor bails out on step 5 and my first question
is what is the logic behind that?

My second question is of course is there any simple way to resolve
this without peppering my code with checks if a property is
instantiated and calls to the container if it is not?

Thanks a lot,
George Mauer

Text of my sample program follows:

namespace CastleProperties {
    class Program {
        static void Main(string[] args) {
            IWindsorContainer wc = new WindsorContainer();
            wc.AddComponent<ICustomerDao, CustomerDao>();
            wc.AddComponent<IProductDao, ProductDao>();
            var pd = wc.Resolve<IProductDao>();
            var cd = wc.Resolve<ICustomerDao>();
            return;
        }
    }

    public class Product {
        private ICustomerDao _customerDao;
        private Customer _customer;
        public Product(ICustomerDao customerDao) { _customerDao =
customerDao; }
        public int ProductId { get; set; }
        public int CustomerId { get; set; }
        public Customer Customer {
            get {
                if (_customer == null) _customer =
_customerDao.GetById(CustomerId);
                return _customer;
            }
        }
    }
    public class Customer {
        private IProductDao _productDao;
        private Product[] _products;
        public Customer(IProductDao productDao) { _productDao =
productDao; }
        public int CustomerId { get; set; }
        public Product[] Products {
            get {
                if (_products == null) _products =
_productDao.GetAllForCustomer(this);
                return _products;
            }
        }
    }

    public interface ICustomerDao {
        Customer GetById(int id);
    }
    public interface IProductDao {
        Product[] GetAllForCustomer(Customer customer);
    }

    public class CustomerDao : ICustomerDao {
        private IProductDao _productDao;
        public IProductDao ProductDao {
            get { return _productDao; }
            set { _productDao = value; }
        }
        public Customer GetById(int id) {
            Customer c = new Customer(_productDao);
            // Query the database and fill out CustomerId
            return c;
        }
    }
    public class ProductDao : IProductDao {
        private ICustomerDao _customerDao;
        public ICustomerDao CustomerDao {
            get { return _customerDao; }
            set { _customerDao = value; }
        }
        public Product[] GetAllForCustomer(Customer customer) {
            return null;
        }

    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to