在 Linq-to-SQL 中模拟多表继承

发布于 2024-10-31 01:38:07 字数 388 浏览 0 评论 0原文

到目前为止,每个人都知道 Linq-to-SQL 本身不支持多表继承(又名,每个子类型一个表),如果您想要本机支持,您可以使用其他 ORM 框架,例如 Entity Framework、NHibernate 等。支持多表继承(如果您有任何问题,请参考 SO 问题“LINQtoSQL 中的多重继承”)疑虑)。

但是,假设您确实想要使用(或仅限于使用)Linq-to-SQL 作为 ORM 层,是否有人确定了一种简单且直接的设计策略来模拟多表继承Linq-to-SQL 项目,以便可以使用自然的、面向对象的 API 针对 Linq-to-SQL 层编写客户端代码?

By now, everyone knows that Linq-to-SQL does not natively support multiple table inheritance (a.k.a., table-per-subtype) and that you can use other ORM frameworks such as the Entity Framework, NHibernate, etc. instead if you want native support for multiple table inheritance (reference the SO question "Multiple Inheritance in LINQtoSQL" if you have any doubts).

However, supposing that you did want to use (or were limited to use) Linq-to-SQL as your ORM layer, has anyone identified a simple and straight-forward design strategy for simulating multiple table inheritance in Linq-to-SQL projects so that client code can be written against the Linq-to-SQL layer using a natural, object-oriented API?

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

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

发布评论

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

评论(1

薄荷梦 2024-11-07 01:38:07

我认为 Sam 给出了很好的答案:Linq2Sql 似乎无法做到这一点。
但只要您只想模拟这种行为,我就会这样做:

      /// <summary>
    /// Declare POCO (Plain Old CLR Object) as you would to manipulate your inheritance
    /// </summary>
    public class Person
    {
        public abstract void Load();
        public abstract void Save();
    }

    /// <summary>
    /// Simulate a consistent behavior
    /// </summary>
    public class Customer : Person
    {

        public override void Load()
        {
            // - Do some Linq2Sql stuff
        }

        public override void Save()
        {
            // - Do some Linq2Sql stuff
        }

    }

对于像“JOIN”这样的查询,我会考虑生成自己的表达式树,如果我真的需要这个,因为这很快就会变得棘手。
您在 hajirazin 的这个示例也可以放在 Person 类中“受保护”,并从“客户”和其他子级中调用正确的值。

总之,我认为所有的工作都必须手动完成才能模拟对象中的请求。表达式树可以做一切事情,但很难最终确定。

此致,

I think Sam had the good answer : Linq2Sql doesn't seems to be able to do that.
But as long as you just want to simulate this behavior, I would do something like that:

      /// <summary>
    /// Declare POCO (Plain Old CLR Object) as you would to manipulate your inheritance
    /// </summary>
    public class Person
    {
        public abstract void Load();
        public abstract void Save();
    }

    /// <summary>
    /// Simulate a consistent behavior
    /// </summary>
    public class Customer : Person
    {

        public override void Load()
        {
            // - Do some Linq2Sql stuff
        }

        public override void Save()
        {
            // - Do some Linq2Sql stuff
        }

    }

For queries like "JOIN", I would consider generating my own expression trees, if I really need this, cause this quickly becomes tricky.
The "Left Outer Join" method you see in this example by hajirazin could also be put "protected" in class Person and called with right values from "Customer" and other children.

In conclusion, I think all the work has to be done manually to simulate requests in Objects. Expression trees could do everything but it is difficult to finalize.

Best regards,

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