views:

72

answers:

1

this class is from http://wiki.fluentnhibernate.org/Getting_started it has some logic in it and I think this violates the Single Responsibility Principle, how do you think, how would you resolve this ?

Another thing that bothers me is why in nhibernate always it is being used IList and not IEnumerable which has less functionality ?

public class Store
{
  public virtual int Id { get; private set; }
  public virtual string Name { get; set; }
  public virtual IList<Product> Products { get; set; }
  public virtual IList<Employee> Staff { get; set; }

  public Store()
  {
    Products = new List<Product>();
    Staff = new List<Employee>();
  }

  public virtual void AddProduct(Product product)
  {
    product.StoresStockedIn.Add(this);
    Products.Add(product);
  }

  public virtual void AddEmployee(Employee employee)
  {
    employee.Store = this;
    Staff.Add(employee);
  }
}
+1  A: 

In my opinion this does not violate the SRP principle. And, as Paco mentioned, it is still a POCO class. POCO does not mean that the object should only contain data.

I would, as you mention, however change from IList<> to IEnumerable<> on my collections and make the setters private (for the collections). That is not a problem for nhibernate to handle. Using those "add" methods is in my opinion the preferred way of handling the collections on your model (blog post about that).

Mattias Jakobsson
the only problem is that with IEnumerable you won't be able to easily add new element to the collection in that AddProduct method, I think you would have to use linq concat extension or something
Omu
That isn't a problem. You just have the private collection as List<> or something with a add method and the public as IEnumerable<> (described in the blog post I mentioned).
Mattias Jakobsson