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,贝里尔

4

3 回答 3

1

.WithLengthOf()Action<XmlElement>在生成 XML 映射时应用的更改列表中添加一个“更改”( )。不幸的是,该字段是private并且没有访问更改列表的属性,所以恐怕(目前)没有办法检查属性映射是否已WithLengthOf应用于它。

于 2009-04-30T16:48:52.537 回答
1

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

于 2009-05-01T19:30:06.977 回答
0

为了在代码中阐明 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);
    }
}
于 2009-07-06T16:26:58.627 回答