NHibernate映射

发布于 2024-10-10 00:58:11 字数 449 浏览 0 评论 0原文

我正在为我未来的项目评估一些 ORM。我不喜欢使用 EF 方法来实现我的特定目标,因为我已经拥有想要持久化的业务对象,并且我想要定位至少 1 个除 SQL Server 之外的 DBMS。我对 Telerik 的 OpenAccess ORM 做了一些研究。它看起来非常强大,但学习起来也非常复杂,并且文档有些过时,因为它们的示例不适用于最新版本。现在我想尝试一下NHibernate。它看起来很棒,但据我所知,它没有生产就绪的 LINQ 提供程序,这对我来说是一个巨大的缺点。但我想没有它我也能活下去。

现在关于这个问题。我的项目中有多个程序集,每个程序集都包含不同的业务对象。我想制作另一个类似 DAL 的程序集,它将处理拆分在不同程序集中的所有业务对象的所有 CRUD 操作。我知道 NHibernate 需要 xml 映射,但在我的情况下,我应该在哪个程序集中嵌入映射 xml 文件、在 DAL 程序集中还是在每个包含业务对象的程序集中?

谢谢

I'm evaluating some ORMs for my future project. I don't like EF approach for my particular goal, because I already have business objects which I want to make persistent and I want to target at least 1 DBMS except SQL Server. I did some research on telerik's OpenAccess ORM. it looks very powerful, but also very complex to learn, and there documentation is somewhat outdated as their examples doesn't work for the latest version. Now I'm thinking to try NHibernate. It looks great, but as far as I know, it doesn't have a production ready LINQ provider, which is a huge drawback for me. But I think I can live without it.

Now about the question. I have multiple assemblies in my project, each of them containing different business objects. I want to make another DAL like assembly which will handle all CRUD operation for all of the business objects which are split in different assemblies. I know that NHibernate needs xml mappings, but in my situation, in which assembly(ies) should I embed the mapping xml files, in DAL assembly or in every assembly containing business object?

Thanks

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

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

发布评论

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

评论(3

零崎曲识 2024-10-17 00:58:12

另一方面,我想添加一些您可能会觉得方便的东西。 nHibernate 确实保留了类的 xml 映射,但由于我不是这个想法的忠实粉丝,所以我决定寻找一些允许我在代码上定义映射的东西,答案是一个名为 流畅的 nHibernate

通过此项目,您可以将映射定义为类文件,并且所有内容都保留为 C# 代码,因此首先消除了嵌入 xml 文件的问题。

你可以查找Fluent nHibernate的文档,但基本思想是这样的:

public class CatMap : ClassMap<Cat>
{
  public CatMap()
  {
    Id(x => x.Id);
    Map(x => x.Name)
      .Length(16)
      .Not.Nullable();
    Map(x => x.Sex);
    References(x => x.Mate);
    HasMany(x => x.Kittens);
  }
}

我希望我能有所帮助。

In another order of things, I want to add something which you might find handy. nHibernate does keep xml mappings for the classes, but since I'm not a huge fan of the idea, I decided to look around for something that allowed me to define the mappings on the code, the answer was a simple project called Fluent nHibernate.

With this project, you can define your mappings a class file and everything stays as C# code, ergo removing the issue with embeding the xml files in the first place.

You can look for the documentation of Fluent nHibernate, but the basic idea would be like this:

public class CatMap : ClassMap<Cat>
{
  public CatMap()
  {
    Id(x => x.Id);
    Map(x => x.Name)
      .Length(16)
      .Not.Nullable();
    Map(x => x.Sex);
    References(x => x.Mate);
    HasMany(x => x.Kittens);
  }
}

I hope I can be of assistance.

誰ツ都不明白 2024-10-17 00:58:12

NHibernate 3.0 有一个新的查询 API,称为“Query Over”,它非常 LINQ 风格。

映射文件应放置在包含业务逻辑的程序集中。

NHibernate 3.0 has a new query-API which is called 'Query Over', and it is very LINQ-isch.

The mapping files should be placed in the assembly that contains the business logic.

○愚か者の日 2024-10-17 00:58:12

除了 David 提到的 FluentNHibernate 之外,我还想指出 NH3 确实有自己的 LINQ 提供程序,该提供程序现在对于大多数项目来说已经足够成熟。

我在当前的项目中使用 FluentNHibernate 和 LINQ,并且对此非常满意。您可以在 http 下载适用于 NH3 的最新 FluentNHibernate 版本:// Fluentnhibernate.org/dls/v1.x/ Fluentnhibernate-NH3.0-binary-1.2.0.694.zip

In addition to David's mention of FluentNHibernate, I want to point out that NH3 does have it's own LINQ provider, which is now mature enough for most projects.

I use FluentNHibernate and LINQ in my current project and am quite happy with it. You can download the latest FluentNHibernate build for NH3 at http://fluentnhibernate.org/dls/v1.x/fluentnhibernate-NH3.0-binary-1.2.0.694.zip

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