Oracle-创建DbContext

创建 DbContext

对于 Oracle 数据库,需要安装 Install-Package Chloe.Oracle 以及 Oracle 的驱动,然后使用 Chloe.Oracle.OracleContext 创建上下文实例。注意:DbContext 实例非线程安全,不能设置为单例。同时,用完务必要将 DbContext 释放。
因为框架本身需要与具体的数据库驱动解耦,所以 OracleContext 构造函数需要一个 IDbConnectionFactory 的参数,IDbConnectionFactory 接口只有一个 CreateConnection() 方法,必须先建个类,实现 CreateConnection 方法。

public class OracleConnectionFactory : IDbConnectionFactory
{
    string _connString = null;
    public OracleConnectionFactory(string connString)
    {
        this._connString = connString;
    }
    public IDbConnection CreateConnection()
    {
        OracleConnection oracleConnection = new OracleConnection(this._connString);
        OracleConnectionDecorator conn = new OracleConnectionDecorator(oracleConnection);
        return conn;
    }
}

由于笔者使用的是 Oracle.ManagedDataAccess 数据库驱动,OracleConnection 创建的 DbCommand 默认是以顺序方式绑定参数,所以,上述例子使用了装饰者模式对 OracleConnection 封装了一遍,主要就是修改 DbCommand 参数绑定方式。OracleConnectionDecorator 定义如下:

class OracleConnectionDecorator : IDbConnection, IDisposable
{
    private OracleConnection _oracleConnection;
    public OracleConnectionDecorator(OracleConnection oracleConnection)
    {
        if (oracleConnection == null)
            throw new Exception("Please call 911.");
        _oracleConnection = oracleConnection;
    }

    public string ConnectionString
    {
        get { return _oracleConnection.ConnectionString; }
        set { _oracleConnection.ConnectionString = value; }
    }
    public int ConnectionTimeout
    {
        get { return _oracleConnection.ConnectionTimeout; }
    }
    public string Database
    {
        get { return _oracleConnection.Database; }
    }
    public ConnectionState State
    {
        get { return _oracleConnection.State; }
    }

    public IDbTransaction BeginTransaction()
    {
        return _oracleConnection.BeginTransaction();
    }
    public IDbTransaction BeginTransaction(IsolationLevel il)
    {
        return _oracleConnection.BeginTransaction(il);
    }
    public void ChangeDatabase(string databaseName)
    {
        _oracleConnection.ChangeDatabase(databaseName);
    }
    public void Close()
    {
        _oracleConnection.Close();
    }
    public IDbCommand CreateCommand()
    {
        var cmd = _oracleConnection.CreateCommand();
        cmd.BindByName = true; //修改 DbCommand 参数绑定方式
        return cmd;
    }
    public void Open()
    {
        _oracleConnection.Open();
    }

    public void Dispose()
    {
        _oracleConnection.Dispose();
    }
}

接下来就可以创建 OracleContext:

string connString = "Your connection string";
OracleContext context = new OracleContext(new OracleConnectionFactory(connString));

OracleContext 生成 sql 语句时默认将表名和字段转成大写形式,如需要修改该默认设置,操作如下:

context.ConvertToUppercase = false;

ASP.NET CORE 配置 Service:

public void ConfigureServices(IServiceCollection services)
{
    //...

    services.AddScoped<Chloe.IDbContext>((serviceProvider) =>
    {
        return new Chloe.Oracle.OracleContext(new OracleConnectionFactory("Your connection string"));
    });

    //...
}