LINQ到XYZ多态性? [英] LINQ-to-XYZ polymorphism?

查看:106
本文介绍了LINQ到XYZ多态性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,客户端要求我们实现我们的数据访问代码,以便基于运行时配置设置使用Oracle或SQL Server数据库。生产环境使用Oracle,但是dev和QA都是针对SQL Server实例运行的。

I have a situation where a client is requiring that we implement our data access code to use either an Oracle or SQL server database based on a runtime configuration settings. The production environment uses Oracle but both dev and QA are running against a SQL Server instance.

(我没有任何控制权或者为什么没有任何背景除了Oracle之外,他们的BI平台和开发人员都希望使用SQL Server)。

(I don't have any control over this or have any background on why this is the case other than Oracle is their BI platform and dev wants to work with SQL Server.)

他们的请求是使用LINQ-to-SQL / LINQ-to- Oracle用于所有数据访问。他们需要支持该应用程序,并且没有知识跳入EF(他们的要求) - 尽管我认为如果我们使用EF,存在同样的问题。

Their request is to use LINQ-to-SQL / LINQ-to-Oracle for all data access. They will need to support the application and do not have the knowledge to jump into EF yet (their requirement) - although I believe the same problem exists if we use EF.

虽然我可以为两个数据库实现LINQ to XYZ类,以便我可以连接到这两个数据库,但它们不共享一个通用接口(DataContext除外),所以我真的不能对接口进行编码,并在运行时插入实际的实现

While I can implement LINQ to XYZ classes for both databases so that I can connect to both, they don't share a common interface (other than DataContext) so I really can't code against an interface and plug the actual implementation in at runtime.

任何想法应该如何处理?

Any ideas how I should approach this?

更新
写完这篇文章后,我对EF进行了一些调查,在我看来,如果我使用EF - 这将是我的长期目标,这个问题就会存在。

UPDATE After writing this post, I did a little investigating into EF and it appears to me that this same problem exists if I use EF - which would be my long term goal.

推荐答案

为了关闭这个主题,这里是我最后做的:

For the sake of closure on this topic, here is what I ended up doing:

我实现了单位的组合的工作和仓库模式。工作单位类是消耗代码的工作原理,并公开了可以对我的根实体执行的所有操作。每个根实体有一个UoW。 UoW通过接口使用存储库类。存储库的实际实现取决于所使用的数据访问技术。

I implemented a combination of the Unit of Work and Repository patterns. The Unit of Work class is what consuming code works with and exposes all of the operations that can be performed on my root entities. There is one UoW per root entity. The UoW makes use of a repository class via an interface. The actual implementation of the repository is dependent on the data access technology being used.

所以,例如,如果我有一个客户实体,我需要支持检索和更新每个记录,我会有一些如下:

So, for instance, if I have a customer entity and I need to support retrieving and updating each record, I would have something like:

public interface ICustomerManager
{
    ICustomer GetCustomer(Guid customerId);
    void SaveCustomer(ICustomer customer);
}

public class CustomerManager : ICustomerManager
{
    public CustomerManager(ICustomerRepository repository)
    {
        Repository = repository;
    }

    public ICustomerRepository Repository { get; private set; }

    public ICustomer GetCustomer(Guid customerId)
    {
        return Repository.SingleOrDefault(c => c.ID == customerId);
    }

    public void SaveCustomer(ICustomer customer)
    {
        Repository.Save(customer);
    }
}

public interface ICustomerRepository : IQueryable<ICustomer>
{
    void Save(ICustomer customer);
}

我正在使用反转控制框架将ICustomerRepository实现注入到CustomerManager类在运行时。实现类将在一个单独的程序集中,随着数据访问技术的改变,它可以被交换出去。所有我们关心的是,存储库使用上面定义的合同实现每个方法。

I'm using an Inversion of Control framework to inject the ICustomerRepository implementation into the CustomerManager class at runtime. The implementation class will be in a separate assembly that can be swapped out as the data access technology is changed. All we are concerned about is that the repository implements each method using the contract defined above.

作为一个附注,为了使用Linq-to SQL,我只需要创建了一个实现ICustomerRepository并为实现ICustomer的生成的Customer实体类添加了一个部分类的LinqCustomerRepository类。然后,我可以从存储库返回L2S实体,作为UoW的ICustomer接口和调用代码的实现,并且它们不会是实体来自L2S代码的更聪明。

As a side note, to do this with Linq-to-SQL, I simply created a LinqCustomerRepository class that implements ICustomerRepository and added a partial class for the generated Customer entity class that implements ICustomer. Then I can return the L2S entity from the repository as the implementation of the ICustomer interface for the UoW and calling code to work with and they'll be none the wiser that the entity originated from L2S code.

这篇关于LINQ到XYZ多态性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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