Fluent NHibernate 还是 NHibernate for Linq?

发布于 2024-09-12 09:25:59 字数 56 浏览 2 评论 0 原文

Fluent NHibernate 或 NHibernate,我们应该选择哪一个来支持 linq?

Fluent NHibernate or NHibernate, Which one should we prefer for linq support?

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

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

发布评论

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

评论(4

↙厌世 2024-09-19 09:25:59

Fluent NHibernate 用于配置,Linq 用于查询。它们做不同的事情,您可以同时使用它们。 Fluent NHibernate 并不是 NHibernate 或 Linq 的替代品,而只是一个帮助程序库,可帮助您在代码中配置 NHibernate,而不是使用 XML 文件。

Fluent NHibernate is from Configuration and Linq is for querying. They do different things and you can use both at the same time. Fluent NHibernate is not a replacement for NHibernate or Linq but merely a helper library that helps you configure NHibernate in code rather than using XML files.

走走停停 2024-09-19 09:25:59

当您使用 NHibernate 时,您可以为对象创建映射以及对这些对象进行 CRUD 的查询 (LINQ)。 NHibernate 使用 XML 映射文件。 Fluent NHibernate 只是根据约定或任何其他可能的方式为您生成这些映射文件。因此,是否使用 FNHib 并不重要。您仍然可以使用 Linq、Hql、Criteria 或 QueryOver 查询对象,并且仍然可以使用 NHibernate。

When you use NHibernate you create mappings for your objects and the queries (LINQ) to CRUD those objects. NHibernate uses XML mapping files. Fluent NHibernate simply generates those mapping files for you based on conventions or any other of the possible ways. So, it does not matter if you use FNHib or not. You can still query your objects with Linq, Hql, Criteria or QueryOver and you are still goin to use NHibernate.

微凉 2024-09-19 09:25:59

NHibernate - 在 XML 中编写配置

Fluent NHibernate - 在 C#VB.NET

如果您希望类型安全并且讨厌在 XML 中编写配置,那么您应该选择 Fluent NHibernate。因此,您可以轻松地重命名和重构任何更改。您只需输入点,intellisense 将为您提供很多文档帮助。编写第一个配置只是需要时间,但一旦你知道它就非常容易了。

Fluent NHibernate 中的 Linq 与 NHibernate 本身是一样的。因为像其他答案一样,Fluent NHibernate 只是用于配置并方便使用 NHibernate,其余部分来自 NHibernate。例如:

        var maleCustomers = (from t in Session.Query<Entities.Customer>()
                   where t.Gender == Gender.Male
                   select t).ToList();

我使用 Fluent NHibernate,但是当您右键单击 .Query<>“转到定义”时,您发现它来自命名空间 NHibernate.Linq。所以无论哪种方式实际上都是使用相同的 Linq。

如果您说 Linq for NHibernate 与内置 Linq for Sql 是不同的。我不太确定其中的区别。我认为它是一样的,但是当我尝试以更复杂的方式查询时,它会抛出异常或只是一个空的 Sql 查询。

结论:无论选择哪种方式,Linq 都是一样的,都是NHibernate Linq

NHibernate - write configuration in XML

Fluent NHibernate - write configuration in C# or VB.NET with type safe

You should choose Fluent NHibernate if you want it type safe and hate to write configuration in XML. So any changes you can easily rename and refactor. You just type dot and intellisense will help you a lots with the documentation. It just takes time to write first config but once you know it is very easy.

Linq in Fluent NHibernate is just the same with NHibernate itself. because like other answer, Fluent NHibernate is just for config and make ease to use NHibernate, the rest is come from NHibernate. for example:

        var maleCustomers = (from t in Session.Query<Entities.Customer>()
                   where t.Gender == Gender.Male
                   select t).ToList();

I use Fluent NHibernate but when you right click on the .Query<> "Go To Definition" you found it come from namespace NHibernate.Linq. So either way is actually you use the same Linq.

Just if you said Linq for NHibernate vs built-in Linq for Sql is different. I am not so sure the difference. I thought it just the same but when I try to query in more complex way, it will throw exception or simply an empty Sql query.

Conclusion: either way you choose for Linq is just the same, it is NHibernate Linq.

夏日浅笑〃 2024-09-19 09:25:59

Fluent for NHibernate 在 CodeFirst 环境中工作得非常好,您可以在其中与 DBA 团队(如果有的话)一起定义数据库约定。这将创建一个与 POCO 非常一致的物理模型。

就遗留数据库而言,几乎总是需要 Fluent Overrides 来处理数据库不够一致而无法使用 Fluent Conventions 的情况。 Fluent Automapping 还可以通过创建自定义属性并从自动映射约定中读取它们来轻松扩展。

就查询而言,您不太可能只想使用 LINQ 进行查询。 NHibernate LINQ 实现以及一般的 LINQ 并不总是能够像您希望的那样最好地描述您的连接和查询。例如,在撰写本文时,NHibernate LINQ 实现不支持 LEFT OUTER JOINS。 LINQ 非常容易阅读,并且大多数时候它都表现得很好,但为了更好地控制,您可能需要将 HQL 或 ICriteria 查询与 LINQ 查询一起使用。

我通常打开 show_sql 以及 log4net 的 NHibernate.SQL 记录器,并查看我的查询创建的 SQL 到数据库的内容。

在我想要控制连接或“急切获取”的情况下,我有时会选择 HQLICriteria这让我可以更好地控制 NHibernate 对查询的处理方式。

从本质上讲,Fluent 为映射对象提供了很大的灵活性,并且方法的组合(LINQ、HQL 和 ICritieria)允许您处理几乎任何查询/调整情况。

Fluent for NHibernate works very well in a CodeFirst environment where you can define your database conventions with your DBA team (if you have one). This creates a very consistent physical model from your POCO's.

As far a legacy databases go, Fluent Overrides are almost always needed to handle the times where the database is not consistent enough to use Fluent Conventions. Fluent Automapping can also be easily extended by creating custom attributes and reading them from your automapping conventions.

As far as querying goes, it's unlikely you'd want to ONLY use LINQ for querying. The NHibernate LINQ implementation, and LINQ in general doesn't always do the best job describing your joins and queries like you would want it. For example, at the time of this writing LEFT OUTER JOINS were not supported in the NHibernate LINQ implementation. LINQ can be very easy to read and most of the time it does just fine but for better control you might want to use HQL or ICriteria queries along with your LINQ queries.

I usually turn on show_sql along with the NHibernate.SQL logger for log4net and see what my queries create as far as SQL to the DB.

In situations where I want to control my joins or my "eager fetching" I sometimes choose HQL or ICriteria which gives me more control over what NHibernate does with my queries.

In essence, Fluent give a lot of flexibility to mapping your objects and a combination of approaches (LINQ, HQL and ICritieria) allows you to handle almost any queries/tuning situation.

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