SOLID SRP 和 FluentNhibernate 实体
这个类来自 http://wiki. Fluentnhibernate.org/Getting_started 它有一些逻辑,我认为这违反了 单一责任原则,您怎么看,您将如何解决这个问题?
另一件让我困扰的事情是为什么在 nhibernate 中总是使用 IList 而不是功能较少的 IEnumerable ?
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这并不违反SRP原则。而且,正如 Paco 提到的,它仍然是一个 POCO 类。 POCO 并不意味着对象应该只包含数据。
正如您所提到的,我会从 IList<> 更改为到 IEnumerable<>在我的收藏上并将设置器设为私有(对于收藏)。这对于 nhibernate 来说不是问题。在我看来,使用这些“添加”方法是处理模型上的集合的首选方法(有关该内容的博客文章)。
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).