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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:01:50+00:00 2026-05-28T14:01:50+00:00

Having defined a domain model I want to figure out how to do the

  • 0

Having defined a domain model I want to figure out how to do the rest of work.


DATA ACCESS LAYER

I had read before that it is not necessary to code own UnitOfWork implementation over ISession (thogh I found a much information on how to do it pretty well). So I’m quite confused.. I have repository interface like this:

public interface IRepository<T> where T: AbstractEntity<T>, IAggregateRoot
{
    T Get(Guid id);
    IQueryable<T> Get(Expression<Func<T, Boolean>> predicate);
    IQueryable<T> Get();
    T Load(Guid id);
    void Add(T entity);
    void Remove(T entity);
    void Remove(Guid id);
    void Update(T entity);
    void Update(Guid id);
}

Where in the concrete implementation there are two options:

OPTION A

Is to inject ISessionFactory thru constructor and have something similar to:

public class Repository<T> : IRepository<T> where T : AbstractEntity<T>, IAggregateRoot
{
    private ISessionFactory sessionFactory;

    public Repository(ISessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
    public T Get(Guid id)
    {
        using(var session = sessionFactory.OpenSession())
        {
            return session.Get<T>(id);
        }
    }
}

OPTION B

Is to use NHibernateHelper class

using(var session = NHibernateHelper.GetCurrentSession())
{
    return session.Get<T>(id);
}

Where NHibernateHelper is

internal sealed class NHibernateHelper
{
    private const string CurrentSessionKey = "nhibernate.current_session";
    private static readonly ISessionFactory sessionFactory;

    static NHibernateHelper()
    {
        sessionFactory = new Configuration().Configure().BuildSessionFactory();
    }

    public static ISession GetCurrentSession()
    {
        HttpContext context = HttpContext.Current;
        ISession currentSession = context.Items[CurrentSessionKey] as ISession;

        if(currentSession == null)
        {
            currentSession = sessionFactory.OpenSession();
            context.Items[CurrentSessionKey] = currentSession;
        }

        return currentSession;
    }

    public static void CloseSession()
    {
        HttpContext context = HttpContext.Current;
        ISession currentSession = context.Items[CurrentSessionKey] as ISession;

        if(currentSession == null)
        {                
            return;
        }

        currentSession.Close();
        context.Items.Remove(CurrentSessionKey);
    }

    public static void CloseSessionFactory()
    {
        if(sessionFactory != null)
        {
            sessionFactory.Close();
        }
    }
} 

What’s option is prefered?

Why(besides the injection)?

If I use option A where do I place configuration of ISessionFactory?

Should it be placed somewhere in ASP.NET MVC project? How?

Thank you for reading the monster-question! Your guidance is appreciated!

  • 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-28T14:01:51+00:00Added an answer on May 28, 2026 at 2:01 pm

    How to handle injecting dependencies with mvc is somewhat version specific but it always helps to use a real Dependency Injection (DI) container. However you slice it, this solution will need you to Inject an ISession into the Repository rather than an ISessionFactory. This allows your DI container to manage the lifetime of the session properly.

    Assuming you’re using Asp.Net MVC 3 and dont have an attachment to a specific DI container already, fire up your Nuget console and type:

    install-package Ninject.MVC3
    

    This will go, download Ninject (which is a DI container) and configure your mvc application to use it. It will also create a file ~/App_Start/NinjectMVC3.cs which is where you’ll configure your dependencies as such.

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ISessionFactory>()
            .ToMethod(c => new Configuration().Configure().BuildSessionFactory())
            .InSingletonScope();
    
        kernel.Bind<ISession>()
            .ToMethod((ctx) => ctx.Kernel.Get<ISessionFactory>().OpenSession())
            .InRequestScope();
    
        kernel.Bind<IRepository<>>().To<Repository<>>();        
    }   
    

    The first statement tells ninject that when something requires an ISessionFactory, it should lazily initialize NHibernate and create one. This session factory is then to be held as an application-wide singleton for the lifetime of the application.

    The second statement tells ninject that when something requires an ISession, it should get an instance of ISessionFactory and call OpenSession(). This Session is then reused within the scope of the request and destroyed at the end of the request.

    The third statement tells ninject that when something requires an IRepository of any type, it should just new one up using it’s built in logic to resolve dependencies.

    From here you can write your code as follows and everything should just work.

    public class WidgetController : Controller
    {
        private readonly IRepository<Widget> _repository;
        public WidgetController(IRepository<Widget> repository)
        {
            _repository = repository;
        }
    }
    

    With regards to the Repository I’d like to point you to an excelent blog post Repository is the new Singleton

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

Sidebar

Related Questions

When designing both the domain-model and class-diagrams I am having some trouble understanding what
I have a MVC application with a Domain Model well defined, with entities, repositories
I'm trying to figure out how I can define validation rules for my domain
a datacontext defined in a module(domain services ado.net ria) a page having add/delete methods
I have my entities such as Customer, Order etc. defined in my Domain Model.
I have created domain model and define entities, value objects, Services and so on.
I'm having a hard time figuring this validation problem. I have one parent domain
Having defined type MyInt int I would like to define a method .ShowMe() that
I've written myself a nice simple little domain model, with an object graph that
As I design the models for a domain, they almost always end up having

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.