Create a DbContext that handle a DatabaseFactory to use DapperExtensions more easily
Asked Answered
C

2

1

This days I try to create an abstract base repository using some basic CRUD functions proposed by DapperExtensions. But the code given as an exemple use a SqlConnection which is made to connect to a SQL Server database. I want to be able to connect to all kind of Database (SQL Server, MySql, etc...). Also their code sample is repeated for each CRUD function as the code below show

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    //Code doing something here...
    cn.Close();
}

So i was thinking about to create a DbContext that can handle the creation, the opening and closing of the connection and also can create the right connection object depending on the database type I want to use (a kind of database factory).

Is there somebody that already done it and could share his code?

Thank you guys !

Cherilyncherilynn answered 27/2, 2017 at 4:1 Comment(1)
It sounds like you are trying to build the facilities of a true ORM. Have you tried both Entity Framework and NHibernate and concluded through extensive, empirical benchmarking that both are simply too slow for your application because you have millions of records and extremely high traffic due to the incredible popularity of your service?Primine
E
0

You are using Dapper-Extensions; following code is with Dapper only. But it does not change the entire concept. Just instead of sql you need to pass poco.

Refer this answer for how I implemented IUnitOfWork and DalSession. In below code, BaseDal is just like BaseRepository.

public abstract class BaseDal
{
    internal BaseDal(IUnitOfWork unitOfWork)
    {
        dapperHandler = new DapperHandler(unitOfWork);
    }

    DapperHandler dapperHandler = null;

    protected T Get<T>(string sql, DynamicParameters param) where T : class
    {
        var result = dapperHandler.Query<T>(sql, param).FirstOrDefault();
        return result;
    }

    protected List<T> GetList<T>(string sql, DynamicParameters param) where T : class
    {
        var result = dapperHandler.Query<T>(sql, param).ToList();
        return result;
    }

    protected int Insert(string sql, DynamicParameters param)
    {
        var result = dapperHandler.Execute(sql, param);
        return result;
    }
}

Edit 1 For example code with Dapper-Extensions, refer this answer that I recently posted.

Endothelioma answered 27/2, 2017 at 12:53 Comment(0)
C
0
public abstract class ABaseRepository<M> : IBaseRepository<M>
        where M : BaseModel
    {
        private static DbProviderFactory factory = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ProviderName);
        protected static DbConnection connection;

    public static IDbConnection CreateOpenConnection()
    {
        connection = factory.CreateConnection();
        connection.Open();

        return connection;
    }

    public dynamic Insert(M model)
    {
        dynamic multiKey;
        using (IDbConnection con = CreateOpenConnection())
        {
            multiKey = con.Insert(model);
        }

        return multiKey;
    }
 }
Cherilyncherilynn answered 27/2, 2017 at 9:29 Comment(0)
E
0

You are using Dapper-Extensions; following code is with Dapper only. But it does not change the entire concept. Just instead of sql you need to pass poco.

Refer this answer for how I implemented IUnitOfWork and DalSession. In below code, BaseDal is just like BaseRepository.

public abstract class BaseDal
{
    internal BaseDal(IUnitOfWork unitOfWork)
    {
        dapperHandler = new DapperHandler(unitOfWork);
    }

    DapperHandler dapperHandler = null;

    protected T Get<T>(string sql, DynamicParameters param) where T : class
    {
        var result = dapperHandler.Query<T>(sql, param).FirstOrDefault();
        return result;
    }

    protected List<T> GetList<T>(string sql, DynamicParameters param) where T : class
    {
        var result = dapperHandler.Query<T>(sql, param).ToList();
        return result;
    }

    protected int Insert(string sql, DynamicParameters param)
    {
        var result = dapperHandler.Execute(sql, param);
        return result;
    }
}

Edit 1 For example code with Dapper-Extensions, refer this answer that I recently posted.

Endothelioma answered 27/2, 2017 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.