Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6158067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:53:34+00:00 2026-05-23T20:53:34+00:00

What is the recommended approach to take with Nhibernate + Repository pattern? There are

  • 0

What is the recommended approach to take with Nhibernate + Repository pattern?

There are so many different articles and opinions around that I am not sure what path to take. Take this lengthy article, for example. It gives an example of Query objects, but each concrete repository accepts an ISession in its constructor. What should I care about NH sessions in my BL (business layer)?

  1. Create a bunch of repositories, each of them having a bunch of specific methods?
    Apparently, that’s too much work because BL is now “allowed” to be aware of NHibernate (Repository is the new Singleton)?

  2. Create a single generic repository, but expose IQueriable<T> and use LINQ in BL
    Every now and then there is a query which LINQ-to-NHibernate won’t be able to process (or I need to tweak the SQL manually once in a hundred queries). This is easy with custom repo methods, but next to impossible with code relying on LINQ. And using both just because LINQ is broken in some cases is nonsense.

  3. Query objects?
    QueryOver is also NH-specific, which means BL is again aware of the DAL implementation.

  4. Yet Another Approach?

Obviously, I need to be able to manage transactions somewhere, maybe using a Unit-of-work patten (although there are also many different implementations of that around).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:53:34+00:00Added an answer on May 23, 2026 at 8:53 pm

    There are many conflicting opinions within the world of software architecture and many of these are very well-founded.

    1) Yes, defining a repository for each aggregate root can be overkill, but you might find that the way you retrieve data for that root may be specific to it and you want to be able to control it in a custom repository. Ayende’s article is very telling and hits closely at the typical developer’s desire to ‘over-architect’ solutions – often to the detriment of functionality.

    2) Using LINQ with NHibernate is an option but I would stress that you must give yourself the ability to fall back to using criteria queries or HQL if you do this. At that stage you might find yourself putting in so many exceptions you’ll wonder what the initial point of the abstraction was.

    3) QueryOver is a type-safe wrapper around criteria queries and this alone makes them far more attractive to me. I so often find myself falling back to criteria for the sheer control that they offer – the fallback often being that I have to use “magic strings”. This would essentially make NHibernate your data access abstraction. In fact, it would be a better abstraction that most ‘repositories’ because most of them just provide CRUD methods…a repository should be able to handle query specifications (exactly how QueryOver works).

    As far as transactions go – it depends on your framework. In reality I don’t think its realistic or beneficial to use the unit of work pattern in an application and hide the implementation from it (you could abstract it – but what’s the point? – are you really going to change your ORM?)

    If you’re using ASP.NET then just (at minimum) start the transaction at the start of the request, rollback on exceptions and commit at the end.

    If you’re using WinForms then you may need to consider the lifespan of each UI task to be your unit of work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the recommended approach to dealing with process which take a long time
Design wise and performance wise which approach is recommended for handling multiple Zeromq sockets
Dependency injection is a useful technique but what approach is recommended when faced with
Are there any recommended libraries, patterns or examples on how to implement Prefetching and/or
As far as I can tell the recommended approach for ASP.NET MVC 1 is
When creating a desktop application in Netbeans, what is the recommended approach in separating
What is the recommended approach to handle a SharePoint deployment via a WSP? During
PROBLEM: I am hosting a widget on a client's website that will be different
I have copied a database but I did not take precautions and copied only
Edit: I decided to take the LINQ to XML approach (see the answer below)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.