简体   繁体   English

适用于Linq的流利NHibernate或NHibernate?

[英]Fluent NHibernate or NHibernate for Linq?

流利的NHibernate或NHibernate,我们应该更喜欢linq支持哪一个?

Fluent NHibernate is from Configuration and Linq is for querying. 流畅的NHibernate来自Configuration,Linq用于查询。 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. 流畅的NHibernate不是NHibernate或Linq的替代品,而只是一个帮助您在代码中配置NHibernate而不是使用XML文件的辅助库。

When you use NHibernate you create mappings for your objects and the queries (LINQ) to CRUD those objects. 当您使用NHibernate时,您可以为对象创建映射,并为查询(LINQ)创建CRUD这些对象。 NHibernate uses XML mapping files. NHibernate使用XML映射文件。 Fluent NHibernate simply generates those mapping files for you based on conventions or any other of the possible ways. 流畅的NHibernate只是根据约定或任何其他可能的方式为您生成那些映射文件。 So, it does not matter if you use FNHib or not. 因此,如果您使用FNHib则无关紧要。 You can still query your objects with Linq, Hql, Criteria or QueryOver and you are still goin to use NHibernate. 您仍然可以使用Linq,Hql,Criteria或QueryOver查询对象,但您仍然需要使用NHibernate。

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). Fluent for NHibernate在CodeFirst环境中运行良好,您可以在DBA团队中定义数据库约定(如果有的话)。 This creates a very consistent physical model from your POCO's. 这可以从您的POCO中创建一个非常一致的物理模型。

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 Overrides来处理数据库不够一致以使用Fluent Conventions的时间。 Fluent Automapping can also be easily extended by creating custom attributes and reading them from your automapping conventions. 通过创建自定义属性并从自动化约定中读取它们,也可以轻松扩展Fluent Automapping。

As far as querying goes, it's unlikely you'd want to ONLY use LINQ for querying. 就查询而言,您不太可能只想使用LINQ进行查询。 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. NHibernate LINQ实现和LINQ一般并不总是能够像你想要的那样最好地描述你的连接和查询。 For example, at the time of this writing LEFT OUTER JOINS were not supported in the NHibernate LINQ implementation. 例如,在撰写本文时,NHibernate LINQ实现不支持LEFT OUTER JOINS。 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. LINQ可以非常容易阅读,并且大部分时间它都可以正常运行但是为了更好地控制,您可能希望使用HQL或ICriteria查询以及LINQ查询。

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. 我通常打开show_sql以及用于log4net的NHibernate.SQL记录器,并查看我的查询创建到SQL的数据库。

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. 在我想控制我的连接或“渴望获取”的情况下,我有时会选择HQLICriteria ,这让我可以更好地控制NHibernate对我的查询所做的事情。

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. 实质上,Fluent为映射对象提供了很大的灵活性,并且组合方法(LINQ,HQL和ICritieria)允许您处理几乎任何查询/调整情况。

NHibernate - write configuration in XML NHibernate - 用XML编写配置

Fluent NHibernate - write configuration in C# or VB.NET with type safe 流畅的NHibernate - 使用类型安全的C#VB.NET编写配置

You should choose Fluent NHibernate if you want it type safe and hate to write configuration in XML. 您应该选择Fluent NHibernate,如果您希望它类型安全并且不喜欢用XML编写配置。 So any changes you can easily rename and refactor . 因此,您可以轻松地重命名和重构任何更改。 You just type dot and intellisense will help you a lots with the documentation. 你只需输入点, intellisense将帮助你获得很多文档。 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. 流畅的NHibernate中的Linq与NHibernate本身是一样的。 because like other answer, Fluent NHibernate is just for config and make ease to use NHibernate, the rest is come from NHibernate. 因为像其他答案一样,Fluent NHibernate只是用于配置并且易于使用NHibernate,其余的来自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 . 我使用Fluent NHibernate但是当你右键单击.Query<> “Go To Definition”时,你会发现它来自namespace NHibernate.Linq So either way is actually you use the same Linq. 所以无论哪种方式实际上你都使用相同的Linq。

Just if you said Linq for NHibernate vs built-in Linq for Sql is different. 如果你说Linq for NHibernate vs内置Linq for Sql是不同的。 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. 我认为它是一样的但当我尝试以更复杂的方式进行查询时,它会抛出异常或只是一个空的Sql查询。

Conclusion : either way you choose for Linq is just the same, it is NHibernate Linq . 结论 :你选择Linq的方式是一样的,它是NHibernate Linq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM