﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Scott Muc commented on Who is the responsible party?</title><description>@Dave
  
  
I would have other validation logic in there. To create a bad example, what if a parent is not allowed to have 0 children? I would have a remove method on the Parent class because it's a business rule that says a Parent must have at least one child.
  
  
I understand what you're saying though, and I like your code sample. Just trying to bring up an alternative form.
</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment11</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment11</guid><pubDate>Wed, 20 May 2009 04:07:27 GMT</pubDate></item><item><title>Peter Morris commented on Who is the responsible party?</title><description>I wouldn't have thought this was a tell-don't-ask scenario.  I'd have said it was more a connascence of algorithm.
  
  
  
</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment10</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment10</guid><pubDate>Tue, 19 May 2009 09:15:07 GMT</pubDate></item><item><title>Ayende Rahien commented on Who is the responsible party?</title><description>Tariq,
  
That is call Command pattern, and it is fairly common way to deal with externalizing the logic while keeping it close. 
</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment9</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment9</guid><pubDate>Tue, 19 May 2009 07:47:08 GMT</pubDate></item><item><title>Dave commented on Who is the responsible party?</title><description>@Scott. Why create (and introduce) a new method? Are you also adding a Get and Delete method? If so, you're violating the S in the SOLID principle where a class should have only one (single) responsibility. 
  
  
I guess you already have a collection of children. So why not override the Add itself. 
  
  
public class ChildCollection : List
&lt;child  
{
  
  private Parent parent;
  
  public ChildCollection(Parent parent)
  
  {
  
    this.parent = parent;
  
  }
  
  
  public override void Add(Child item)
  
  {
  
     item.Parent = parent;
  
     base.Add(item);
  
  }
  
}
  
  
public class Parent
  
{ 
  
   private readonly ChildCollection childs;
  
   public ChildCollection Childs { get { return childs; } }
  
  
   public Parent()
  
   {
  
      childs = new ChildCollection(this);
  
   }
  
}
  
  
That prevents that methods in your class (or a derived one) can bypass AddChild and add a child directly (without setting the parent). It also means you can make Childs a public property. 
  
  
Just my 2 eurocents..
&gt;</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment8</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment8</guid><pubDate>Tue, 19 May 2009 07:11:52 GMT</pubDate></item><item><title>Scott Muc commented on Who is the responsible party?</title><description>What about the following?
  
  
parent.AddChild(child);
  
  
where::
  
  
public virtual void AddChild(Person child)
  
{
  
   child.Parent = this;
  
   this.children.Add(child);
  
}
</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment7</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment7</guid><pubDate>Tue, 19 May 2009 06:07:16 GMT</pubDate></item><item><title>Tariq commented on Who is the responsible party?</title><description>So then how does that explain for instance the reason for some people creating an ObjectTask class which would do some additional functionality related to the behaviour(e.g  CartTask.AddToCart(Cart cart, Item someItem) where CartTask would calculate and set shipping or log history or any additional tasks) shouldnt these be handled by the AddtoCart function?
</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment6</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment6</guid><pubDate>Mon, 18 May 2009 23:53:19 GMT</pubDate></item><item><title>Ayende Rahien commented on Who is the responsible party?</title><description>Bidi collections tend to cause a lot of problems in the long run
</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment5</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment5</guid><pubDate>Mon, 18 May 2009 19:32:23 GMT</pubDate></item><item><title>Jason Y commented on Who is the responsible party?</title><description>It is possible (though not necessarily easier) to place logic in collection classes and properties so that
  
  
parent.Children.Add(child);  // Children is a custom collection
  
child.Parent = parent;          // Parent is a property, not a field
  
  
does _not_ violate Tell, Don't Ask because the Parent and ChildCollection classes (and any classes derived from them) implement all the logic.  .NET does this repeatedly, I think.
</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment4</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment4</guid><pubDate>Mon, 18 May 2009 19:23:14 GMT</pubDate></item><item><title>Joe commented on Who is the responsible party?</title><description>This is one of the big reasons why I like NHiberate, the parent can easily hide the underlying children collection and dictate how you get at and set its children.
  
  
Most other ORMs that I have been exposed to (not naming names to avoid a flame war) makes the children collection public on the parent and does not offer a way of controlling access to the collection.
</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment3</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment3</guid><pubDate>Mon, 18 May 2009 18:20:50 GMT</pubDate></item><item><title>Ayende Rahien commented on Who is the responsible party?</title><description>Scott,
  
The question is, who is responsible for doing so
</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment2</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment2</guid><pubDate>Mon, 18 May 2009 18:06:42 GMT</pubDate></item><item><title>Scott White commented on Who is the responsible party?</title><description>There are times you have to set a children's parent, such as when using a foreign generator in NHib with 1 to 1 relationships and you want to propagate save-update.
</description><link>http://ayende.com/4021/who-is-the-responsible-party#comment1</link><guid>http://ayende.com/4021/who-is-the-responsible-party#comment1</guid><pubDate>Mon, 18 May 2009 17:35:20 GMT</pubDate></item></channel></rss>