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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:08:57+00:00 2026-06-17T11:08:57+00:00

I just have a quick question. Im trying to use Unity with my asp.net

  • 0

I just have a quick question. Im trying to use Unity with my asp.net MVC project. Im coming across a problem when using the Unit of Work pattern with an EF context.

Say i inject the uow in the constructor, but have 4 or 5 actions in the controller that need to use the UnitOfWork in a using statement. This isnt going to work! Because Id have to do a
new UnitOfWork() in each action method.

should i be injecting a UnitOfWork into each action method? or into just the constructor? or should I even be injecting this at all!! The problem im facing is that i want to be able to unit test my controller with Mock data, and i can only do this if I inject the UnitOfWork or the DBContext.

  • 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-17T11:08:59+00:00Added an answer on June 17, 2026 at 11:08 am

    Inject factory instead. This way you still achieve separation of concerns and loose coupling, yet you won’t face any issues with using statements:

    private IUnitOfWorkFactory factory;
    
    public MyController(IUnitOfWorkFactory factory)
    {
        this.factory = factory;
    }
    
    public ActionResult MyAction()
    {
        using (var uow = factory.CreateUnitOfWork())
        {
            // ...
        }
    }
    

    Edit:

    Natural advantage of such approach is its configurability – you can register whichever factory you like to serve different controllers and wire it up at composition root:

    // Note: this isn't unity syntax, but I hope my point is clear
    container.Register<ISessionFactory, ReusableSessionFactory>("Reusable");
    container.Register<ISessionFactory, FreshSessionFactory>("Fresh");
    container.Register<IController, LoginController>().With("Fresh");
    container.Register<IController, HomeController>().With("Reusable");
    

    Now,

    • LoginController will use factory that under the hood serves new session upon each request
    • HomeController on the other hand will reuse the same session for all its lifespan

    It’s worth noting that from the controller point of view, it’s irrelevant which factory serves the session as it’s a mere implementation detail. That’s why we hide session factory dependency behind abstraction (interface in this example) and perform all the object-to-dependency binding at application’s root.

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

Sidebar

Related Questions

Just a quick question. I don't use sitefinity and I don't currently have access
I have got just a quick question regarding the header hierarchy when using HTML5
Just getting started with Powershell and have a quick question. Trying to run this
Hello: Just a quick question.. I hope. I am trying to use this program
Link I'm using ASP.NET with C# and trying to use linq to sql to
I just have a quick question about how to get the length of a
I have a quick question. I just wanted to know if it was valid
Just a quick question, I have a figure which contain an image and a
Just a quick question here. I have a program in which I need to
Just a (hopefully) quick question, I have the following HTML code: <tr> <td><img src=img/icons/file_pdf.png></td>

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.