LINQ-to-XYZ polymorphism?

I have a situation where a client requires us to implement our data access code to use an Oracle or SQL Server database based on runtime configuration settings. The production environment uses Oracle, but both dev and QA work with an instance of SQL Server.

(I have no control over this or there are no prerequisites why this is so, except Oracle, is their BI platform, and dev wants to work with SQL Server.)

Their query is to use LINQ-to-SQL / LINQ-to-Oracle to access data. They will need to maintain the application and not have the knowledge to switch to EF yet (their requirement) - although I believe that the same problem exists if we use EF.

While I can implement LINQ to XYZ classes for both databases so that I can connect to both, they do not have a common interface (except for the DataContext), so I really can not code the interface and connect the real implementation at runtime.

Any ideas how I should approach this?

UPDATE After writing this post, I researched EF a bit and it seems to me that the same problem exists if I use EF - this will be my long-term goal.

+3
source share
3 answers

To close this topic, here is what I ended up doing:

. Unit of Work - , , . UoW . UoW . .

, , , , - :

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

, Linq-to-SQL, LinqCustomerRepository, ICustomerRepository Customer, ICustomer. L2S ICustomer UoW , , L2S.

0

. MEF framework DAL. (dev, production, QA) DAL (Oracle, SQL ..).

MEF, - .

- Joydip Kanjilal . .

0

, ORM datacontext , , IDataContext.

DAL . , , MEF, , IoC.

0

Source: https://habr.com/ru/post/1780802/


All Articles