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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:17:35+00:00 2026-05-18T03:17:35+00:00

I am using ASP.NET MVC 2 with nhibernate. I have this Sales class. Sales

  • 0

I am using ASP.NET MVC 2 with nhibernate. I have this Sales class. Sales has many Payments. Sales’ payments should not be modified once the sales’ status becomes confirmed. I need suggestions on how to enforce this.

I’m stumped on several things when trying to put this validations:

  • For adding and deleting payments:
    • I can create AddPayment and DeletePayment methods in Sales, but everybody must remember to use these instead of adding and deleting the payments collection directly
    • I don’t want to hide the payments collection because nhibernate needs it, also this collection is used in other parts of the software
  • For modifying existing payments:
    • I don’t think I should put the validation in the Payments setters because nhibernate needs to access the setters.
  • Should I throw exception? There’s a discussion about the disadvantages of throwing exception to prevent object entering invalid state. In my case object state after modification may still be valid, but I want to block the modification. Is throwing exception reasonable in this case? What are the alternatives?

Should I enforce this in the controller actions?

  • 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-18T03:17:36+00:00Added an answer on May 18, 2026 at 3:17 am

    You can hide the collection by making it protected or even private. NHibernate will still find it. Alternatively you could have the collection getter return an immutable collection when the sales status is the restricted value.

    private ISet<Payment> _payments;
    
    public virtual ISet<Payment> Payments
    {
        get
        {
            if (Status == SalesStatus.Confirmed)
                return new ImmutableSet<Payment>(_payments);
    
            return _payments;
        }
        private set { _payments = value; }
    }
    

    Putting validation rules in the setters is also fine. You can tell NHibernate to access the backing field directly (you may have to add a backing field if you’re currently using auto properties).

    <property name="_name" access="field"/>
    <property name="Description" access="nosetter.camelcase-underscore"/>
    

    Edit for the additional question…

    I don’t tend to throw exceptions until right before a save. Throwing them earlier, like in a property setter, can be annoying to UI developers who have to go back to the user with just one error at a time. I do mostly MVC applications too, so I’ve been using the System.ComponentModel.DataAnnotations validation attributes of late. While limited in some respects, they work well with MVC for the model checks at the browser and in the controller. If you use those, however, I recommend creating a custom interceptor to check them all right before a save. The interceptor is where I will throw an exception if anything is awry.

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

Sidebar

Related Questions

I have a scenario similar to this: Asp.NET MVC 4 website using nHibernate Session
I have a ASP.NET MVC application using NHibernate and the application runs fine when
I'm developing an application using asp.net mvc, NHibernate and DDD. I have a service
I have a asp.net-mvc website with a SQL server backend (using nhibernate for OR
I have a ASP.NET MVC 3 application using NHibernate over a PostgreSQL database. There
I am using NHibernate and ninject in ASP.Net MVC, using this page as a
I'm using S#arpArchitecture (ASP.NET MVC and Fluent NHibernate). I have an entity as follows:
I'm using Fluent NHibernate in an Asp.net MVC application. I have it set up
I have an asp.net-mvc website and i am using ninject for IOC and nhibernate
I'm using Ninject, Fluent NHibernate and ASP.NET MVC. Up until now everything has been

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.