0

Please see the domain object below:

public Class Person
{
    public virtual Guid Id { get; protected set; }
    public virtual string FirstName { get; protected set; }
    public virtual string Surname { get; protected set; }
    public virtual System.DateTime DateOfBirth { get; protected set; }

        //Domain methods are here.

}

and the NHibernate mappings below:

public class PersonMap : ClassMapping<Person>
{
    public PersonMap()
    {
        Id<Guid>(x => x.Id);
        Property<string>(x => x.FirstName);
        Property<string>(x => x.Surname);
        Property<DateTime>(x => x.DateOfBirth);
    }

}

This works as expected. Say I wanted to change the domain model to this:

public Class Person
{
    public virtual Guid Id { get; protected set; }
    public virtual FirstName FirstName { get; protected set; }
    public virtual Surname Surname { get; protected set; }
    public virtual DateOfBirth DateOfBirth { get; protected set; }
}

Notice the primitive types are replaced with objects. The reason I am doing this is to remove primitive obsession as described here: http://enterprisecraftsmanship.com/2015/03/07/functional-c-primitive-obsession/

I have read the documentation here (page 144): http://stc.sbu.ac.ir/AdminTools/Docs/Files/nhibernate_reference.pdf. It is telling me to introduce a custom type. I have also read this question: nHibernate mapping to custom types. I am still struggling to do this with NHibernate code mapping and hence the reason for the question.

0

2 Answers 2

1

Have you looked at mapping them via components?

This might be an easier starting point, compared with a custom type, if your data's in the same table.

Sign up to request clarification or add additional context in comments.

2 Comments

Before I mark your answer. Do you know if it is possible to map an NHibernate field to a class constructor instead of a class property (I have used a class property in my answer below)?
Not sure about that. If you look here (nhibernate.info/doc/nh/en/…) you can see the access strategies available. It doesn't look like the constructor is supported.
0

Following on from David Osbournes comment; the answer was to do this:

Component(x => x.FirstName, y =>
                {
                    y.Property<string>(z => z.FirstName);
                });

NHibernate using the FirstName property of a class to map to the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.