NHibernate 属性访问策略,如何设置以便首选 field.camelcase-underscore 但 autoproperty 会回退

发布于 2024-08-19 19:18:16 字数 116 浏览 2 评论 0原文

我想设置访问策略,以便如果存在 field.camelcase-underscore 属性支持,则使用该属性,否则使用自动属性。

这是默认行为(因为自动道具本质上具有后场)吗?或者我如何强制执行此操作?

I want to set up the access strategy so that if a field.camelcase-underscore property backing is present then use that else use the automatic property.

is this the default behavior (since auto props have back fields essentially)? or how to I enforce this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未央 2024-08-26 19:18:16

默认情况下是使用属性的设置器,因此如果您有支持字段,则需要将访问指定为驼峰式下划线字段(或您使用的任何命名约定)。

可能有一种更简单的方法来实现此目的,但您可以使用 Fluent NHibernate 的约定来强制使用支持字段(如果可用)的行为,否则使用设置器。应用约定时,您可以反映实体类型以检查是否存在相应的驼峰式下划线字段。如果找到支持字段,则修改映射以使用驼峰下划线作为访问。

以下是使用 IPropertyConvention 的示例。 (您可能还想在一对多约定等中进行相同类型的检查):

public class PropertyAccessConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        Type entityType = instance.EntityType;
        string camelCaseUnderscoreName = 
            ConvertToCamelCaseUnderscore(instance.Name);

        bool hasBackingField = HasField(entityType, camelCaseUnderscoreName);

        // Default is to use property setter, so only modify mapping
        // if there is a backing field

        if (hasBackingField)
            instance.Access.CamelCaseField(CamelCasePrefix.Underscore);
    }

    private static string ConvertToCamelCaseUnderscore(string propertyName)
    {
        return "_" +
            propertyName[0].ToString().ToLower() +
            propertyName.Substring(1);
    }

    private bool HasField(Type type, string fieldName)
    {
        FieldInfo backingField = type.GetField(
            fieldName, 
            BindingFlags.NonPublic | BindingFlags.Instance);

        return backingField != null;
    }
}

The default is to use the setter of the property, so you need to specify access as camelcase underscore field (or whatever naming convention you use) if you have a backing field.

There might be a simpler way to achieve this, but you can use Fluent NHibernate's conventions to enforce this behaviour of using backing fields if available, and setters otherwise. When the convention is applied you can reflect over the entity type to check if there is a corresponding camelcase underscore field or not. If a backing field is found, you modify the mapping to use camelcase underscore as access.

Here is an example using IPropertyConvention. (You might want to do the same kind of check in a one-to-many convention etc as well):

public class PropertyAccessConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        Type entityType = instance.EntityType;
        string camelCaseUnderscoreName = 
            ConvertToCamelCaseUnderscore(instance.Name);

        bool hasBackingField = HasField(entityType, camelCaseUnderscoreName);

        // Default is to use property setter, so only modify mapping
        // if there is a backing field

        if (hasBackingField)
            instance.Access.CamelCaseField(CamelCasePrefix.Underscore);
    }

    private static string ConvertToCamelCaseUnderscore(string propertyName)
    {
        return "_" +
            propertyName[0].ToString().ToLower() +
            propertyName.Substring(1);
    }

    private bool HasField(Type type, string fieldName)
    {
        FieldInfo backingField = type.GetField(
            fieldName, 
            BindingFlags.NonPublic | BindingFlags.Instance);

        return backingField != null;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文