NHibernate 单向关联

发布于 2024-12-02 23:53:16 字数 1923 浏览 2 评论 0原文

尝试使用 Fluent NHibernate 的入门 项目。出于一些原因,我尝试对示例进行一些自定义,其中包括消除 json 序列化的循环引用。

我所做的是将其引用的 StoreStoreMap 类剥离回 EmployeeProduct 类。现在看起来像这样:

Store/StoreMap

public class Store
{
   public virtual int Id { get; private set; }
   public virtual string Name { get; set; }
}
public StoreMap()
{
   Id(x => x.Id);
   Map(x => x.Name);
}

Employee/EmployeeMap

public class Employee
{
  public virtual int Id { get; private set; }
  public virtual string FirstName { get; set; }
  public virtual string LastName { get; set; }
  public virtual Store Store { get; set; }
}

public class EmployeeMap : ClassMap<Employee>
{
  public EmployeeMap()
  {
    Id(x => x.Id);
    Map(x => x.FirstName);
    Map(x => x.LastName);
    References(x => x.Store).Cascade.All();
  }
}

Product/ProductMap

public class Product
{
  public virtual int Id { get; private set; }
  public virtual string Name { get; set; }
  public virtual double Price { get; set; }
  public virtual IList<Store> StoresStockedIn { get; private set; }

  public Product()
  {
    StoresStockedIn = new List<Store>();
  }

  public virtual void StockAt(Store store)
  {
    StoresStockedIn.Add(store);
  }
}

public class ProductMap : ClassMap<Product>
{
  public ProductMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    Map(x => x.Price);
    HasManyToMany(x => x.StoresStockedIn)
      .Cascade.All()
      .Table("StoreProduct");
  }
}

我已将“Cascade”操作移至 Product 中Employee 而不是 Store。从我所做的测试来看,HasMany 和 HasManyToMany 关联似乎都工作正常。

我的问题是这是否是正确的方法。是否会导致我没有预料到的事情。

Playing around with Fluent NHibernate's Getting Started project. I tried to customize the example a bit, for a few reasons, among them elimination of circular reference for json serialization.

What I have done is to strip the Store and StoreMap classes of it's references back to Employee and Product classes. It now looks like this:

Store/StoreMap

public class Store
{
   public virtual int Id { get; private set; }
   public virtual string Name { get; set; }
}
public StoreMap()
{
   Id(x => x.Id);
   Map(x => x.Name);
}

Employee/EmployeeMap

public class Employee
{
  public virtual int Id { get; private set; }
  public virtual string FirstName { get; set; }
  public virtual string LastName { get; set; }
  public virtual Store Store { get; set; }
}

public class EmployeeMap : ClassMap<Employee>
{
  public EmployeeMap()
  {
    Id(x => x.Id);
    Map(x => x.FirstName);
    Map(x => x.LastName);
    References(x => x.Store).Cascade.All();
  }
}

Product/ProductMap

public class Product
{
  public virtual int Id { get; private set; }
  public virtual string Name { get; set; }
  public virtual double Price { get; set; }
  public virtual IList<Store> StoresStockedIn { get; private set; }

  public Product()
  {
    StoresStockedIn = new List<Store>();
  }

  public virtual void StockAt(Store store)
  {
    StoresStockedIn.Add(store);
  }
}

public class ProductMap : ClassMap<Product>
{
  public ProductMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    Map(x => x.Price);
    HasManyToMany(x => x.StoresStockedIn)
      .Cascade.All()
      .Table("StoreProduct");
  }
}

I've moved "Cascade" operations into the Product and Employee instead of Store. From the testing I've done, both HasMany and HasManyToMany associations seem to be working okay.

My question is if it's the right approach. Whether or not it will lead to something that I have not anticipated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

长梦不多时 2024-12-09 23:53:16

据我所知,在模型类中拥有关系的两端是 nHibernate 的标准做法(实际上,从业务角度来看这通常是有意义的)。
尽管您不必这样做(我认为您的示例应该可以正常工作)。
如果您不想暴露一端(或两端),您可以随时将它们定义为私有或受保护。

关于 json 序列化的 ps:不建议序列化模型实体。这有几个原因,最重要的是:
1.您向用户显示的数据可能与您在实体中拥有的数据不同(例如,您可能想向员工提供他们的姓名、ID、商店名称和他们拥有的产品数量。这将很难使用您的模型类来做到这一点)。
2.你的对象中很可能有未初始化的集合(如果你使用延迟加载)。这些没有被序列化。

对于序列化,DTO 是推荐的方法。

as far as I know, it's standard practice in nHibernate to have both ends of a relationship in your model classes (actually, it usually makes sense from the business point of view).
Although you don't have to do it (your example, I think, should work fine).
If you don't want to expose one (or both) ends you can always define them as private or protected.

p.s concerning json serialization: it's not recommended to serialize your model entities. this is for a few reasons, the top ones being:
1. the data you display to the user may be different from what you have in your entities (for example- you might want to present an employee with their name, Id, their store name and number of products they own. it would be hard to do that using your model classes).
2. it's quite possible that you'd have uninitialized collections in your objects (if you use lazy-loading). These do not get serialized.

For serialization, DTOs is the recommended approach.

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