with a publicly exposed collection any consumer can alter the
collection. plus it exposes the type of implementation. if you needed
to change the implementation (say a list to set or vice versa) that
could impact other areas of the system. if it were encapsulated it
would only happen in a single class. encapsulation would also solve
your null Municipality issue. you could encapsulate this logic into a
single place.

taking it one step father you could encapsulate the creation of a city
by declaring it within a municipality.

class Municipality
{
   AddNewCity(string name)
   {
        if(citys.Exists(c=>c.Name == name)) return;
        citys.Add(new City(name, this));
   }
}

class City
{
   pubic virtual string Name {get; private set;}
   pubic virtual Municipality Municipality {get; private set;}

   internal City(string name, Municipality municipality)
   {
        Name = name;
        Municipality = municipality
   }
}

it's a simple change but it has a big impact. you can begin to define
the rules directly into the domain objects. now if you want to create
a city.

using(var tx = session.BeginTransaction())
{
   try
   {
      session.Get<Municipality>(1L).AddNewCity("New York");
      tx.Commit();
   }
   catch
   {
      tx.Rollback();
      throw;
   }
}

yes, if you remove one of the relationships it becomes unidirectional.

On Nov 10, 4:24 pm, Kasper Nielsen <[email protected]> wrote:
> Thanks a lot for your answers Jason - quite a lot to take into
> consideration :-)
>
> But: Wouldn't it make sense to be able to create a new City and add it
> to a municipality like this:
>
> Municipality mun = Load(...);
> mun.Cities.Add(new City() { Name = "Cityname" });
> session.DoSomeSaveStuff();
>
> Or is it just me? I think the above would be quite intuitive.
>
> And it also makes me think: What if the relationship was only
> implemented on the Municipality class (that's an unidirectional
> mapping right?)

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" 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/nhusers?hl=en.

Reply via email to