列属性自动映射

发布于 2024-08-02 07:41:48 字数 2888 浏览 10 评论 0原文

可以为列命名创建约定:

我有这段代码:

public AutoPersistenceModel Generate()
{
    var result = AutoPersistenceModel.MapEntitiesFromAssemblyOf<User>()
        .Where(GetAutoMappingFilter)
        .WithConvention(GetConventions);

    return result;
}

private bool GetAutoMappingFilter(Type t)
{
    return
        t.GetInterfaces().Any(
            x => x.IsGenericType && (x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>)));
}


private static void GetConventions(Conventions conventions)
{
    conventions.GetPrimaryKeyNameFromType = type => type.Name.ToLower() + "_id";
    conventions.FindIdentity = type => type.Name.ToLower() == "id";
    conventions.GetTableName = type =>
        {
            if (!type.Name.Contains("Lookup"))
            {
                return Inflector.Net.Inflector.Pluralize(type.Name).ToLower();
            }
            return Inflector.Net.Inflector.Underscore(type.Name)
                .Replace("lookup", "lu").ToLower();
        };
    conventions.IsBaseType = DontMapAsJoinedSubclassTypesInheritedFrom;
    conventions.GetForeignKeyNameOfParent = type => Inflector.Net.Inflector.Underscore(type.Name) + "_id";
    conventions.GetForeignKeyName = type => Inflector.Net.Inflector.Underscore(type.Name) + "_id";
    conventions.OneToManyConvention = m => m.Cascade.All();

}


private static bool DontMapAsJoinedSubclassTypesInheritedFrom(Type arg)
{
    var derivesFromEntity = arg == typeof(Entity);
    var derivesFromEntityWithTypedId = arg.IsGenericType &&
        (arg.GetGenericTypeDefinition() == typeof(EntityWithTypedId<>));
    return derivesFromEntity || derivesFromEntityWithTypedId;
}

和一个实体类

public class Organisation : EntityWithTypedId<int>
{
    public virtual Organisation Parent { get; set; }
    public virtual LookOrganisationType OrganisationType { get; set; }
    [DomainSignature]
    public virtual string OrganisationName { get; set; }
    public virtual string Address1 { get; set; }
    public virtual string Address2 { get; set; }
    public virtual string City { get; set; }
    public virtual string Postcode { get; set; }
    public virtual LookupCountry Country { get; set; }
    public virtual string TelNo { get; set; }
    public virtual string FaxNo { get; set; }
    public virtual string Email { get; set; }
    public virtual User Contact { get; set; }
    public virtual DateTime? DateCreated { get; set; }
    public virtual DateTime? DateAmended { get; set; }
    public virtual bool Active { get; set; }
}

最后,我想要一个列,例如 TelNo,如 tel_no。 所以约定是如果 Property 中间包含大写字母 ti 应该加下划线。 Inflector.Net.Inflector.Underscore 工作正常。但我不知道如何写约定。

比如:

(conventions.GetPropertyName = type => Inflector.Net.Inflector.Underscore(type.ColumnName))

谢谢

There is o possibility to create a convention for Column naming:

I have this piece of code:

public AutoPersistenceModel Generate()
{
    var result = AutoPersistenceModel.MapEntitiesFromAssemblyOf<User>()
        .Where(GetAutoMappingFilter)
        .WithConvention(GetConventions);

    return result;
}

private bool GetAutoMappingFilter(Type t)
{
    return
        t.GetInterfaces().Any(
            x => x.IsGenericType && (x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>)));
}


private static void GetConventions(Conventions conventions)
{
    conventions.GetPrimaryKeyNameFromType = type => type.Name.ToLower() + "_id";
    conventions.FindIdentity = type => type.Name.ToLower() == "id";
    conventions.GetTableName = type =>
        {
            if (!type.Name.Contains("Lookup"))
            {
                return Inflector.Net.Inflector.Pluralize(type.Name).ToLower();
            }
            return Inflector.Net.Inflector.Underscore(type.Name)
                .Replace("lookup", "lu").ToLower();
        };
    conventions.IsBaseType = DontMapAsJoinedSubclassTypesInheritedFrom;
    conventions.GetForeignKeyNameOfParent = type => Inflector.Net.Inflector.Underscore(type.Name) + "_id";
    conventions.GetForeignKeyName = type => Inflector.Net.Inflector.Underscore(type.Name) + "_id";
    conventions.OneToManyConvention = m => m.Cascade.All();

}


private static bool DontMapAsJoinedSubclassTypesInheritedFrom(Type arg)
{
    var derivesFromEntity = arg == typeof(Entity);
    var derivesFromEntityWithTypedId = arg.IsGenericType &&
        (arg.GetGenericTypeDefinition() == typeof(EntityWithTypedId<>));
    return derivesFromEntity || derivesFromEntityWithTypedId;
}

and an Entity class

public class Organisation : EntityWithTypedId<int>
{
    public virtual Organisation Parent { get; set; }
    public virtual LookOrganisationType OrganisationType { get; set; }
    [DomainSignature]
    public virtual string OrganisationName { get; set; }
    public virtual string Address1 { get; set; }
    public virtual string Address2 { get; set; }
    public virtual string City { get; set; }
    public virtual string Postcode { get; set; }
    public virtual LookupCountry Country { get; set; }
    public virtual string TelNo { get; set; }
    public virtual string FaxNo { get; set; }
    public virtual string Email { get; set; }
    public virtual User Contact { get; set; }
    public virtual DateTime? DateCreated { get; set; }
    public virtual DateTime? DateAmended { get; set; }
    public virtual bool Active { get; set; }
}

Finally I want to have a column for, let's say TelNo like tel_no.
So the Convention is if Property contains capital letter in the middle ti should be underscored. Inflector.Net.Inflector.Underscore works fine. But I do not know how to write the convention.

Somethig like:

(conventions.GetPropertyName = type => Inflector.Net.Inflector.Underscore(type.ColumnName))

Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文