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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:11:48+00:00 2026-06-01T05:11:48+00:00

I am trying to use the Unity IoC with the unit of work &

  • 0

I am trying to use the Unity IoC with the unit of work & repository patterns, however I’m struggling to work out how my repository would get it’s Nhibernate session to perform it’s work…

Below I have my IUnityContainer which does my IoC bindings.. I then have my NHibernate repo which needs to be provided with a ISession, which should be provided from the NHibernate unit of work, but I am now sure how I can tell Unity to provide it in it’s constructor..

I have made a binding for the IUnitOfWork to a Nhib… one, but how does one pass that value in the IRepository RegisterType?

private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();
        NHibernateHelper helper = new NHibernateHelper();

        // register all your components with the container here
        // e.g. container.RegisterType<ITestService, TestService>();            

        container.RegisterControllers();
        container.RegisterType<IUnitOfWork, NHibernateUnitOfWork>(new HierarchicalLifetimeManager());
        container.RegisterInstance<ISessionFactory>(helper.SessionFactory);
        container.RegisterType<IRepository, NHibernateRepository>() ;

        return container;
    }



public class NHibernateRepository : IRepository
{

public NHibernateRepository(ISession session)
        {
            _session = session;
        }
}


    public class NHibernateUnitOfWork : IUnitOfWork
    {
        private readonly ISessionFactory _sessionFactory;
        private readonly ITransaction _transaction;
        public ISession Session { get; private set; }

        public NHibernateUnitOfWork(ISessionFactory sessionFactory)
        {
            _sessionFactory = sessionFactory;
            Session = _sessionFactory.OpenSession();
            Session.FlushMode = FlushMode.Auto;
            _transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
        }
}
  • 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-01T05:11:49+00:00Added an answer on June 1, 2026 at 5:11 am

    Register your ISession with your IUnityContainer like so:

    container.RegisterType<ISession>(new InjectionFactory(c => c.Resolve<ISessionFactory>().OpenSession());
    

    Then when an ISession is required, an ISessionFactory will be resolved and a session started.

    In the applications I work on, we define our repositories and unit of work like so:

    public class NhUnitOfWork : IUnitOfWork
    {
        readonly ISession _session;
    
        public IRepository<T> GetRepository<T>() where T : class
        {
            return new NhRepository<T>(_session);
        }
    
        public NhUnitOfWork(ISession session)
        {
            _session = session;
        }
    
        public void Dispose()
        {
            // Dispose logic, i.e. save/rollback
        }
    }
    
    public class NhRepository<T> : IRepository<T> where T : class
    {
        readonly ISession _session;
    
        public void Add(T item)
        {
            _session.Save(item);
        }
    
        public void Delete(T item)
        {
            _session.Delete(item);
        }
    
        public void Update(T item)
        {
            _session.Update(item);
        }
    
        public NhRepository(ISession session)
        {
            _session = session;
        }
    }
    

    Then your services use it something like this:

    public class MyService
    {
        readonly Func<IUnitOfWork> _unitOfWorkFactory;
    
        public MyService(Func<IUnitOfWork> unitOfWorkFactory)
        {
            _unitOfWorkFactory = unitOfWorkFactory;
        }
    
        public void DoServiceStuff()
        {
            using(var uow = _unitOfWorkFactory())
            {
                var newUser = new User() { Username = "My User" };
    
                var userRepo = uow.GetRepository<User>();
                userRepo.Add(newUser);
    
                uow.Save();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out how to use IoC in situations where the
I am trying to use unity to automatically inject a datacontext on my repository
I'm new to NHibernate and I'm trying to impelement Generic Repository Pattern and Unit
I'm trying to use Arquillian for some unit tests, however I can't find why
I'm trying to use check unit testing framework for my C application. But I
I'm looking for an IoC container to use in my Compact Framework application. Trying
I'd like to use Unity as an IoC container for an ASP.NET MVC 3
im trying to get ms unity to inject an object into a constructor and
Currently I am trying to use a config file to give Unity Framework information
I am trying to use the Unity event aggregator to do messaging between various

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.