流畅的 NHibernate,使用接口 [英] Fluent NHibernate, working with interfaces

查看:15
本文介绍了流畅的 NHibernate,使用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚切换到 Fluent NHibernate,但遇到了一个问题,但没有找到任何相关信息.

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

情况如下:

public class Field : DomainObject, IField
{
    public Field()
    {  
    }

    public virtual string Name { get; set; }
    public virtual string ContactPerson { get; set; }
    public virtual bool Private { get; set; }
    public virtual IAddress Address { get; set; }  
}

IAddress 是一个名为 Address 的类实现的接口

IAddress is an interface implemented by a class named Address

public class Address : DomainObject, IAddress
{
    public Address()
    {
    }

    public virtual string City { get; set; }
    public virtual string Country { get; set; }
    public virtual string PostalCode { get; set; }
    public virtual string StreetAddress { get; set; }
}

这是我的两个类的映射文件

Here's my mapping files for both classes

地址

public class AddressMap : ClassMap<Address>
{   
    public AddressMap()
    {
        WithTable("Addresses");
        Id(x => x.Id, "Id").Access.AsCamelCaseField(Prefix.Underscore).GeneratedBy.Guid();
        Map(x => x.City, "City");
        Map(x => x.Country, "Country");
        Map(x => x.PostalCode, "PostalCode");
        Map(x => x.StreetAddress, "StreetAddress");
    }
}

字段

public class FieldMap : ClassMap<Field>
{
    public FieldMap()
    {
        WithTable("Fields");
        Id(x => x.Id, "Id").Access.AsCamelCaseField(Prefix.Underscore).GeneratedBy.Guid();
        Map(x => x.Name, "Name");
        Map(x => x.ContactPerson, "ContactPerson");
        Map(x => x.Private, "Private");
        References(x => x.Address, "AddressId").Cascade.Delete().Cascade.SaveUpdate();
    }
}

因此,当我尝试从我的数据库中检索字段对象时,我收到一个 NHibernate 错误,指出 IAddress 未映射.有什么办法可以指定NHibernate在映射中使用Address类吗?

So when I tried to retrive a field object from my database, I get an NHibernate error that states that IAddress is not mapped. Is there any way to specify to NHibernate to use the Address class in the mapping?

如果需要更多信息,请告诉我.

Please let me know if more information are needed.

非常感谢,

查尔斯

推荐答案

我发现使用接口而不是具体类作为属性是有正当理由的.

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

例如,如果您的 Field 类位于与 Address 类不同的项目中,并且您没有从 Field 类的项目对 Address 类的项目的依赖.

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.

还有其他方法可以处理这种情况,但最简单的方法通常是尝试您正在执行的操作并明确告诉 NHibernate 用于 IAddress 的具体类.

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.

您现在可以在 Fluent NHibernate 中执行此操作,如下所示:

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

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

不幸的是,你不能用 HasMany 或 HasManyToMany 来做到这一点.由于 C# 中缺乏良好的协方差支持,我不确定这是否可行.

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#.

这篇关于流畅的 NHibernate,使用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆