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

  • Home
  • SEARCH
  • 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 6044701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:02:29+00:00 2026-05-23T07:02:29+00:00

I’m using the Unity MVC3 code at http://unitymvc3.codeplex.com/ to have a NHibernate session per

  • 0

I’m using the Unity MVC3 code at http://unitymvc3.codeplex.com/ to have a NHibernate session per request instance of my IUnitOfWork.

It was working a few weeks ago, I’ve made some changes, and now my IUnitOfWork appears to have a static lifetime.

From my global.asax.cs:

private static IUnitOfWorkFactory _UoWFactory = new UnitOfWorkFactory(Enums.Databases.MyDBName)
                                                            .AddMapping<DepartmentMap>()
                                                            .CompleteConfiguration();

/// <summary>
/// Configure Unity Application Block
/// </summary>
private void ConfigureUnity()
{
    CIUnity.UnityContainer.Container = new UnityContainer()
        .RegisterType(typeof(IRepository<>), typeof(Repository<>))
        .RegisterType<IUnitOfWork, UnitOfWork>(Enums.Databases.MyDBName.ToString(), new HierarchicalLifetimeManager(), new InjectionConstructor(Enums.Databases.MyDBName))
        .RegisterInstance<IUnitOfWorkFactory>(Enums.Databases.MyDBName.ToString(), _UoWFactory);

    DependencyResolver.SetResolver(new UnityDependencyResolver(CIUnity.UnityContainer.Container));
}

ConfigureUnity() is called from my Application_Start()

Heres me test code from my HomeController constructor:

private IUnitOfWork uow;
private IUnitOfWork uow2;

public HomeController()
{
    uow = CIUnity.Container.Resolve<IUnitOfWork>(Enums.Databases.MyDBName.ToString());
    IRepository<Employee> repo = uow.GetRepository<Employee>();

    uow2 = CIUnity.Container.Resolve<IUnitOfWork>(Enums.Databases.MyDBName.ToString());
    IRepository<Department> deptrepo = uow2.GetRepository<Department>();

    var q = from a in repo.Find()
            select a;

    var d = from b in deptrepo.Find()
            select b;
}

… the IUnitOfWork is resolved to the same instance, however, on subsequent page requests, a new IUnitOfWork is not created… but the code works, so its somehow registering / pulling the object so that it acts static.

  • 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-23T07:02:30+00:00Added an answer on May 23, 2026 at 7:02 am

    I would get rid of the IUnitOfWorkFactory altogether and just inject IUnityOfWork into your controller – providing that you only ever want one unit of work per request which seems logical. You are then utilising the power of the IoC container more and not just using it as a service locator.

    Just change your HomeController constructor to:

    public HomeController(IUnitOfWork unitOfWork)
    {
      IRepository<Employee> repo = unitOfWork.GetRepository<Employee>();
    
      IRepository<Department> deptrepo = unitOfWork.GetRepository<Department>();
    
      ...
    }
    

    And if you have not already done so, add:

    CIUnity.Container.RegisterControllers();
    

    before you call DependencyResolver.SetResolver

    When the MVC core tries to instantiate your controller, it will use the Unity.MVC3 resolver which will automatically satisfy any dependencies which in this case means the unitOfWork.

    The Unity.MVC3 resolver will also automatically take care of disposing your unit of work at the end of the request.

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

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
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

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.