Do I must expose the aggregate children as public properties to implement the Persistence ignorance?

Posted by xuehua on Stack Overflow See other posts from Stack Overflow or by xuehua
Published on 2010-11-26T15:00:48Z Indexed on 2011/03/13 0:10 UTC
Read the original article Hit count: 160

Filed under:

Hi all,

I'm very glad that i found this website recently, I've learned a lot from here.
I'm from China, and my English is not so good. But i will try to express myself what i want to say.

Recently, I've started learning about Domain Driven Design, and I'm very interested about it. And I plan to develop a Forum website using DDD.

After reading lots of threads from here, I understood that persistence ignorance is a good practice.

Currently, I have two questions about what I'm thinking for a long time.

  1. Should the domain object interact with repository to get/save data?
  2. If the domain object doesn't use repository, then how does the Infrastructure layer (like unit of work) know which domain object is new/modified/removed?

For the second question. There's an example code: Suppose i have a user class:

public class User
{
 public Guid Id { get; set; }
 public string UserName { get; set; }
 public string NickName { get; set; }

 /// <summary>
 /// A Roles collection which represents the current user's owned roles.
 /// But here i don't want to use the public property to expose it.
 /// Instead, i use the below methods to implement.
 /// </summary>
 //public IList<Role> Roles { get; set; }

 private List<Role> roles = new List<Role>();
 public IList<Role> GetRoles()
 {
    return roles;
 }
 public void AddRole(Role role)
 {
    roles.Add(role);
 }
 public void RemoveRole(Role role)
 {
    roles.Remove(role);
 }
}

Based on the above User class, suppose i get an user from the IUserRepository, and add an Role for it.

IUserRepository userRepository;
User user = userRepository.Get(Guid.NewGuid());
user.AddRole(new Role() { Name = "Administrator" });

In this case, i don't know how does the repository or unit of work can know that user has a new role?

I think, a real persistence ignorance ORM framework should support POCO, and any changes occurs on the POCO itself, the persistence framework should know automatically. Even if change the object status through the method(AddRole, RemoveRole) like the above example.

I know a lot of ORM can automatically persistent the changes if i use the Roles property, but sometimes i don't like this way because of the performance reason.

Could anyone give me some ideas for this? Thanks.

This is my first question on this site. I hope my English can be understood.
Any answers will be very appreciated.

© Stack Overflow or respective owner

Related posts about domain-driven-design