WorldIsRound
WorldIsRound

Reputation: 1554

Fluent NHibernate mapping for read only properties

I have recently migrated to Fluent NHibernate 1.1 from 1.0 and the migration has some breaking changes.

For instance, fields in domain model like

    public virtual string CustomerType
    {
        get { return /*computed value based on _Type which is a column in database */; }

    }

    public virtual string MemberType
    {
        get { return _Type; }
        set { _Type = value; }

    }

used to work fine without specifying any configuration/convention. Now it throws an error stating "Could not find setter". I see solutions where one creates a member variable such as customerType or _customerType or for that matter putting in a protected setter.

Note that the CustomerType is dependent on another value retrieved from database.

I have also seen alternatives like http://support.fluentnhibernate.org/discussions/help/269-fluentnhibernate-11-automapper-doesnt-accept-read-only-properties-anymore where the DefaultAutoMappingConfiguration is overridden such as

   public override bool ShouldMap(Member member)
    {
        if (member.IsProperty && !member.CanWrite)
        {
            return false;
        }

        return base.ShouldMap(member);
    }

But this means other fields with private or protected setters are altogether skipped in the mapping.

I am looking for a solution where Fluent NHibernate does not look for setter when it is not specified but maps private/protected setters.

Any directions on how to go about with this?

Upvotes: 1

Views: 1807

Answers (2)

Vadim
Vadim

Reputation: 17957

If this is the only mapping or one of the few that breaks then manually ignore it/map it with a no setter mapping. Otherwise turn off mapping non writeable properties as the suggestion you listed and map any exceptions. Not sure you can do much else, short of forking the code yourself and fixing the problem.

UPDATE I just ran a test and PropertyInfo.CanWrite returns true for protected and private setters. So I'm not sure there really is a problem with the solution you posted already.

Upvotes: 1

Bronumski
Bronumski

Reputation: 14272

Does the CustomerValue come out of the database as it is a derived value?

If it doesn't remove the virtual and exclude it in the mapping.

Upvotes: 1

Related Questions