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

  • Home
  • SEARCH
  • 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 4092788
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:32:09+00:00 2026-05-20T19:32:09+00:00

Don’t get me wrong, I want them to get saved. But I always thought

  • 0

Don’t get me wrong, I want them to get saved. But I always thought I had to call Session.SaveOrUpdate(x); to do it. But here is what is going on.

Basic Question

Objects are getting ‘saved’ despite me never calling a save method.

Details

Objects are mapped through Fluent nHibernate , nothing special here.

I am mapping my ISessionFactory and ISession through Ninject in ASP.NET MVC like this.

        Bind<ISessionFactory>()
            .ToMethod(c => CreateSessionFactory())
            .InSingletonScope();

        Bind<ISession>()
            .ToMethod(c => OpenSession())
            .InRequestScope()
            .OnActivation(session =>
            {
                System.Diagnostics.Debug.WriteLine("Creating Session");
                session.BeginTransaction();
                session.FlushMode = FlushMode.Commit;
            })
            .OnDeactivation(session =>
            {
                if (session.Transaction.IsActive)
                {
                    try
                    {
                        System.Diagnostics.Debug.WriteLine("Disposing Session");

                        session.Transaction.Commit();
                    }
                    catch
                    {
                        session.Transaction.Rollback();
                    }
                }
            });

Then, when I want to perform something, I have either a simple method on the domain class, or an extension method (simply for the fact that it is easier to organize), like this.

class Member {
  public virtual int Id { get; set; }
  public virtual string Email { get; set; }
  public virtual string Password { get; set; }
}

Then I pepper in some extension methods for flavor..

using System;
using System.Linq;

namespace System.Linq
{
    public static class  MembershipExtensions
    {
        public static void ChangePassword(this Membership.Member member, string password)
        {
            member.Password = System.Security.Cryptography.Hashing.ComputeHash(password, "SHA512", null);
        }

        public static bool VerifyPassword(this Membership.Member member, string password)
        {
            return System.Security.Cryptography.Hashing.VerifyHash(password, "SHA512", member.Password);
        }
    }
}

So far, all is good in the land of Ooo. Now here is where stuff gets wonky.

    [HttpPost]
    public ActionResult Password(ViewModels.MemberEditPasswordViewModel model)
    {
        if (ModelState.IsValid)
        {
            // attempt to perform the password change on the current member. 
            var member = User.AsMember(); // retrieve the current user

            if (member.VerifyPassword(model.Current)) // verify the current password
                member.ChangePassword(model.Password);

            //if (Bus.Update(member) != null) {
            //    return RedirectToAction("Index", "Home");
            //}
            //else {
            //    ModelState.AddModelError("", "The original password is incorrect.");
            //}
        }

        // If we got this far, something failed, redisplay the form
        // with the errors listed and give them a chance to try again.
        return View(model);
    }

Here’s the crazy part. This works. Notice what is so strange? I never call any kind of SaveOrUpdate to the Member object. I commented out my Update code just to test it out.

Additional Data

This is information that may be relevant to the question, but I didn’t want to bog down everything with extra code.

For those looking for my AsMember() method, here it is as well. I’m trying to give all the data I can. I like this behavior, but I’m not sure if it is normal, or correct…

using System;
using System.Linq;
using System.Linq.Expressions;

namespace System.Web.Mvc
{
    public static class MembershipProviderExtensions
    {
        public static Membership.Member AsMember(this System.Security.Principal.IPrincipal user)
        {
            // if the user is not authenticated, then we should
            // not have made it this far, and error out gracefully.
            if (!user.Identity.IsAuthenticated)
                return null;

            // get the service bus from the locator
            var bus = DependencyResolver.Current.
                GetService<IServiceBus>();

            // return the queried result
            return bus.Request<Membership.Queries.CurrentMember>(user);
        }
    }
}

The current member is simply fetched through a query object, sent to the IServiceBus. (This is a custom service bus, not an open source project one).

/// <summary>
/// Resolves the currently authenticated <see cref="Member"/>.
/// </summary>
public class CurrentMember : IRequestFor<System.Security.Principal.IPrincipal, Member>
{
    private readonly ISession session;

    public CurrentMember(ISession session) {
        this.session = session;
    }

    public Member Reply(System.Security.Principal.IPrincipal Model)
    {
        // if the user is not authenticated, then we should
        // not have made it this far, and error out gracefully.
        if (!Model.Identity.IsAuthenticated)
            return null;

        return session.Query<Member>()
            .Where(context => context.Email == Model.Identity.Name)
            .Take(1)
            .SingleOrDefault();
    }
}

Here is the Request<T> implementation..

    public dynamic Request<T>(dynamic message)
    {
        dynamic implementation = kernel.Get<T>();
        return implementation.Reply(message);
    }
  • 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-20T19:32:10+00:00Added an answer on May 20, 2026 at 7:32 pm

    You should set the session to FlushAction.Never

    Objects that are loaded using the session will flush at the disposable.

    Moreover, there is the bonus performance boost since the session does not need to check the state of all the loaded objects.
    This is significant in scenarios where a lot of data is loaded.

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

Sidebar

Related Questions

I don't want PHP errors to display /html, but I want them to display
Don't ask me how but I managed to get accidentally the following remote branches
I don't like Jackson. I want to use ajax but with Google Gson. So
I don't remember whether I was dreaming or not but I seem to recall
I don't expect a straightforward silver bullet answer to this, but what are the
I don't want to take the time to learn Obj-C. I've spent 7+ years
I don't know if anyone has seen this issue before but I'm just stumped.
Don't ask why, but is there any way to suppress a failed linking error?
Don't need to do this right now but thinking about the future... What would
Don't want to sort the entries. using this does not preserve the order as

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.