Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit of work/repository managers for NHibernate?

I'm a .NET developer by day, but have been working with Rails and Merb for the past year on my own side projects, so when it comes to MVC and ORMs, I'm more used to them and using ActiveRecord and DataMapper.

I'm getting started with ASP.NET MVC, and I like what I see in NHibernate and Fluent NHibernate, but looking for a little bit more around how to best handle the unit of work or repository type model.

I'd like something that is not huge on architecture. I've seen some projects like S#arp Architecture, but it looks kind of bloated.

All I am really after is a simple way to manage connections and handling data retrieval/storage. A simple guide or sample application would even suffice.

like image 610
Ken Robertson Avatar asked Apr 23 '09 21:04

Ken Robertson


People also ask

What is unit of work in Entity Framework?

The unit of work class serves one purpose: to make sure that when you use multiple repositories, they share a single database context. That way, when a unit of work is complete you can call the SaveChanges method on that instance of the context and be assured that all related changes will be coordinated.

What is unit of work in asp net core?

The Unit of Work is a type of business transaction, and it will aggregate all Repository transactions (CRUD) into a single transaction. Only one commit will be made for all modifications. If any transaction fails to assure data integrity, it will be rolled back.

Do we need unit of work with Entity Framework?

Not necessarily. EF already provides the unit of work pattern for you. The only reason to still have a unit of work is if you: want to include non-EF-datasources in an atomic data operation.

What is repository in Entity Framework?

Repository Pattern is used to create an abstraction layer between data access layer and business logic layer of an application. Repository directly communicates with data access layer [DAL] and gets the data and provides it to business logic layer [BAL].


1 Answers

There are a few components you can use for easy Unit of Work implementations:

  • NHibernate Burrow - part of the NHContrib project
  • Rhino Commons - Unit of Work implementation, documentation is in sample code, on the blog and on the web
  • Conversation per Business Transaction - from Fabio Maulo, a primary nhibernate developer, with some documentation on his site

As for sample projects, here is one for Burrow. I am using Burrow for a project right now and it's working fine for me. Although I don't use them, there are repository classes that use the unit of work session management, so it is really easy to get going with it. I also use Fluent NHibernate and it's straightforward to get everything working together.

like image 159
Steven Lyons Avatar answered Sep 22 '22 03:09

Steven Lyons