Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4119150
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:03:13+00:00 2026-05-20T23:03:13+00:00

I’m using NHibernate, DI/IoC and the Unit of Work pattern. Most UoW examples I’ve

  • 0

I’m using NHibernate, DI/IoC and the Unit of Work pattern.

Most UoW examples I’ve seen make sure that there can be only one active UoW/session at the same time, for example this one and this one.

Unfortunately, I don’t quite understand yet how I should handle two services which both use UoW, but one calls the other.
Take this example:

A logger which uses an UoW:

public class LoggerService : ILoggerService
{
    private ILoggerRepository repo;

    public LoggerService(ILoggerRepository Repo)
    {
        this.repo = Repo;
    }

    public void WriteLog(string message)
    {
        using (UnitOfWork.Start())
        {
            // write log
        }
    }
}

…and another service which uses an UoW as well AND calls the logger:

public class PlaceOrderService : IPlaceOrderService
{
    private IOrderRepository repo;
    private ILoggerService service;

    public PlaceOrderService(IOrderRepository Repo, ILoggerService Service)
    {
        this.repo = Repo;
        this.service = Service;
    }

    public int PlaceOrder(int orderNumber)
    {
        using (UnitOfWork.Start())
        {           
            // do stuff

            this.service.WriteLog("Order placed!");  // will throw exception!!

            // do more stuff
        }
    }
}

If my UoW implementation makes sure that there is only one active UoW at the same time (and throws an exception if you try to start another one, like in both linked examples), my code will crash in the this.service.WriteLog line in the PlaceOrder method:
There will already be an active UoW created by the PlaceOrder method, and the WriteLog method will attempt to open a second one, so the UoW implementation will throw an exception because of this.

So, what can I do about this?
I came up with two ideas, but both look somehow “hacky” to me.

  1. Don’t start a new UoW in the LoggerService, instead assume there is already an active one in the calling code.
    That’s what I’m doing at the moment. I just removed the using (UnitOfWork.Start()) stuff from the LoggerService and made sure that you can’t directly call the LoggerService, only from other services.
    This means that the code above will work, but the LoggerService will crash if the calling code doesn’t start an UoW because the LoggerService assumes that one already exists.

  2. Leave the example code as it is, but change the implementation of UoW.Start() like this:
    a) if there is no active UoW, start a new one
    b) if there already is an active UoW, return this one
    This would enable me to call the LoggerService directly AND from other services, no matter if there is already an UoW or not.
    But I’ve never seen anything like this in any example on the net.

(And in this example, there are only two services. It could get much more complicated, just think of a PlaceSpecialOrderService class which does some special stuff and then calls PlaceOrderService.PlaceOrder()…)

Any advices?
Thanks in advance!


EDIT:

Thank you for the answers so far.

Okay, maybe logging was not the best example.
I see your point concerning using a separate session for logging, and I will give this a look and try it.

Anyway, I still need to find a way to make nested service calls work.
Imagine some other example instead of logging, like the PlaceSpecialOrderService example I mentioned above.

To the answerers suggesting that I start my UoW somewhere in the infrastructure, and not directly in the services:
On one hand, that makes sense too, but on the other hand it would obviously mean that I can’t do two different transactions in one service call.
I’ll have to think about that, because I’m quite sure that I’ll need this somewhere (like: save an order in one transaction, then do more stuff in a second transaction, and even if that fails, the order doesn’t get rolled back).

Did you do it this way (one UoW per service call) in your apps?
Didn’t you ever need the possibility to start a second UoW in the same service call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:03:14+00:00Added an answer on May 20, 2026 at 11:03 pm

    I think I found a solution myself.

    Actually, it was one of my proposed solutions in my question:

    Leave the example code as it is, but change the implementation of UoW.Start() like this:
    a) if there is no active UoW, start a new one
    b) if there already is an active UoW, return this one
    This would enable me to call the LoggerService directly AND from other services, no matter if there is already an UoW or not.
    But I’ve never seen anything like this in any example on the net.

    I already came up with this idea before I asked my question here, but I was not sure if this is a good solution, because I found loads of different UoW implementations on the net, but nothing similar to my idea.

    But I actually found an implementation of this – I read the complete post but I just somehow over-read the relevant part 🙂
    It was in the first link that I posted in my original question.
    The UoW implementation there has a bool field “isRootUnitOfWork”.
    The constructor basically does this (simplified):

    if (HasActiveSession)
    {
        isRootUnitOfWork = false;
        session = GetActiveSession();
    }
    else
    {
        isRootUnitOfWork = true;
        session = CreateSession();
    }
    

    I think that’s the most flexible solution. I can call a single one of my services, or have one call the other…and it all works, without doing any special tricks with the UoW.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.