Once again, Iâ€™m going to talk about NCommon. I want to share some of Ritesh Raoâ€™s posts on concepts that NCommon incorporates. Iâ€™m very impressed with his architecture and it helps that he provides NCommon for NHibernate, Linq2Sql, Entity Framework (and I see he is venturing into EF v2).
My last post touched on this, but Iâ€™ll review it again:
- Framework for implementing a Unit of Work Pattern
- Framework for implementing a Repository pattern that utilizes Linq
- Framework for implementing a Validations and Business Rules
- Implementation of the Specification pattern using Expressions
- Utility class to help store application specific data in the Thread Local Storage / Current Web Request and AppDomain Level Storage
- Guard class that mimics common guard statements that verify parameter values and throws exceptions if values are not acceptable.
NCommon uses the Apache License 2.0
NCommon uses the Microsoft.Practices.ServiceLocation â€“ this allows you to plug in your dependency inversion container of choice.
I have gotten rid of the generic IoC wrapper implementation from the library as it doesnâ€™t seem necessary anymore with the rally behind the Common Service Locator project.
This is shown in the accompany tests setup:
public void SetUp()
var context = new TestModel();
var locator = MockRepository.GenerateStub<IServiceLocator>();
locator.Stub(x => x.GetInstance<IUnitOfWorkFactory>())
ServiceLocator.SetLocatorProvider(() => locator);
NCommon sets the object context and implements the IUnitOfWorkFactory (the above shows with Entity Framework).
An example of how this is used can be found in the UnitOfWorkScopeTransaction class (UnitOfWorkScopeTransaction.cs- trunk/NCommon/src/Data)
ie. the â€˜GetTransactionForScopeâ€™ method to retrieve the UnitOfWorkFactory:
var factory = ServiceLocator.Current.GetInstance<IUnitOfWorkFactory>(); var newTransaction = new UnitOfWorkScopeTransaction(factory, isolationLevel); newTransaction.AttachScope(scope); CurrentTransactions.Add(newTransaction); return newTransaction;
The NCommon tests highlight the features of NCommon as well, including Fetching Strategies, Specification, Repositories, and Unit of Work.
Letâ€™s look at some of Riteshâ€™s postsâ€¦
Some key items in this post to quote:
repositories should really represent a queryable data store that doesnâ€™t abstract away queries behind methods that some unfortunate developer has to maintain and evolve, but rather allow consumers of the repository to query it directly. Hence why NCommon relies on repositories to implement the IQueryable interface to provide a query infrastructure directly on top of NCommon repositories.
I think this is an important piece to understand, and Ritesh follows up with the comment:
The approach I would take is rather than exposing the infrastructure requirements in the query object, Iâ€™d like it to take in an IQueryable and return back a IQueryable. This will allow chaining of queries by multiple of such Query objects without exposing any infrastructure concerns.
The problem with exposing ICriteria, or any other infrastructure component, to all layers of the application is that eventually a lot of infrastructure concern creeps into layers of the application where they donâ€™t belong. I dislike the idea of having the expose ICriteria to the UI for adding paging and sorting on top of the query encapsulated by ICriteria.
That being said, in my opinion IQueryable is best suited for this job. Its a framework level member that is infrastructure agnostic, provides a very nice way to chain queries together and encapsulates our query requirements rather well.