In the SharpArchitecture.MultiTenant.Data project I’ve created an NHibernate folder (and namespace) and added a base class for queries that provides access to the ISession (similar to the existing code in the Repository base class):
As this project is enabled for multi-tenants I’ve also created a marker interface to indicate if the query is tenant specific:
In MultiTenantSessionFactoryKeyProvider, I’ve updated the GetKeyFrom method to test for implementation of the IMultiTenantQuery interface:
Now everything is in place to create some queries, so for the list of Customers I’ve created an interface for the query as below:
and an implementation:
As you can see, this code is using NHibernate projections and transforms to get a list of the required view models, bypassing the need for a data transfer object (DTO) and mappers. This is a trivial example, but in reality the query would be more complex and likely to flatten the object structure.
Now the controller itself can use the query interface to get the paged list of view models:
I quite like this solution; the unnecessary layers of abstraction are removed and the queries are nicely encapsulated, if anything more complex is required (e.g. multiple data sources) then it is always possible to fall back to the services / repositories style as before.