Using NCommon – Unit Of Work, Repository, etc…

 

***Update***

The NCommon codebase has been moved from CodePlex to Google Code.  You can get this code here:

http://code.google.com/p/ncommon/

Any links below have not been updated

—-

I was looking for a good repository for using Entity Framework in a proof of concept project.  I stumbled upon a project called ‘NCommon’.   The beauty of this framework is that is appears to support Entity Framework, Linq to Sql, and NHibernate.

The basis of NCommon is that it helps build a ‘domain driven’ architecture.  A summary from the link above describes the major parts of NCommon:

NCommon is a library that contains implementations of commonly used design patterns when developing applications. So far NCommon provides the following:

  • 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
  • A Guard class that mimics common guard statements that verify parameter values and throws exceptions if values are not acceptable.

NCommon includes a sample website – done with asp.net mvc.  I setup a ‘test’ (it’s not really a test, more of a ‘ok, looks good, how does it work’)

My sample snippet is a call to retrieve and address entity and update it’s state:

Step one:  Register the Unit of Work and Repository with your IOC container of choice – in this case I use Castle Windsor, you can use any IOC however, since NCommon uses the Microsoft ServiceLocator.  (Here is an example done in Unity)

image

Since I have chosen to try Entity Framework, I’m registering the ‘EFUnitOfWorkFactory’ and the ‘EFRepository’ – again, you could use Linq2Sql or NHibernate here.  The unit of work factory is, of course, in charge of creating the unit of work  🙂

Step Two:  This is some underlying plumbing that occurs, I’m telling the ServiceLocator what IOC container I’m using for NCommon, additionally, I’m telling the EFUnitOfWorkFactory what EF DataContext I’m using (‘BistroEntities’ is my ObjectContext for my EF test project):

image

I’ll show in a bit what happens at a asp.net mvc level, but this is the core ‘setup’ involved – this is done once in your application, after that you never look at it again   🙂

Now, let’s show how I can use this ‘unit of work’ with a ‘repository’ pattern:

image

First off, I want to define in the code that I’m beginning a unit of work.  Martin Fowler tells us all about ‘Unit of Work’ :

Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems.

Obviously we want this in our application !  ( I should say you ‘need’ this – lol)

In my senario above, I’m only making one ‘save’ call, however, I could have a situation where I’m saving a collection of entities – the address, a company update, an employee update, a audit trail save, etc… This is all accomplished through the UnitOfWorkScope – it is handling the underlying Transaction as well.  The UnitOfWorkScope does a lookup to the ServiceLocator to find the corresponding type of unit of work.

Secondly, I’m instantiating a new EFRepository.  In the asp.net mvc sample, you’ll see this will be associated to a controller by using dependency injection.  It would look something like this:

public AddressController : Controller
{
       private readonly EFRepository<Address> _addressRepository
       public AddressController(EFRepository<Address> addressRepository)
       {
              _addressRepository = addressRepository;
…

(Basically, the container handles the instantiation of the repository on your behalf via dependency injection)

The entity is retrieved, updated, and saved.

Notice the Unit of work ‘Save’ call:  This save call is committing the underlying transaction.  If I left this out…the save wouldn’t occur  🙂

So, how does this work in the NCommon sample website code?

Obviously the real question is, ‘how do we set this up to use in our application’:

In the asp.net mvc application you must set it up in the global.cs file – where the application starts basically: (note, the NCommon sample code uses NHibernate):

image

I’ll skip over the NHibernate specific parts here, you can download the sample, the key item here is telling the Store about the NHibernate ISession (for EF/Linq2Sql this is ‘similiar’ to your context object):

image

The next section is the registration (again, see the sample code)

image

As with above, you can see the registration of the NHUnitOfWorkFactory and the NHRepository.

These get registered along with the controller registration that asp.net mvc offers as well as the ServiceLocator registration of our container.

Lastly then we move to the controller itself, again, looking at the NCommon sample web application:

image

Since the controller is registered, and the IRepository is registered, this is ‘autowired’ to find in the container the IRepository instance and create it on your behalf.

Lastly, we use it:

image

I think this is a fantastic setup and architecture – I would probably insert a ‘service layer’ in between my controller and my repository however.  (and if you have seen my other posts, I think something like RIA.NET – which I think should be called ‘DomainServices.NET’ comes into play…eventually…)

Thanks to Ritesh Rao for his NCommon library as well as for his help in getting starting with NCommon

Using NCommon – Unit Of Work, Repository, etc…

 

***Update***

The NCommon codebase has been moved from CodePlex to Google Code.  You can get this code here:

http://code.google.com/p/ncommon/

Any links below have not been updated

—-

I was looking for a good repository for using Entity Framework in a proof of concept project.  I stumbled upon a project called ‘NCommon’.   The beauty of this framework is that is appears to support Entity Framework, Linq to Sql, and NHibernate.

The basis of NCommon is that it helps build a ‘domain driven’ architecture.  A summary from the link above describes the major parts of NCommon:

NCommon is a library that contains implementations of commonly used design patterns when developing applications. So far NCommon provides the following:

  • 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
  • A Guard class that mimics common guard statements that verify parameter values and throws exceptions if values are not acceptable.

NCommon includes a sample website – done with asp.net mvc.  I setup a ‘test’ (it’s not really a test, more of a ‘ok, looks good, how does it work’)

My sample snippet is a call to retrieve and address entity and update it’s state:

Step one:  Register the Unit of Work and Repository with your IOC container of choice – in this case I use Castle Windsor, you can use any IOC however, since NCommon uses the Microsoft ServiceLocator.  (Here is an example done in Unity)

image

Since I have chosen to try Entity Framework, I’m registering the ‘EFUnitOfWorkFactory’ and the ‘EFRepository’ – again, you could use Linq2Sql or NHibernate here.  The unit of work factory is, of course, in charge of creating the unit of work  🙂

Step Two:  This is some underlying plumbing that occurs, I’m telling the ServiceLocator what IOC container I’m using for NCommon, additionally, I’m telling the EFUnitOfWorkFactory what EF DataContext I’m using (‘BistroEntities’ is my ObjectContext for my EF test project):

image

I’ll show in a bit what happens at a asp.net mvc level, but this is the core ‘setup’ involved – this is done once in your application, after that you never look at it again   🙂

Now, let’s show how I can use this ‘unit of work’ with a ‘repository’ pattern:

image

First off, I want to define in the code that I’m beginning a unit of work.  Martin Fowler tells us all about ‘Unit of Work’ :

Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems.

Obviously we want this in our application !  ( I should say you ‘need’ this – lol)

In my senario above, I’m only making one ‘save’ call, however, I could have a situation where I’m saving a collection of entities – the address, a company update, an employee update, a audit trail save, etc… This is all accomplished through the UnitOfWorkScope – it is handling the underlying Transaction as well.  The UnitOfWorkScope does a lookup to the ServiceLocator to find the corresponding type of unit of work.

Secondly, I’m instantiating a new EFRepository.  In the asp.net mvc sample, you’ll see this will be associated to a controller by using dependency injection.  It would look something like this:

public AddressController : Controller
{
       private readonly EFRepository<Address> _addressRepository
       public AddressController(EFRepository<Address> addressRepository)
       {
              _addressRepository = addressRepository;
…

(Basically, the container handles the instantiation of the repository on your behalf via dependency injection)

The entity is retrieved, updated, and saved.

Notice the Unit of work ‘Save’ call:  This save call is committing the underlying transaction.  If I left this out…the save wouldn’t occur  🙂

So, how does this work in the NCommon sample website code?

Obviously the real question is, ‘how do we set this up to use in our application’:

In the asp.net mvc application you must set it up in the global.cs file – where the application starts basically: (note, the NCommon sample code uses NHibernate):

image

I’ll skip over the NHibernate specific parts here, you can download the sample, the key item here is telling the Store about the NHibernate ISession (for EF/Linq2Sql this is ‘similiar’ to your context object):

image

The next section is the registration (again, see the sample code)

image

As with above, you can see the registration of the NHUnitOfWorkFactory and the NHRepository.

These get registered along with the controller registration that asp.net mvc offers as well as the ServiceLocator registration of our container.

Lastly then we move to the controller itself, again, looking at the NCommon sample web application:

image

Since the controller is registered, and the IRepository is registered, this is ‘autowired’ to find in the container the IRepository instance and create it on your behalf.

Lastly, we use it:

image

I think this is a fantastic setup and architecture – I would probably insert a ‘service layer’ in between my controller and my repository however.  (and if you have seen my other posts, I think something like RIA.NET – which I think should be called ‘DomainServices.NET’ comes into play…eventually…)

Thanks to Ritesh Rao for his NCommon library as well as for his help in getting starting with NCommon

RIA.NET, ADO.NET Data Services… NHibernate?

My observation, despite seeing slides that throw ‘nhibernate’ into them, is that, of course, the new technologies I see emerging from Microsoft is really geared toward the Entity Framework.  That doesn’t surprise me, I do think it provides some rather seamless integration.

However, I do think it tends to push these great open source projects out of the fold. 

One of the most interesting projects I’ve seen come out of Microsoft has been the asp.net mvc project.  Rather than push out these open source projects, they embraced them, and even added support for them (the biggest one being jQuery).  Granted, the community took the ball and ran with it with the MVCContrib project.  The MVCContrib project for asp.net mvc, to me, represents a great community involvement into the asp.net mvc development process.   It excels with different view engine options, different validation options, new controls, different Inversion of control container support, etc… On top of that, we have developers building templates like the Sharp Architecture (oh never mind, looks like they moved away from codeplex to google… which maybe highlights what I’m trying to say here…)

I know RIA.NET is very new, and my guess is that it was probably pushed as hard as possible to make it out the door by Mix 09 (my guess, I have no idea what happens internally at MS – lol), but the success of this IMO is the ability to pull off what asp.net mvc did by embracing the community, getting them involved. 

Showing ‘how to use RIA.NET with NHibernate’ is one great example – (even a ‘here is how to use this with DAAB’ would be nice – create database connectivity).  

So, what I’m trying to say … :  we need to see from MS the continuation of what we saw from the asp.net mvc team – the willingness to go outside of ‘just the core ORM EF/Linq2SQL’.   Just to give an idea, I’ve seen one example of ADO.NET DataServices with NHibernate out on the web – done by Shawn Wildermuth.  That is about all I’ve seen , and even in that post (none of the needed NHibernate.Linq libraries were included).  Perhaps it is due to NHibernate moving slowly on the Linq front – it is there now as a wrapper around the ICriteria, but it is critical imo to getting developers interested in technologies such as RIA.NET.  Shawn’s example shows ‘here is the EF version, here is the NHibernate version’. 

I’d like to see more of this – just including the verbage of ‘oh, and you can do that with NHibernate’ isn’t very helpful, it comes across to me as just giving lip service to the open source community when there isn’t any examples or concentration on the matter   :)   Nikhil gave great examples of RIA with Azure, Linq2SQL, even jQuery in his slides and talks from Mix 09… I encourage them to extend that into the technologies such as DAAB and NHibernate to really show how RIA can be the ‘glue’ between those layers and the UI.

Update:

I guess I do see from Brad Abram’s blog post ‘What is RIA.NET Services’ that these items I address are in the pipeline and just not ready yet.

ie.

System.Web.DomainServices.Providers.dll  – the LINQ2SQL and EF provider… we hope between us and the community to provide much more in the future such as Azure, Dataset, nHibernate, etc

 

So… it sounds like the missing piece right now is that an nHibernate DomainService Provider must be written.

I would love to see someone(s) from the RIA.NET team really engaging the community, answering questions, posting information – become very transparent – as I see a ton of potential in what MS has done with RIA.NET.  I think after the release at Mix 09 it will continue to produce many questions by developers interested in learning more about it  ie. with Nikhil Kothari showing jQuery calling RIA.NET services it opens the door up to many more possibilities outside of ‘just Silverlight’. 

I think there is a great opportunity to really show and promote this ‘prescriptive pattern’‘.   In the rich applications (ie. asp.net mvc with jquery, asp.net webforms needing richer client side capability, then finally the silverlight controls) I’ve built, the RIA.NET capabilities would have filled that much needed space.

Basically… I’m very interested in learning more about slide shown from Nikhil’s talk:

I hope to see the RIA.NET team continue to flush this out with examples, demo code, etc… filling in the voids  🙂

RIA.NET, ADO.NET Data Services… NHibernate?

My observation, despite seeing slides that throw ‘nhibernate’ into them, is that, of course, the new technologies I see emerging from Microsoft is really geared toward the Entity Framework.  That doesn’t surprise me, I do think it provides some rather seamless integration.

However, I do think it tends to push these great open source projects out of the fold. 

One of the most interesting projects I’ve seen come out of Microsoft has been the asp.net mvc project.  Rather than push out these open source projects, they embraced them, and even added support for them (the biggest one being jQuery).  Granted, the community took the ball and ran with it with the MVCContrib project.  The MVCContrib project for asp.net mvc, to me, represents a great community involvement into the asp.net mvc development process.   It excels with different view engine options, different validation options, new controls, different Inversion of control container support, etc… On top of that, we have developers building templates like the Sharp Architecture (oh never mind, looks like they moved away from codeplex to google… which maybe highlights what I’m trying to say here…)

I know RIA.NET is very new, and my guess is that it was probably pushed as hard as possible to make it out the door by Mix 09 (my guess, I have no idea what happens internally at MS – lol), but the success of this IMO is the ability to pull off what asp.net mvc did by embracing the community, getting them involved. 

Showing ‘how to use RIA.NET with NHibernate’ is one great example – (even a ‘here is how to use this with DAAB’ would be nice – create database connectivity).  

So, what I’m trying to say … :  we need to see from MS the continuation of what we saw from the asp.net mvc team – the willingness to go outside of ‘just the core ORM EF/Linq2SQL’.   Just to give an idea, I’ve seen one example of ADO.NET DataServices with NHibernate out on the web – done by Shawn Wildermuth.  That is about all I’ve seen , and even in that post (none of the needed NHibernate.Linq libraries were included).  Perhaps it is due to NHibernate moving slowly on the Linq front – it is there now as a wrapper around the ICriteria, but it is critical imo to getting developers interested in technologies such as RIA.NET.  Shawn’s example shows ‘here is the EF version, here is the NHibernate version’. 

I’d like to see more of this – just including the verbage of ‘oh, and you can do that with NHibernate’ isn’t very helpful, it comes across to me as just giving lip service to the open source community when there isn’t any examples or concentration on the matter   :)   Nikhil gave great examples of RIA with Azure, Linq2SQL, even jQuery in his slides and talks from Mix 09… I encourage them to extend that into the technologies such as DAAB and NHibernate to really show how RIA can be the ‘glue’ between those layers and the UI.

Update:

I guess I do see from Brad Abram’s blog post ‘What is RIA.NET Services’ that these items I address are in the pipeline and just not ready yet.

ie.

System.Web.DomainServices.Providers.dll  – the LINQ2SQL and EF provider… we hope between us and the community to provide much more in the future such as Azure, Dataset, nHibernate, etc

 

So… it sounds like the missing piece right now is that an nHibernate DomainService Provider must be written.

I would love to see someone(s) from the RIA.NET team really engaging the community, answering questions, posting information – become very transparent – as I see a ton of potential in what MS has done with RIA.NET.  I think after the release at Mix 09 it will continue to produce many questions by developers interested in learning more about it  ie. with Nikhil Kothari showing jQuery calling RIA.NET services it opens the door up to many more possibilities outside of ‘just Silverlight’. 

I think there is a great opportunity to really show and promote this ‘prescriptive pattern’‘.   In the rich applications (ie. asp.net mvc with jquery, asp.net webforms needing richer client side capability, then finally the silverlight controls) I’ve built, the RIA.NET capabilities would have filled that much needed space.

Basically… I’m very interested in learning more about slide shown from Nikhil’s talk:

I hope to see the RIA.NET team continue to flush this out with examples, demo code, etc… filling in the voids  🙂

Nikhil Kothari on .NET RIA Services

Nikhil gave a talk yesterday at Mix 09 and is sharing his slides, code, etc… read more on his blog here

.NET RIA Services builds on Silverlight 3 on the client and ASP.NET on the server to simplify building n-Tier data-driven applications. First, .NET RIA Services provides a prescriptive CRUD-based pattern for authoring your application/domain/business logic, i.e. queries, operations, rules for authorization, validation etc. on the middle tier and makes that functionality easily consumable from your presentation tier in the form of bindable data. Second, on the presentation tier, we’re providing new data controls such as a DataSource, enhanced DataGrid and DataForm/DataPager controls for mainline data/LOB scenarios. Third, we’re also providing higher level building blocks such as authentication, cross-tier user state and the like.

There is no doubt in my mind as I watch/listen to Nikhil in his video that the goal and vision of RIA Services is spot on.

Update : my favorite slides in his deck :

The ‘prescriptive’ pattern…

image

…which flows into the bigger picture:

image

(note: these are Nikhil’s images not mine!!!!)

Thanks Nikhil, this is fantastic stuff!

IE8 is out

To keep your site compatible you can add some meta tags:

Turn the mode to IE7:

<meta http-equiv="X-UA-Compatible" content="IE=7">

Make sure IE8 emulate IE7:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>

I haven’t had a chance to really compare the difference of these 2 yet, but I’m glad they exist  🙂