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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:37:26+00:00 2026-05-22T01:37:26+00:00

I am trying to pass my unitofwork into my generic base repository, but when

  • 0

I am trying to pass my unitofwork into my generic base repository, but when i try and call some of the methods the unitofwork isnt being passed into the base repository.

The scenario: I inject the userRepository below into my UserController all fine, its when it calls the userRepository.Save(user) it fails due to the unitofwork being null. Im not sure why though?

Im using nhibernate and structuremap. I think ive wired everything up correctly but here is some code to double check:

Here is the base repository:

public class BaseRepository<T> : IRepository<T> where T : IAggregateRoot
{
  private readonly IUnitOfWork _unitOfWork;

  public BaseRepository(IUnitOfWork unitOfWork)
  {
    _unitOfWork = unitOfWork; 
  } 

  public BaseRepository()
  {}

  public void Save(T Entity)
  {
    _unitOfWork.Session.Save(Entity);
  }
}

A specific repository:

  public class UserRepository : BaseRepository<User>, IUserRepository
  {
  }

This is my nhibernate structuremap configuration:

public NhibernateRegistry()
{
  For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use<UnitOfWork>();

  For(typeof(IRepository<>)).Use(typeof(BaseRepository<>));

  // Nhibernate Session 
  For<ISession>().HybridHttpOrThreadLocalScoped().Use(context => context.GetInstance<ISessionFactory>().OpenSession());

  // Nhibernate SessionFactory
  For<ISessionFactory>().Singleton().Use(NhibernateHelper.CreateSessionFactory());
}`

Here is my nhibernate http module:

public class NHibernateModule : IHttpModule
{
  private IUnitOfWork _unitOfWork;

  public void Init(HttpApplication context)
  {
    context.BeginRequest += ContextBeginRequest;
    context.EndRequest += ContextEndRequest;
  }

  private void ContextBeginRequest(object sender, EventArgs e)
  {
    _unitOfWork = ObjectFactory.GetInstance<IUnitOfWork>();
  }

  private void ContextEndRequest(object sender, EventArgs e)
  {
    try { _unitOfWork.Commit();  }
    catch { _unitOfWork.Rollback();  }
    finally { Dispose(); }
  }

  public void Dispose()
  {
    if (_unitOfWork != null)
      _unitOfWork.Dispose();
  }

}

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

    UserRepository needs a constructor that takes in the IUnitOfWork and passes it to the BaseRepository constructor. Currently, UserRepository is using the parameterless constructor of BaseRepository, so no IUnitOfWork is injected. Get rid of the parameterless constructor, and make sure all derived types pass the IUnitOfWork to the base.

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

Sidebar

Related Questions

Trying to pass some values into an imagebutton click event, something like this: <asp:ImageButton
I just trying to pass some values but it's throwing an error all the
Im trying to pass some values into a text box from a child page
im trying to pass two parameters to a function, i being an int value
I'm trying to pass in a Base64 string into a C#.Net web application via
I am trying to pass some Subsonic collections to a client via a web
I am trying to pass a dataString to to an ajax call using JQuery.
Im trying to pass some variables from the URL to the PHP script. Now
Iam trying to pass dynamic argument values i.e username from request using spring ioc.But
I am trying to pass not only the dropdownlist ID using this, but I'm

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.