Automapping Read Only Properties

1,132 views
Skip to first unread message

Mohamed Meligy

unread,
Apr 8, 2011, 2:46:24 AM4/8/11
to fluent-n...@googlegroups.com
I am trying to map read only properties (property with getter and no setter) via automapping.

Normally I'd expectthe default behavior of FluentNHibernate to map this directly as Access -> Read Only (since they don't even have private fields)
But it doesn't, and it's OK, because it's hard to tel whether I'm using a backing field or not, and also to assume that I don't.

Let's say I add 
 public virtual string DisplayTitle
 {
     get
     {
         return Name;
     }
 } 
To the Product class in FluentNHibernate source Automapping project.

Problem 1:: When running, I get an incomplete invalid or incomplete configuration as per: Could not find a setter for 'DisplayTitle'
...

The next thought I had was to use Conventions, I added a IPropertyConvention as:
public void Apply(IPropertyInstance instance)
{
    if(instance.Property.CanWrite == false)
    {
        instance.Access.ReadOnlyProperty();
    }
}
It seems to do something, since then the problem 

Problem 2:  When running, I get an incomplete invalid or incomplete configuration yet this time as per: 

{"could not find PropertyAccessor type: nosetter"} 

which gets the inner exception:

{"Could not load type nosetter. Possible cause: no assembly name specified.":""}

----------

If I try to use override normally at the specific property, it does work, but I want to do it as convention.

---------------------

Notes:

The reason I'm trying this is trying to make a FluentNHibernate version of the article:

http://jfromaniello.blogspot.com/2011/03/same-domain-with-conform-and-nhibernate.html

Jose sent me his tries with FNH and I tweaked them a bit, and currently the only problem that sounds extra work is this one.

I first tried it on the corresponding solution which used NHibernate 3.1 and FluentNHibernate 1.2 from NUGET,

 then when it didn't work, I tried to get latest DLLs from FluentNHibernate source, both "Master" and "1.x" branches.

Then to make sure it's not related to our solution I tried what I wrote here on the sample automapping project in FluentNHibernate source. I tried , both "Master" and "1.x" branches.after replacing the NHibernate references with the NHibernate 3.1 DLLs in the FluentnNHibernate source folder "Tools\NHibenrate\3.x".


Any idea how to fix problem 2 at least?


Thanks a lot.


Regards,


Mohamed Meligy
Readify | Senior Developer

M:+61 451 835006 | W: readify.net

Description: Description: Description: Description: rss_16  Description: Description: Description: Description: cid:image003.png@01CAF81D.6A076510  Description: Description: Description: Description: cid:image005.png@01CAF81D.6A076510

tclay

unread,
Apr 29, 2011, 1:46:54 PM4/29/11
to Fluent NHibernate
Have you tried using a protected empty setter?

public virtual string DisplayTitle
{
get
{
return Name;
}
protected set
{
;
}
}


On Apr 8, 12:46 am, Mohamed Meligy <eng.mel...@gmail.com> wrote:
> I am trying to map read only properties (property with getter and no setter)
> via automapping.
>
> Normally I'd expectthe default behavior of FluentNHibernate to map this
> directly as Access -> Read Only (since they don't even have private fields)
> But it doesn't, and it's OK, because it's hard to tel whether I'm using a
> backing field or not, and also to assume that I don't.
>
> Let's say I add
>  public virtual string DisplayTitle
>  {
>      get
>      {
>          return Name;
>      }
>  }
> To the Product class in FluentNHibernate source Automapping project.
>
> Problem *1*:: When running, I get an incomplete invalid or incomplete

Mohamed Meligy

unread,
Apr 30, 2011, 2:12:50 AM4/30/11
to fluent-n...@googlegroups.com
No, I didn't. Jose told me I should be able to do this and he is doing it in his confORM example.
 
Note that this different from the other "private setter" problem arising in .NET 3.2.
 
Anyway, after a chat with Jose we agreed this is fine to use in Override and this is how I completed the sample and put on
http://gurustop.net/blog/2011/04/17/nh-nhibernate-mapping-jose-romaniello-ef-conform-domain-using-fnh-fluentnhibernate/
 
 
Thanks a lot,
 
Regards,

Mohamed Meligy
Readify | Senior Developer

M:+61 451 835006 | W: readify.net

Description: Description: Description: Description: rss_16  Description: Description: Description: Description: cid:image003.png@01CAF81D.6A076510  Description: Description: Description: Description: cid:image005.png@01CAF81D.6A076510



--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.


Paul Batum

unread,
Apr 30, 2011, 3:27:53 AM4/30/11
to fluent-n...@googlegroups.com
The API might have changed a little but this blog post covers the approach that most people use:

James Gregory

unread,
May 1, 2011, 5:08:20 AM5/1/11
to fluent-n...@googlegroups.com
Automapping is really going to struggle to make sense of your DisplayTitle property. As it's a getter only, it'll try to find a backing field that matches; off the top of my head, I don't know what it'll do if it doesn't find one.

The Access.ReadOnlyProperty() method looks suspiciously broken. I'm under the impression you need to supply a "naming strategy" to go with nosetter, which that method definitely does not do. Try one of the ReadOnlyPropertyThroughXXX() methods instead. I think this is a badly implemented method.

Really though, I don't think this property should be mapped at all. It doesn't seem relevant to NHibernate. I'd use an override or change your automappingConfiguration to skip this member.
Reply all
Reply to author
Forward
0 new messages