Within my various projects I implement the command/query separation pattern and use NHibernate as my ORM.
In general I keep my commands and queries in separate projects relevant to the particular set of activities such as UserManagement
, TagManagement
, QuestionManagement
etc.
I quite like having everything divided up nicely into these repositories with easily findable functionality. There are certainly benefits in doing things this way.
I am however starting to wonder about the value of this abstraction for basic queries especially given that NHibernate already offers such a powerful abstraction by itself.
Consider this in my MVC controller:
_nHibernateSession.Get<UserProfile>(_sessionData.UserId);
I could abstract this out into a query in the UserManagement
repository but I'm not sure what value that offers.
What do you think? Would you keep everything within the command/query paradigm or would you use the nhibernate session directly in your controllers for simple requests like this?
I prefer to keep all queries in the repository, if you need to make a lot of different query you can expose the a repository that implements an IQueryable and wrap session.Query of LINQ To Nhibernate, that has limitations respect to HQL or Criteria.
But for me there isn't a valid reason to make this, only if you are providing a Data Access Layer to somebody else and you will not know the queries.
You can see an example of this on a DDD project I'm working.