简体   繁体   English

使用Fluent NHibernate覆盖C#属性访问器方法的基本行为

[英]Overriding the base behavior of a C# property accessor method with Fluent NHibernate

I have a bunch of C# classes which are inheriting a number of properties from an abstract base class. 我有一堆C#类,它们从一个抽象基类继承了许多属性。 All of the types are mapped to a database model using Fluent NHibernate, and all of the property definitions use automatic getters and setters (the standard "get; set;" syntax). 所有类型都使用Fluent NHibernate映射到数据库模型,并且所有属性定义都使用自动getter和setter(标准的“get; set;”语法)。 I recently found a need to provide a specific implementation for one of the accessor methods for a base type property on one of my derived types. 我最近发现需要为我的一个派生类型的基本类型属性提供一个访问器方法的特定实现。 So I created an explicit backing field for the base class property: 所以我为基类属性创建了一个显式的后备字段:

public abstract class BaseEntity : IBaseEntity
{
    protected bool active_field;

    ...

    public virtual bool active { get { return active_field; } set { active_field = value; } }

    ...

}

And then defined the specific getter logic for the "active" property in the derived type definition: 然后在派生类型定义中为“active”属性定义特定的getter逻辑:

public override bool active
    {
        get { return active_field && (this.Expiration == null || this.Expiration < DateTime.Now); }
        set { active_field = value; }
    }

When I fire up the project, though, NHibernate throws an exception: 但是,当我启动项目时,NHibernate会抛出异常:

An exception of type 'System.InvalidOperationException' occurred in FluentNHibernate.dll but was not handled in user code. FluentNHibernate.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理。 Additional information: Tried to add property 'active' when already added. 附加信息:尝试在已添加时添加属性“活动”。

I'm guessing this has something to do with NHibernate needing to provide it's own override for the property definitions (hence why it requires the properties be declared virtual in the first place), but I'm not all that NHibernate savvy. 我猜这与NHibernate有关,需要为属性定义提供它自己的覆盖(因此为什么它要求属性首先被声明为虚拟),但我并不是所有NHibernate的精明。 That being the case, I'm happy to provide any other details, but don't really know what else is relevant. 既然如此,我很乐意提供任何其他细节,但不知道还有什么相关的。 Is there an obvious reason why this can't work? 是否有一个明显的原因导致无法正常工作? If so, is there a simple workaround? 如果是这样,有一个简单的解决方法吗?

I am not exactly sure about the origin of your problem, but, trying to answer your question: 我不确定你的问题的根源,但是,试着回答你的问题:

  1. NHibernate needs all properties to be virtual to allow then to be lazy-loaded because it creates a proxy type of your entity type (which is an extension of your class). NHibernate需要所有属性都是虚拟的,以允许延迟加载,因为它创建了实体类型的代理类型(这是类的扩展)。 It needs them to be virtual so it can override them and inject a new behavior in it (fill them with the database value); 它需要它们是虚拟的,因此它可以覆盖它们并在其中注入新的行为(用数据库值填充它们);
  2. You do can map non-public properties and fields, but you would have to do this explicitly, follows an example on how to do it with FluentNHibernate. 您可以映射非公共属性和字段,但您必须明确地执行此操作,并按照如何使用FluentNHibernate执行此操作的示例。

Mapping a private property or field: 映射私有财产或字段:

public class EntityMap : ClassMap<Entity>
{
    Id(e => e.Id).GeneratedBy.Identity();
    Map(Reveal.Member<Entity>("PrivatePropertyName"));
}

Now a suggestion to try to solve your problem: you can hide the property on your extended class using the new keyword (the property still needs to be virtual ). 现在建议尝试解决您的问题:您可以使用new关键字隐藏扩展类的属性(属性仍然需要是虚拟的 )。

public virtual new bool active
{
    get { return active_field && (this.Expiration == null || this.Expiration < DateTime.Now); }
    set { active_field = value; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM