NHibernate/ActiveRecord – Constructor Mapping Support?

(update: I was pretty tired when I originally wrote this post and I don’t think I expressed myself very well. I hope the updated version reads better.)Why doesn’t NHibernate/ActiveRecord support mapping with a public constructor? Currently there are two ways in which NHibernate can inject data into your class.

  • Setting your instance variables directly
  • Calling a “set” method on your class.

I want a third way.

  • Calling a public constructor.

Why would I want yet another way to map data into a model class? Mapping to a constructor would allow us to truly create “Idiomatic” .NET objects. With the current mapping options, artifacts of database mapping litter our model classes.Examples of litter:

  • default/no argument constructors who’s only purpose is to allow NHibernate to create an instance of your class
  • private/protected/internal/public properties who’s only purpose is to allow NHibernate to inject data into our objects.

Litter is not a huge price to pay for everything that NHibernate gives us, but the litter could be eliminated completely by allowing mapping to a constructor.Constructor mapping could be achieved using a syntax similar to the following:

public Address([Field] string street,               [Field] string city,               [Field] string state,               [Field] string zip){    this.street = street;    this.city = city;    this.state = state;    this.zip = zip;}

Or something similar to this:

[Constructor]public Address(string street,               string city,               string state,               string zip){    this.street = street;    this.city = city;    this.state = state;    this.zip = zip}

Has mapping to public constructor ever been proposed? Is there a reason for not mapping allowing mapping to a constructor?

Advertisements

5 Responses

  1. I think the new version of NH will support protected parameterless constructors so AR should as well.

  2. I updated the post to better reflect my thoughts. Given your response, I don’t think I was very clear. Sorry.

  3. Do you mean you would prefer the mapping to be declared in the constructor rather than putting AR attributes on the required properties? Sorry, I’m not sure you what you meant, but it sounded interesting.

  4. Adam,

    I’m experiencing exactly the same issue as you’ve described.

    In some cases, I want to call some doStuff() private method from my constructor, but the current format doesn’t allow for this.

    Do you know if there’s been any further suggestions for this?

  5. @Artie

    I have not seen any.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: