{"id":251,"date":"2010-06-09T08:02:46","date_gmt":"2010-06-09T13:02:46","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=251"},"modified":"2010-06-09T08:02:46","modified_gmt":"2010-06-09T13:02:46","slug":"nhibernate-and-auto-properties","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2010\/06\/09\/nhibernate-and-auto-properties\/","title":{"rendered":"NHibernate and Auto Properties"},"content":{"rendered":"<p>I&#8217;ve been working through the <a href=\"http:\/\/www.amazon.com\/gp\/product\/B0036V7H5G?ie=UTF8&#038;tag=timhoosblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=B0036V7H5G\">NHibernate with ASP.NET ProblemDesignSolution (Wrox Blox)<\/a><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.assoc-amazon.com\/e\/ir?t=timhoosblo-20&#038;l=as2&#038;o=1&#038;a=B0036V7H5G\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/>, with some small changes.  I&#8217;m writing my sample in C# using the .Net framework 3.5.  I prefer to use auto-properties.  <\/p>\n<p>It&#8217;s common that fields have private setters and only nhibernate can map using the backing field (set via reflection).  <\/p>\n<pre>\r\n<code>public String City { get; private set; }<\/code>\r\n<\/pre>\n<p>\nThe problem is telling NHibernate how to find the backing field.  When you have an explicit field, you end up with something like:<\/p>\n<pre>\r\n<code>&lt;property access=\u201dfield.camelcase-underscore\u201d name=\u201dCity\u201d \/&gt;<\/code>\r\n<\/pre>\n<p>\nBut with no backing field, that is a problem.  I started looking around on the net and found the following:\n<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/1491307\/mapping-private-set-using-nhibernate\">StackOverflow discussion without much good info<\/a><br \/>\nand <a href=\"http:\/\/weblogs.asp.net\/sfeldman\/archive\/2008\/02\/29\/c-3-0-auto-property-and-nhibernate.aspx\">a blog post with an interesting response from Ayende Rahien.<\/a><\/p>\n<p>Ayende is saying to not worry about the issue, NHibernate will still be able to set via reflection.  That&#8217;s fine, but it feels a little &#8220;magical&#8221;, especially to a new developer coming along.  <\/p>\n<p>Why not set protected?  It&#8217;s not unreasonable to expect a maintaining developer to understand that NHibernate sub-classes your class.<\/p>\n<pre>\r\n<code>public String City { get; protected set; }<\/code>\r\n<\/pre>\n<p>\nNow the mapping stays simple too:<\/p>\n<pre>\r\n<code>&lt;property name=\u201dCity\u201d \/&gt;<\/code>\r\n<\/pre>\n<p>\nAnd there is no need to expect a maintaining developer to know that reflection magic is setting the property.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working through the NHibernate with ASP.NET ProblemDesignSolution (Wrox Blox), with some small changes. I&#8217;m writing my sample in C# using the .Net framework 3.5. I prefer to use auto-properties. It&#8217;s common that fields have private setters and only nhibernate can map using the backing field (set via reflection). public String City { get; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,18,24],"tags":[105,69,103,104],"class_list":["post-251","post","type-post","status-publish","format-standard","hentry","category-aspnet","category-programming","category-web","tag-c","tag-linkedin","tag-nhibernate","tag-reflection"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}