mercredi 15 septembre 2021

What design pattern should I use in DAL layer while having multiple database sources with different models?

I've an already existing SQL database that has table Car for example that looks like this

public class Car
{
    public string Brand { get; set; }
    public string AvailableColorsCommaSperated { get; set; }
}

Now I'm working on migrating to MongoDb so that the data could look like that

public class Car
{
    public string Brand { get; set; }
    public List<string> Colors { get; set; }
}

But I've to maintain the SQL database at the same time as it will remain used for sometime even after migration.

Now in my DAL layer I had a Master interface and class that looks like Repository pattern which the interface was like that

public interface ICarDAL
{
    List<Car> GetAllCars();
}

public class CarDAL : ICarDAL
{
    private readonly ICarSQL carSQL;
    public CarDAL(ICarSQL carSQL)
    {
        this.carSQL = carSQL;
    }
    public List<Car> GetAllCars()
    {
        return carSQL.GetAllCars();
    }
}

While to implement that using the SQLContext another interface and class with implementation exists as so

public interface ICarSQL : ICarDAL
{
    new List<Car> GetAllCars();
}

public class CarSQL : ICarSQL
{
    private readonly DbContext dbContext; 
    public CarSQL(DbContext dbContext)
    {
        this.dbContext = dbContext;
    }

    public List<Car> GetAllCars()
    {
        // Get Cars..
    }
}

I could have no problem of adding a seperate layer for the Mongo to inherit from the ICarDAL and implement the functions on its own, but the problem is that I will have different Car Model for each one of them (the Car is just an example)

How can I abstract the DAL layer so that I can make the CRUD operations with different Models to different Databases? and am I going to need different context or use the same one with a factory to distinguish between different databases classes?

Aucun commentaire:

Enregistrer un commentaire