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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:57:34+00:00 2026-05-11T09:57:34+00:00

I have a person entity containing an Address as a value object: public Person()

  • 0

I have a person entity containing an Address as a value object:

public Person() {     WithTable('Person');     Id(x => x.Id);     Component<Address>(x => x.Address, a =>     {         a.Map(x => x.Address1);         a.Map(x => x.Address2);         a.Map(x => x.Address3);         a.Map(x => x.Town);         a.Map(x => x.Postcode);     }); } 

It states in the NHibernate docs that if all the properties of a value object (Address1, Address2 etc) are null, the entire component will be mapped as null (i.e. Person.Address will be null). This is giving me problems in cases where all address fields are null because in my pages where I might have (I’m doing ASP MVC):

<%= Html.TextBoxFor((x => x.Address.Address1))%> 

This breaks with a null reference exception. So I’m looking for a clean way to set Address as a new Address() object rather than null if all the fields are empty when I load a Person from the database without doing it manually. I’ve discounted the following ideas:

Doing null checking in my view (yuk, horrible)

Making database fields not-nullable (I’m dealing with a legacy database)

Anyone any ideas?

  • 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. 2026-05-11T09:57:34+00:00Added an answer on May 11, 2026 at 9:57 am

    I don’t have any definitive answers creating a method/property accessor that isn’t mapped, and that returns a default/null-object if the actual address is null.

    public Address GetAddressOrDefault() {   return Address ?? new NullAddress(); } 

    Or similar to the first, create a wrapper for your Address that you use in the view.

    public class AddressViewData {   private Address address;    public AddressViewData(Address address)   {     this.address = address ?? new NullAddress();   }    // expose all address properties as pass-throughs   public string Street   {     get { return address.Street; }   } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Person and an Organisation Entity: Person looks like this: public class
Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
We have an entity (class) containing data and methods (lets call it Person). There
I am trying to map an Address entity and a Person entity using a
I have an entity Person: public class Person { public virtual int Id {get;
I have Person entity which has composition with Location Entity @ManyToOne(fetch = FetchType.EAGER, cascade
In my core data model I have a Person entity that has a to
I have a Person object with two constructors - one takes an int (personId),
I have a Person object with a Nullable DateOfBirth property. Is there a way
I am using VS 2010 and I have an entity 'Person'. The pluralization-service turns

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.