在LINQ到SQL模拟多表继承 [英] Simulating Multiple Table Inheritance in Linq-to-SQL

查看:135
本文介绍了在LINQ到SQL模拟多表继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,大家都知道,LINQ到SQL本身不支持多表继承(即,表每个亚型),并且可以使用其他的ORM框架,如实体框架,NHibernate的,等等,而是如果你想对多个表继承原生支持(参考SO问题多重继承的LINQtoSQL 如果您有任何疑问)。

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).

但是,假设你是想用(或者被限制使用)的LINQ到SQL作为你的ORM层,有没有人确定了一个简单而直接的设计策略的模拟的多在LINQ到SQL项目,使客户端code可以使用天然的,面向对象的API编写针对LINQ到SQL层表继承?

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?

推荐答案

我觉得山姆有很好的答案: LINQ2SQL 的似乎并不能够做到这一点。 但只要你只是想的模拟的这种行为,我会做这样的事情:

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
        }

    }

有关类似加盟,我会考虑产生自己的 EX $ P查询$ pssion树木的,如果我真的需要这一点,导致这个很快就会变得非常棘手。 在你的<一见左外部联接的方法href="http://stackoverflow.com/questions/584820/how-do-you-perform-a-left-outer-join-using-linq-extension-methods">this例如,通过 hajirazin 也可以把类人保护,并要求与客户和其他孩子正确的价值观。

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.

在最后,我想所有的工作都必须手工完成,以模拟对象的请求。防爆pression树木能做到的一切,但也很难完成。

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.

最好的问候,

这篇关于在LINQ到SQL模拟多表继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆