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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:04:23+00:00 2026-06-13T23:04:23+00:00

I have an UnitOfWork attribute, something like this: public class UnitOfWorkAttribute : ActionFilterAttribute {

  • 0

I have an UnitOfWork attribute, something like this:

public class UnitOfWorkAttribute : ActionFilterAttribute
{
    public IDataContext DataContext { get; set; }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {            
        if (filterContext.Controller.ViewData.ModelState.IsValid)
        {
            DataContext.SubmitChanges();
        }

        base.OnActionExecuted(filterContext);
    }
}

As you can see, it has DataContext property, which is injected by Castle.Windsor. DataContext has lifestyle of PerWebRequest – meaning single instance reused for each request.

Thing is, that from time to time I get DataContext is Disposed exception in this attribute and I remember that ASP.NET MVC 3 tries to cache action filters somehow, so may that causes the problem?

If it is so, how to solve the issue – by not using any properties and trying to use ServiceLocator inside method?

Is it possible to tell ASP.NET MVC to not cache filter if it does cache it?

  • 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-06-13T23:04:23+00:00Added an answer on June 13, 2026 at 11:04 pm

    I would strongly advice against using such a construct. For a couple of reasons:

    1. It is not the responsibility of the controller (or an on the controller decorated attribute) to commit the data context.
    2. This would lead to lots of duplicated code (you’ll have to decorate lots of methods with this attribute).
    3. At that point in the execution (in the OnActionExecuted method) whether it is actually safe to commit the data.

    Especially the third point should have drawn your attention. The mere fact that the model is valid, doesn’t mean that it is okay to submit the changes of the data context. Look at this example:

    [UnitOfWorkAttribute]
    public View MoveCustomer(int customerId, Address address)
    {
        try
        {
            this.customerService.MoveCustomer(customerId, address);
        }
        catch { }
    
        return View();
    }
    

    Of course this example is a bit naive. You would hardly ever swallow each and every exception, that would just be plain wrong. But what it does show is that it is very well possible for the action method to finish successfully, when the data should not be saved.

    But besides this, is committing the transaction really a problem of MVC and if you decide it is, should you still want to decorate all action methods with this attribute. Wouldn’t it be nicer if you just implement this without having to do anything on the Controller level? Because, which attributes are you going to add after this? Authorization attributes? Logging attributes? Tracing attributes? Where does it stop?

    What you can try instead is to model all business operations that need to run in a transaction, in a way that allows you to dynamically add this behavior, without needing to change any existing code, or adding new attributes all over the place. A way to do this is to define an interface for these business operations. For instance:

    public interface ICommandHandler<TCommand>
    {
        void Handle(TCommand command);
    }
    

    Using this interface, your controller would look like this:

    private readonly ICommandHandler<MoveCustomerCommand> handler;
    
    // constructor
    public CustomerController(
        ICommandHandler<MoveCustomerCommand> handler)
    {
        this.handler = handler;
    }
    
    public View MoveCustomer(int customerId, Address address)
    {
        var command = new MoveCustomerCommand
        {
            CustomerId = customerId,
            Address = address,
        };
    
        this.handler.Handle(command);
    
        return View();
    }
    

    For each business operation in the system you define a class (a DTO and Parameter Object). In the example the MoveCustomerCommand class. This class contains merely the data. The implementation is defined in a class that implementation of the ICommandHandler<MoveCustomerCommand>. For instance:

    public class MoveCustomerCommandHandler
        : ICommandHandler<MoveCustomerCommand>
    {
        private readonly IDataContext context;
    
        public MoveCustomerCommandHandler(IDataContext context)
        {
            this.context = context;
        }
    
        public void Handle(MoveCustomerCommand command)
        {
            // TODO: Put logic here.
        }
    }
    

    This looks like an awful lot of extra useless code, but this is actually really useful (and if you look closely, it isn’t really that much extra code anyway).

    Interesting about this is that you can now define one single decorator that handles the transactions for all command handlers in the system:

    public class TransactionalCommandHandlerDecorator<TCommand>
        : ICommandHandler<TCommand>
    {
        private readonly IDataContext context;
        private readonly ICommandHandler<TCommand> decoratedHandler;
    
        public TransactionalCommandHandlerDecorator(IDataContext context,
            ICommandHandler<TCommand> decoratedHandler)
        {
            this.context = context;
            this.decoratedHandler = decoratedHandler;
        }
    
        public void Handle(TCommand command)
        {
            this.decoratedHandler.Handle(command);
    
            this.context.SubmitChanges();
        }
    }
    

    This is not much more code than your UnitOfWorkAttribute, but the difference is that this handler can be wrapped around any implementation and injected into any controller, without the controller to know about this. And directly after executing a command is really the only safe place where you actually know whether you can save the changes or not.

    You can find more information about this way of designing your application in this article: Meanwhile… on the command side of my architecture

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

Sidebar

Related Questions

Example: public abstract class BaseControler : Controller { public IUnitOfWork UnitOfWork { get; set;
I have lots of repositories like this: public class PersonRepository : IPersonRepository { private
I have a model like the following: public class Employee { public Employee() {
I have a repository pattern setup using NHibernate. The base class looks like this:
I am looking into the UoW pattern and have 3 questions. public class UnitofWork
have written this little class, which generates a UUID every time an object of
Let's say I have this in an implementation of IInstanceProvider: public void ReleaseInstance(InstanceContext instanceContext,
I have the following models: User: public class User : IEntity, INamedType { public
I Have an MVC Project roughly organized like this: Project 1: MVC app (ViewModels,
I'm using Entity Framework 4 and I have created a UnitOfWork class that creates

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.