Fluent NHibernate, working with interfaces

前端 未结 3 646
自闭症患者
自闭症患者 2020-12-01 05:23

I just switched to Fluent NHibernate and I\'ve encountered an issue and did not find any information about it.

Here\'s the case :

public class Field          


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 05:51

    I find that there are valid reasons for using an interface instead of a concrete class as a property.

    For example, if your Field class was in a seperate project to the Address class, and you didn't have a dependency on the Address class's project from the Field class's project.

    There are other ways of dealing with this situation, but the simplest way is often to attempt what you are doing and explicity tell NHibernate the concrete class to use for IAddress.

    You can now do this in Fluent NHibernate, like this:

    References(x => x.Address, "AddressId")
        .Class(typeof(Address);
    

    Unfortunately you can't do this with HasMany or HasManyToMany. I'm not sure if this would even be possible due to lack of good covariance support in C#.

提交回复
热议问题