0

I'm trying to map an Id from an Identity class with Fluent NHibernate.

Identity class:

public interface IValueObject<T> {
    bool SameValueAs(T other);
}

[Serializable]
public class Identity<TEntity> : IValueObject<Identity<TEntity>> {

    public long Id { get; protected set; }

    public Identity(long id) {
        this.Id = id;
    }

    protected Identity() { }

    public bool SameValueAs(Identity<TEntity> other) {
        return other != null && this.Id == other.Id;
    }
}

Model:

public interface IEntity<T> {
    Identity<T> Identity { get; }
    bool SameIdentityAs(T other);
}

public class Employee: IEntity<Employee> {
    public virtual Identity<Employee> Identity { get; set; }
    public virtual string Name { get; set; }
}

How can I map this Employee? This way doesn't work, I get the following exception when building the SessionFactory: Could not find a getter for property 'Id' in class 'Employee'

public class EmployeeMap : ClassMap<Employee> {

    public EmployeeMap() {
        Id(x => x.Identity.Id).GeneratedBy.Native();
        Map(x => x.Name);
    }
}
Dani
  • 971
  • 12
  • 31

1 Answers1

0

What you are trying to do is not supported.

There is a longer explanation but, particularly when using DB-generated ids, you should use an unwrapped, native int or long.

That said, you can map the id as a private field in your entity and expose it with your wrapper. This still won't work on LINQ queries, so it's of limited value.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Is there any possibility using another id-strategy and the wrapper (Identity class)? – Dani Aug 17 '12 at 14:48
  • Maybe with `assigned` (i.e. your code is responsible for assigning it before save), mapping the wrapper as a composite id... Still, I think you're getting in a lot of trouble for something with dubious benefits. – Diego Mijelshon Aug 17 '12 at 16:58