SOLID SRP 和 FluentNhibernate 实体

发布于 2024-08-18 04:29:55 字数 1024 浏览 5 评论 0原文

这个类来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妥活 2024-08-25 04:29:55

我认为这并不违反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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文