在 Subsonic SimpleRepository 中使用我自己的属性

发布于 2024-11-25 07:05:57 字数 148 浏览 2 评论 0原文

我最近一直在寻找 ORM,SubSonic 及其 SimpleRepository 似乎是我正在寻找的解决方案。

有没有办法使用我自己的属性或来自 System.ComponentModel 的属性来驱动某些 SQL 的生成?我想让我的模型/域对象远离第三方的东西。

I have been looking for an ORM of late and SubSonic with its SimpleRepository appears to be the solution I'm looking for.

Is there a way to use my own attributes or ones from System.ComponentModel to drive some of the generation of the SQL? I want to keep my model/domain objects clean of third-party stuff.

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

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

发布评论

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

评论(1

太阳公公是暖光 2024-12-02 07:05:57

我不建议这样做,但这是可以做到的。

创建您自己的与 SubSonics 属性匹配的属性(例如 OrmIgnore 而不是 SubSonicIgnore,仍然需要实现 SubSonic.SqlGeneration.Schema.IClassMappingAttribute 或 <代码>SubSonic.SqlGeneration.Schema.IPropertyMappingAttribute 或SubSonic.SqlGeneration.Schema.IRelationMappingAttribute

查看 SubSonic.Core\Extensions\Object.cs 中的代码,了解发生了什么情况,

    public static ITable ToSchemaTable(this Type type, IDataProvider provider)
    {

        ...

        var typeAttributes = type.GetCustomAttributes(typeof(IClassMappingAttribute), false);

        foreach (IClassMappingAttribute attr in typeAttributes)
        {
            if (attr.Accept(result))
            {
                attr.Apply(result);
            }
        }

        ...

        // Now work with attributes
        foreach (IPropertyMappingAttribute attr in attributes.Where(x => x is IPropertyMappingAttribute))
        {
            if (attr.Accept(column))
            {
                attr.Apply(column);
            }
        }

        ....

    }

您的 Apply 实现应该修改架构来执行您想要的操作。
就像这个(来自 SubSonicDefaultSettingAttribute):

    public void Apply(IColumn column)
    {
        column.DefaultSetting = DefaultSetting;
    }

您应该检查 SubSonic 源并将每个自定义属性标记为过时

[Obsolete("Use OrmIgnore instead", true)]
[AttributeUsage(AttributeTargets.Property)]
public class SubSonicIgnoreAttribute : Attribute { }

有一些对属性(不使用接口)的直接引用需要修复。

你将不得不寻找字符串

    private static bool ColumnIsIgnored(object[] attributes)
    {
        foreach (var att in attributes)
        {
            if (att.ToString().Equals("SubSonic.SqlGeneration.Schema.SubSonicIgnoreAttribute"))
            {
                return true;
            }
        }

        return false;
    }

引用

    private static bool ColumnIsIgnored(object[] attributes)
    {
        foreach (var att in attributes)
        {
            if (att.ToString().EndsWith("OrmIgnoreAttribute"))
            {
                return true;
            }
        }

        return false;
    }

I wouldn't recommend that but this can be done.

Create your own attributes that match SubSonics attributes (like OrmIgnore instead of SubSonicIgnore, the still have to Implement SubSonic.SqlGeneration.Schema.IClassMappingAttribute or SubSonic.SqlGeneration.Schema.IPropertyMappingAttribute or SubSonic.SqlGeneration.Schema.IRelationMappingAttribute

Look at this code from SubSonic.Core\Extensions\Object.cs to get an idea what's happening

    public static ITable ToSchemaTable(this Type type, IDataProvider provider)
    {

        ...

        var typeAttributes = type.GetCustomAttributes(typeof(IClassMappingAttribute), false);

        foreach (IClassMappingAttribute attr in typeAttributes)
        {
            if (attr.Accept(result))
            {
                attr.Apply(result);
            }
        }

        ...

        // Now work with attributes
        foreach (IPropertyMappingAttribute attr in attributes.Where(x => x is IPropertyMappingAttribute))
        {
            if (attr.Accept(column))
            {
                attr.Apply(column);
            }
        }

        ....

    }

your Apply implementation should modify the schema to do what you want.
Like this one (from SubSonicDefaultSettingAttribute):

    public void Apply(IColumn column)
    {
        column.DefaultSetting = DefaultSetting;
    }

You should check out the SubSonic source and mark every custom attribute as obsolete

[Obsolete("Use OrmIgnore instead", true)]
[AttributeUsage(AttributeTargets.Property)]
public class SubSonicIgnoreAttribute : Attribute { }

There are some direct references to the attributes (that don't use the interface) that you will need to fix.

And you will have to look for string references

    private static bool ColumnIsIgnored(object[] attributes)
    {
        foreach (var att in attributes)
        {
            if (att.ToString().Equals("SubSonic.SqlGeneration.Schema.SubSonicIgnoreAttribute"))
            {
                return true;
            }
        }

        return false;
    }

with

    private static bool ColumnIsIgnored(object[] attributes)
    {
        foreach (var att in attributes)
        {
            if (att.ToString().EndsWith("OrmIgnoreAttribute"))
            {
                return true;
            }
        }

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