Friday, February 29, 2008 8:45 PM Sean Feldman

C# 3.0 Auto Property And NHibernate

I was reading about NHibernate mapping of the properties that have no setter, and how it's done through the backing field (reflection I assume). The setting looks like this:

<property name="Name" 
          access="nosetter.camelcase-underscore" 
          column="name" type="string" length="20" />

And then I got curious what would be that with the new C# 3.0 auto properties.

As a test I made a simple class with a simple string property that looks like this:

      private string test;
      
      public string Test
      {
        get { return test; }
        private set { test = value; }
      }

And with auto property this looks much sexier:

      public string Test { get; set; }

But what about backing field? Reflector shows the next:

    [CompilerGenerated]
    private string <Test>k__BackingField;
 
    public string Test 
    { 
         [CompilerGenerated] get; 
         private[CompilerGenerated] set; 
    }
 
t

Now I was wandering how  that should be communicated in the mapper file for NHibernate?... Googled.... nothing. Any ideas?

Filed under:

Comments

# re: C# 3.0 Auto Property And NHibernate

Friday, February 29, 2008 11:06 PM by Aaron Lerch

Just curious - why would you use an automatic property that has no setter? How is that useful at all? :)

# re: C# 3.0 Auto Property And NHibernate

Friday, February 29, 2008 11:35 PM by Sean Feldman

@Aaron,

my bad, I updated the code. The setter with auto property can be private. That's what i was trying to say. I guess the real question is how to configure the NHibernate mapping to use backing fields that are autogenerated by .NET

# re: C# 3.0 Auto Property And NHibernate

Saturday, March 01, 2008 4:56 AM by Colin Ramsay

You can specify a custom class for your access strategy, which might help if you could determine the means by which the backing field is generated.

Do you need to further update your code sample? How does:

public string Test { get; set; }

Represent a private setter?

# re: C# 3.0 Auto Property And NHibernate

Saturday, March 01, 2008 10:18 AM by Ayende Rahien

You can probably just leave it mapped as a property.

NHibernate in general would ignore visibility.

# re: C# 3.0 Auto Property And NHibernate

Saturday, March 01, 2008 11:08 AM by Sean Feldman

@Colin,

the code is updated, took time to refresh.

@Ayende,

thank you for bringing that to my knowledge.

# NHibernate and Auto Properties &laquo; Tim Hoolihan

Wednesday, June 09, 2010 9:02 AM by NHibernate and Auto Properties « Tim Hoolihan

Pingback from  NHibernate and Auto Properties &laquo;  Tim Hoolihan