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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:37:56+00:00 2026-05-24T15:37:56+00:00

i have followed the pattern on this site to hook up ninject and nhibernate

  • 0

i have followed the pattern on this site to hook up ninject and nhibernate to my asp.net-mvc3 site.

Here is the code in my global.aspx.cs:

 internal class ServiceModule : NinjectModule
{
    public override void Load()
    {
        var helper = new NHibernateHelper(connectionString);
        Bind<ISessionFactory>().ToConstant(helper.SessionFactory)
            .InSingletonScope();

        Bind<IUnitOfWork>().To<UnitOfWork>()
            .InRequestScope();
        Bind<ISession>().ToProvider(new SessionProvider())
            .InRequestScope();
        Bind<IIntKeyedRepository<FAQ>>().To<Repository<FAQ>>()
            .InRequestScope();
       }

the issue is that i now need to do Update() and Add() in my controllers;

I have this as my controller code:

    public FAQController(IIntKeyedRepository<FAQ> faqRepository, IUnitOfWork unitOfWork)
    {
        _faqRepository = faqRepository;
        _unitOfWork = unitOfWork;
    }


  [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    public ActionResult AddFAQ(FAQ contact)
    {
        var c = new FAQ {Question = contact.Question, Answer = contact.Answer};
        _faqRepository.Add(c);
        _unitOfWork.Commit();
        return RedirectToAction("Index");
    }

my main question is that it feels wrong to pass in Iunitofwork in the constructor as many other actions don’t need it. I only really need it for the actions where i do updates and inserts into my db. Since i am using ninject IOC on the link above it seems to say to pass this unitofwork object through IOC.

So, is there a better more optimized way to using the UnitOfWork pattern with IOC in asp.net-mvc that does call beingtransaction for every method in my controller.

  • 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-24T15:37:57+00:00Added an answer on May 24, 2026 at 3:37 pm

    An alternative way to handle transactions is to use an IActionFilter Open the transaction in OnActionExecuting and commit on OnActionExecuted

    public class TransactionFilter : IActionFilter
    {
        private readonly ISession session;
        private ITransaction transaction;
    
        public TransactionFilter(ISession session)
        {
            this.session = session;
        }
    
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            this.transaction = this.session.BeginTransaction();
        }
    
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            try
            {
                if (this.transaction.IsActive)
                {
                    if (filterContext.Exception == null)
                    {
                        this.transaction.Commit();
                    }
                    else
                    {
                        this.transaction.Rollback();
                    }
                }
            }
            finally
            {
                this.transaction.Dispose();
            }
        }
    }
    

    Define an attribute to mark the actions that use a transaction:

    [AttributeUsage(AttributeTargets.Method)]
    public class TransactionAttribute : Attribute
    { 
    }
    

    Change your Ninject configuration:

    internal class ServiceModule : NinjectModule
    {
        public override void Load()
        {
            var helper = new NHibernateHelper(connectionString);
            Bind<ISessionFactory>().ToConstant(helper.SessionFactory)
                .InSingletonScope();
    
            Bind<ISession>().ToProvider<SessionProvider>().InRequestScope();
            Bind(typeof(IRepository<>)).To(typeof(Repository<>));
            Bind(typeof(IIntKeyedRepository<>)).To(typeof(Repository<>));
            BindFilter<TransactionFilter>(FilterScope.Action, null)
                .WhenActionMethodHas<TransactionAttribute>();
        }
    }
    

    Finally change your controller:

    public FAQController(IIntKeyedRepository<FAQ> faqRepository)
    {
        _faqRepository = faqRepository;
    }
    
    
    [Transaction]
    [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    public ActionResult AddFAQ(FAQ contact)
    {
        var c = new FAQ {Question = contact.Question, Answer = contact.Answer};
        _faqRepository.Add(c);
        return RedirectToAction("Index");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file containing some lines of code followed by a string pattern.
A rookie here. I have this specific issue in implementing Model View Presenter Pattern
I have followed this guide: http://maven.apache.org/guides/plugin/guide-java-plugin-development.html I have created a maven-plugin project hello-maven-plugin with
I have followed this guide to install Solr in TomCat running on Windows Server
Hi have followed this tutorial using Core plot framework http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application My project compiles, but
I have followed this tutorial which allowed me to create a Silverlight DataGrid that
I'm working on a big ASP.NET MVC site, a commerce service sold to customers.
I am creating a HelloWorld web/spring application from scratch. I have followed this tutorial
I have found this tutorial online http://net.tutsplus.com/tutorials/php/creating-a-php5-framework-part-1/ I have created myself a simple sort
If I have HTML like this: <dsometext<f<legit></legit> whatever What regex pattern do I use

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.