如何在nhibernate中映射一个接口? [英] How to map an interface in nhibernate?

查看:152
本文介绍了如何在nhibernate中映射一个接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了两个类 NiceCustomer & RoughCustomer 这暗示了接口 ICustomer



code> ICustomer 有四个属性。它们是:
$ b


  1. 属性Id()作为整数

  2. 属性名称()作为字符串

  3. 属性IsNiceCustomer() c $ c $ b $ li $ $ c $ ReadOnly Property AddressFullText()As String

我不知道如何将接口 ICustomer 映射到数据库。



我在内部异常中得到这样的错误。


关联指的是一个未映射的类:ICustomer


我正在使用Fluent和NHibernate。

解决方案

您可以通过在配置阶段插入EmptyInterceptor直接映射到NHibernate中的接口。这个拦截器的工作是为你在映射文件中定义的接口提供实现。

  public class ProxyInterceptor:EmptyInterceptor 
public ProxyInterceptor(ITypeHandler typeHandler){
// TypeHandler是一个定义所有Interface / Poco关系的自定义类
//应该写成与您的系统匹配


//为实现交换接口
public override object实例化(string clazz,EntityMode entityMode,object id)
{
var handler = TypeHandler.GetByInterface clazz所);
if(handler == null ||!handler.Interface.IsInterface)return base.Instantiate(clazz,entityMode,id);
var poco = handler.Poco;
if(poco == null)return base.Instantiate(clazz,entityMode,id);

//返回Poco为接口
var instance = FormatterServices.GetUninitializedObject(poco);
SessionFactory.GetClassMetadata(clazz).SetIdentifier(instance,id,entityMode);

返回实例;





$ p
$ b $ p $之后,所有的关系和映射可以定义为接口。

  public父类:IParent {
public int ID {get;组; }
public string Name {get;组; }
public IChild Child {get;组; }
}

public Child:IChild {
public int ID {get;组; }
public string Name {get;组; }
}

公共类ParentMap:ClassMap< IParent>
{
public ParentMap()
{
Id(x => x.ID).GeneratedBy.Identity()。UnsavedValue(0);
Map(x => x.Name)
}
}

...

如果要实现ORM的真正解耦,将所有配置/映射放置在单独项目中并仅引用接口,则此类技术非常有用。然后,您的域图层不会被ORM污染,如果需要,您可以在稍后阶段将其替换。


I'm using two class NiceCustomer & RoughCustomer which implment the interface ICustomer.

The ICustomer has four properties. They are:

  1. Property Id() As Integer
  2. Property Name() As String
  3. Property IsNiceCustomer() As Boolean
  4. ReadOnly Property AddressFullText() As String

I don't know how to map the interface ICustomer, to the database.

I get an error like this in the inner exception.

An association refers to an unmapped class: ICustomer

I'm using Fluent and NHibernate.

解决方案

You can map directly to interfaces in NHibernate, by plugging in an EmptyInterceptor during the configuration stage. The job of this interceptor would be to provide implementations to the interfaces you are defining in your mapping files.

public class ProxyInterceptor : EmptyInterceptor
{
    public ProxyInterceptor(ITypeHandler typeHandler) {
        // TypeHandler is a custom class that defines all Interface/Poco relationships
        // Should be written to match your system
    }

    // Swaps Interfaces for Implementations
    public override object Instantiate(string clazz, EntityMode entityMode, object id)
    {
        var handler = TypeHandler.GetByInterface(clazz);
        if (handler == null || !handler.Interface.IsInterface) return base.Instantiate(clazz, entityMode, id);
        var poco = handler.Poco;
        if (poco == null) return base.Instantiate(clazz, entityMode, id);

        // Return Poco for Interface
        var instance = FormatterServices.GetUninitializedObject(poco);
        SessionFactory.GetClassMetadata(clazz).SetIdentifier(instance, id, entityMode);

        return instance;
    }

}

After this, all relationships and mappings can be defined as interfaces.

public Parent : IParent {
    public int ID { get; set; }
    public string Name { get; set; }
    public IChild Child { get; set; }
}

public Child : IChild {
    public int ID { get; set; }
    public string Name { get; set; }
}

public class ParentMap : ClassMap<IParent>
{
    public ParentMap()
    {
        Id(x => x.ID).GeneratedBy.Identity().UnsavedValue(0);
        Map(x => x.Name)
    }
}   

...

This type of technique is great if you want to achieve true decoupling of your ORM, placing all configuration/mappings in a seperate project and only referencing interfaces. Your domain layer is then not being polluted with ORM, and you can then replace it at a later stage if you need to.

这篇关于如何在nhibernate中映射一个接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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