功能NHibernate,与工作接口 [英] Fluent NHibernate, working with interfaces

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

问题描述

我刚切换到流利的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是一类名为地址实现的接口

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();
    }
}

所以,当我试图从我的数据库retrive一个字段对象,我得到指出IAddress没有映射的NHibernate的错误。有什么办法来指定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类的项目地址类的项目的依赖。

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.

您现在就可以做到这一点的功能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天全站免登陆