Reputation:
Fluent NHibernate or NHibernate, Which one should we prefer for linq support?
Upvotes: 3
Views: 2357
Reputation: 1
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.
Upvotes: 0
Reputation: 8558
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.
Upvotes: 0
Reputation: 4746
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.
Upvotes: 22
Reputation: 4548
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.
Upvotes: 4