流畅的 NHibernte 变更/约定

发布于 2024-07-17 15:06:29 字数 875 浏览 6 评论 0原文

我喜欢我在这篇博文中看到的模式(http://marekblotny .blogspot.com/2009/04/conventions-after-rewrite.html),作者在应用约定之前检查是否已进行表名更改。

public bool Accept(IClassMap target)
{
    //apply this convention if table wasn't specified with WithTable(..) method
    return string.IsNullOrEmpty(target.TableName);
}

我用于字符串长度的约定接口是 IProperty:

public class DefaultStringLengthConvention: IPropertyConvention
{
    public bool Accept(IProperty property) {
        //apply if the string length hasn't been already been specified
        return ??; <------ ??
    }

    public void Apply(IProperty property) {
        property.WithLengthOf(50);
    }
}

我没有看到 IProperty 在哪里公开任何告诉我该属性是否已设置的信息。 这可能吗?

TIA, 贝里尔

I like the pattern I saw in this blog post (http://marekblotny.blogspot.com/2009/04/conventions-after-rewrite.html), where the author is checking to see if a table name alteration has already been made before applying a convention.

public bool Accept(IClassMap target)
{
    //apply this convention if table wasn't specified with WithTable(..) method
    return string.IsNullOrEmpty(target.TableName);
}

The convention interface I'm using for a string length is IProperty:

public class DefaultStringLengthConvention: IPropertyConvention
{
    public bool Accept(IProperty property) {
        //apply if the string length hasn't been already been specified
        return ??; <------ ??
    }

    public void Apply(IProperty property) {
        property.WithLengthOf(50);
    }
}

I don't see where IProperty exposes anything that tells me if the property has already been set. Is this possible?

TIA,
Berryl

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

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

发布评论

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

评论(3

帝王念 2024-07-24 15:06:29

.WithLengthOf() 将“更改”(Action) 添加到生成 XML 映射时要应用的更改列表中。 不幸的是,该字段是 private 并且没有属性可以访问更改列表,因此恐怕(当前)无法检查属性映射是否具有 WithLengthOf应用于它。

.WithLengthOf() adds an "alteration" (Action<XmlElement>) to the list of alterations to apply when the XML mapping is generated. Unfortunately, that field is private and there is no property to access the alteration list, so I'm afraid there is (currently) no way to check if a property map has had WithLengthOf applied to it.

君勿笑 2024-07-24 15:06:29

在出现更好的替代方案之前,您可以使用 HasAttribute("length")

Until a better alternative comes along, you can use HasAttribute("length").

策马西风 2024-07-24 15:06:29

为了在代码中阐明 Stuart 和 Jamie 所说的内容,以下是有效的方法:

public class UserMap : IAutoMappingOverride<User>
{
    public void Override(AutoMap<User> mapping) {
        ...
        const int emailStringLength = 30;
        mapping.Map(x => x.Email)
            .WithLengthOf(emailStringLength)                        // actually set it
            .SetAttribute("length", emailStringLength.ToString());  // flag it is set
        ...

    }
}

public class DefaultStringLengthConvention: IPropertyConvention
{
    public bool Accept(IProperty property) {
        return true;
    }

    public void Apply(IProperty property) {
        // only for strings
        if (!property.PropertyType.Equals(typeof(string))) return;

        // only if not already set
        if (property.HasAttribute("length")) return;

        property.WithLengthOf(50);
    }
}

To clarify in code what Stuart and Jamie are saying, here's what works:

public class UserMap : IAutoMappingOverride<User>
{
    public void Override(AutoMap<User> mapping) {
        ...
        const int emailStringLength = 30;
        mapping.Map(x => x.Email)
            .WithLengthOf(emailStringLength)                        // actually set it
            .SetAttribute("length", emailStringLength.ToString());  // flag it is set
        ...

    }
}

public class DefaultStringLengthConvention: IPropertyConvention
{
    public bool Accept(IProperty property) {
        return true;
    }

    public void Apply(IProperty property) {
        // only for strings
        if (!property.PropertyType.Equals(typeof(string))) return;

        // only if not already set
        if (property.HasAttribute("length")) return;

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