I am building a web app and am trying to follow the "aggregate" and "repository" design principle from Eric Evan's Domain Driven Design book. I am also using NHibernate for persistence.
I am finding myself having to create repositories for every kind of object in my system regardless of whether it is an aggregate root or not. Mainly this is because of the web application strategy I am used to using, where you basically supply an ID of the item you are working with, and then performing the operation based on that ID. For example...
Code:
editaddress.rails?id=123, deleteaddress.rails?id=123, etc.
So, even if Address is a part of the larger Customer aggregate, I end up needing repository methods like this to work with addresses...
Code:
Address address = AddressRepository.Load(id);
AddressRepository.Delete(address);
I would like to follow the aggregate/repository principles more closely, but I am having some challenges. One approach I am looking at is changing my design a bit to allow for the following...
Code:
deleteaddress.rails?customerid=222&addressid=123
And in my page, doing something more along the lines of...
Code:
Customer customer = CustomerRepository.Load(customerid);
customer.RemoveAddress(addressid);
CustomerRepository.Save(customer);
This seems better. I do not have to have repository methods for addresses. But, now I run into another issue. How to handle the removal of the address based on the id. In my Customer.RemoveAddress(int id) method, I end up with something like...
Code:
foreach (Address address in this.Addresses)
if (address.ID == id)
this.Addresses.Remove(address);
This does not seem like the best approach (with the looping through each address). I have gotten into a habit of using IList<Entity> collections to store my associated collections of objects, such as Addresses. Is there a better approach to allow a more "clean" and "explicit" approach to working with the addresses in my exampe?
Thanks for any suggestions!