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 6186359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:52:36+00:00 2026-05-24T01:52:36+00:00

Well in a web application a unit of work is responsible for the transaction

  • 0

Well in a web application a unit of work is responsible for the transaction management.

But what about a windows application?

As far as I know the repository is the connector between my data access layer and my business layer.
It hides all the data access stuff from my business layer.

Using this fact let me think of taking all the transaction stuff into the repository.

But I read that having Commit/RollBack methods on the repository is violating the repository’s intent.

I ask myself who is responsible for transaction management in a non web application and how do I hide the transaction/Nhibernate stuff from the business layer?

  • 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-24T01:52:37+00:00Added an answer on May 24, 2026 at 1:52 am

    The general answer is “Whoever instantiates the ISession should dispose of it. If the transaction has not been committed, this is effectively a rollback.”

    I’ve had success by using the command pattern to define an operation that I want to perform on a unit of work. Say we have a Person entity and one of the things we can do is change a person’s name. Let’s start with the entity:

    public class Person
    {
        public virtual int Id { get; private set; }
        public virtual string Name { get; private set; }
    
        public virtual void ChangeName(string newName)
        {
            if (string.IsNullOrWhiteSpace(newName))
            {
                throw new DomainException("Name cannot be empty");
            }
    
            if (newName.Length > 20)
            {
                throw new DomainException("Name cannot exceed 20 characters");
            }
    
            this.Name = newName;
        }
    }
    

    Define a simple POCO Command like this:

    public class ChangeNameCommand : IDomainCommand
    {
        public ChangeNameCommand(int personId, string newName)
        {
            this.PersonId = personId;
            this.NewName = newName;
        }
    
        public int PersonId { get; set; }
        public string NewName { get; set; }
    }
    

    …and a Handler for the command:

    public class ChangeNameCommandHandler : IHandle<ChangeNameCommand>
    {
        ISession session;
    
        public ChangeNameCommandHandler(ISession session)
        {
            // You could demand an IPersonRepository instead of using the session directly.
            this.session = session;
        }
    
        public void Handle(ChangeNameCommand command)
        {
            var person = session.Load<Person>(command.PersonId);
            person.ChangeName(command.NewName);
        }
    }
    

    The goal is that code that exists outside of a Session/Work scope can do something like this:

    public class SomeClass
    {
        ICommandInvoker invoker;
    
        public SomeClass(ICommandInvoker invoker)
        {
            this.invoker = invoker;
        }
    
        public void DoSomething()
        {
            var command = new ChangeNameCommand(1, "asdf");
            invoker.Invoke(command);
        }
    }
    

    The invocation of the command implies “do this command on a unit of work.” This is what we want to happen when we invoke the command:

    1. Begin an IoC nested scope (the “Unit of Work” scope)
    2. Start an ISession and Transaction (this is probably implied as part of step 3)
    3. Resolve an IHandle<ChangeNameCommand> from the IoC scope
    4. Pass the command to the handler (the domain does its work)
    5. Commit the transaction
    6. End the IoC scope (the Unit of Work)

    So here’s an example using Autofac as the IoC container:

    public class UnitOfWorkInvoker : ICommandInvoker
    {
        Autofac.ILifetimeScope scope;
    
        public UnitOfWorkInvoker(Autofac.ILifetimeScope scope)
        {
            this.scope = scope;
        }
    
        public void Invoke<TCommand>(TCommand command) where TCommand : IDomainCommand
        {
            using (var workScope = scope.BeginLifetimeScope("UnitOfWork")) // step 1
            {
                var handler = workScope.Resolve<IHandle<TCommand>>(); // step 3 (implies step 2)
                handler.Handle(command); // step 4
    
                var session = workScope.Resolve<NHibernate.ISession>();
                session.Transaction.Commit(); // step 5
    
            } // step 6 - When the "workScope" is disposed, Autofac will dispose the ISession.
              // If an exception was thrown before the commit, the transaction is rolled back.
        }
    }
    

    Note: The UnitOfWorkInvoker I’ve shown here is violating SRP – it is a UnitOfWorkFactory, a UnitOfWork, and an Invoker all in one. In my actual implementation, I broke them out.

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

Sidebar

Related Questions

My web application worked very well in a Windows Server 2003 with .NET Framework
From nearlyfreespeech's website, they state that the following don't work well: mod_python Web application
This is odd, but we have written an asp.Net web application (all's well there)
My question may be obvious but I'd like to build a well-designed web application.
I recently upgraded a Web Application Project (as well as some dependent projects) from
I have a small web application with ASP.NET AJAX running well under the Cassini
Well, simple question. I'm working with VS2008 on an ASP.NET web application which has
Currently I'm prototyping search with Lucene.Net-2.0-004 on a web application. It's working very well,
It is a well known problem to every web developer. As far as I
We're developing a data heavy modular web application stack with java but have little

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.