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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:41:45+00:00 2026-05-12T11:41:45+00:00

Here is my scenario: I’m using nhibernate, openning and closing the session in an

  • 0

Here is my scenario:

I’m using nhibernate, openning and closing the session in an IHttpModule (PreRequestHandlerExecute and PostRequestHandlerExecute).

Ninject takes care of injecting my session in all my repositories and I’m very happy with it.

Now suppose an Update to one of my entities (code simplified):

Controller:

User user = _userService.GetUser(id);
user.Name = "foo";
user.Email = "foo@bar.com";
user.Group = _groupService.GetGroup(idGroup);
if(_userService.Edit(user)) {
   RedirectToAction("Index");
}
else {
   return View(user);
}

Service:

if(ValidateUser(user) {
   return _rep.Update(user);
}
return false;

ValidateUser is doing the validation logic and inserting any error in a IValidationDictionary that is a wrapper for the ModelState in the controller.

So far so good, I get all errors (if any) and I can show them in the view.


Here comes the problem:

When I try to save an user with an error (no name for instance), the method _rep.Update(user) is never called, but the user gets saved anyway.

Googling around it come to my knowledge the nhibernate AutoDirtyCheck, witch means that if I change an entity in memory, it will be persisted automatically in the database.

Very powerful feature I agree, but since my session is commited in PostRequestHandlerExecute, my invalid entity is saved anyway, something that I don’t want.

I tried to remove this behavior using unhaddins, it worked, but then my child objects are not automatically saved when I save only the parent 🙁


So how to solve that?

Make ValidadeUser public and validade a copy before calling the _userService.GetUser(id)?

Put the validation logic elsewhere? Maybe in the entity class itself? (I like it so much separated!).

Thanks a lot in advance.

  • 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-12T11:41:46+00:00Added an answer on May 12, 2026 at 11:41 am

    FYI – you can set the Nhibernate session FlushMode property to FlushMode.Never to be totally in control of when NHibernate will flush updates to the database. It might be that you could cheat and if an action is not authorized – never do a flush and the nhibernate session will die when the response is over (if the session didnt go away you really should evict the modified object, tho)

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

Sidebar

Related Questions

Let me try to set the scenario here first. This is done using ASP.NET
Tackling a strange scenario here. We use a proprietary workstation management application which uses
I know how to write tab host,but here the scenario is little bit different
here is the scenario 1: user starts to type some word, autocomplete engine shows
Here's my scenario - I have an SSIS job that depends on another prior
Here is the scenario: I'm writing an app that will watch for any changes
Here's the scenario: A C# Windows Application project stored in SVN is used to
Here is the scenario that I have. I have a cvs repository in one
Here's the scenario-- you've got a JSON object on the browser, and let's say
Here's the scenario: You have a Windows server that users remotely connect to via

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.