Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:如何使用dapper ORM的接口_C#_Dapper - Fatal编程技术网

C#:如何使用dapper ORM的接口

C#:如何使用dapper ORM的接口,c#,dapper,C#,Dapper,我有复杂的应用程序到数据库操作,我正在使用简洁的Micro ORM,我想制作松散耦合的代码。请建议我如何使用界面而不是整洁的类。 我有以下代码: public IEnumerable<Category> Find() { using (IDbConnection _conn = GetConnection) { _conn.Open(); return _conn.Query<Categor

我有复杂的应用程序到数据库操作,我正在使用简洁的Micro ORM,我想制作松散耦合的代码。请建议我如何使用界面而不是整洁的类。 我有以下代码:

public IEnumerable<Category> Find()
    {
        using (IDbConnection _conn = GetConnection)
        {
            _conn.Open();
            return _conn.Query<Category>("usp_Category", commandType: CommandType.StoredProcedure);
        }
    }
public IEnumerable Find()
{
使用(IDbConnection _conn=GetConnection)
{
_conn.Open();
返回连接查询(“usp\U类别”,commandType:commandType.StoredProcess);
}
}
我想换成

public IEnumerable<ICategory> Find()
    {
        using (IDbConnection _conn = GetConnection)
        {
            _conn.Open();
            return _conn.Query<ICategory>("usp_Category", commandType: CommandType.StoredProcedure);
        }
    }
public IEnumerable Find()
{
使用(IDbConnection _conn=GetConnection)
{
_conn.Open();
返回连接查询(“usp\U类别”,commandType:commandType.StoredProcess);
}
}

你试图用
简洁的
C#
做的事情是不正确的:

_conn.Query<ICategory>("usp_Category", commandType: CommandType.StoredProcedure);

IEnumerable
不是正确的用法,也不能通过任何方式获取,要理解您需要理解
接口
具体的
类之间的区别及其用法

您试图用
Dapper
C
做的事情是不正确的:

_conn.Query<ICategory>("usp_Category", commandType: CommandType.StoredProcedure);

IEnumerable
不是正确的用法,也不能通过任何方式获取,要理解您需要理解
接口
具体的
类之间的区别以及它们的用法

,即使您希望松散地耦合代码,在某个时候,您必须为每个ICategory实例创建一个具体的实现

在您提供的示例中,Dapper调用就是发生这种情况的地方。Dapper将从查询中获取数据(通过IDbConnection实现类型的连接,因此存在一些松散耦合),并将每一行转换为一个Category实例

“Find”方法将返回一个IEnumerable,由这些具体实现组成。但是,因为IEnumerable是协变的,所以IEnumerable可以转换为IEnumerable

这允许您的“Find”方法具有IEnumerable的返回类型,这意味着该方法的使用者不需要知道任何东西,只需要返回一组ICategory实现即可(Dapper需要知道要填充的具体类型,但Find方法的调用方不需要知道将返回什么样的ICategory实现)

您的代码只需稍作更改:

public IEnumerable<ICategory> Find()
{
    using (IDbConnection _conn = GetConnection())
    {
        return _conn.Query<Category>("usp_Category", commandType: CommandType.StoredProcedure);
    }
}
public IEnumerable Find()
{
使用(IDbConnection\u conn=GetConnection())
{
返回连接查询(“usp\U类别”,commandType:commandType.StoredProcess);
}
}

(请注意,我删除了
\u conn.Open();
行,因为Dapper将为您打开尚未打开的连接)。

即使您希望松散地耦合代码,在某个时候您也必须为每个ICategory实例创建一个具体的实现

在您提供的示例中,Dapper调用就是发生这种情况的地方

“Find”方法将返回一个由这些具体实现组成的IEnumerable。但是,由于IEnumerable是协变的,IEnumerable可能被转换为IEnumerable

这允许您的“Find”方法具有IEnumerable的返回类型,这意味着该方法的使用者不需要知道任何东西,只需要返回一组ICategory实现即可(Dapper需要知道要填充的具体类型,但Find方法的调用方不需要知道将返回什么样的ICategory实现)

您的代码只需稍作更改:

public IEnumerable<ICategory> Find()
{
    using (IDbConnection _conn = GetConnection())
    {
        return _conn.Query<Category>("usp_Category", commandType: CommandType.StoredProcedure);
    }
}
public IEnumerable Find()
{
使用(IDbConnection\u conn=GetConnection())
{
返回连接查询(“usp\U类别”,commandType:commandType.StoredProcess);
}
}

(请注意,我删除了
\u conn.Open();
行,因为如果连接尚未打开,Dapper将为您打开连接)。

我们可以使用通用实现,而不是松散耦合的代码,它将使用通用存储库模式使用ORM存储库

 public virtual IEnumerable<T> GetAll()
    {
        IEnumerable<T> items = null;
        using (DbConnection cn = this.Connection)
        {
            cn.Open();
            items = cn.Query<T>("Select * from [TableName]");
        }

        return items;
    }
public虚拟IEnumerable GetAll()
{
IEnumerable items=null;
使用(DbConnection cn=this.Connection)
{
cn.Open();
items=cn.Query(“从[表名]中选择*”;
}
退货项目;
}

您可以在GitHub下找到使用ASP.NET Core的通用实现的库,而不是松散耦合的代码,我们可以使用通用实现,它将使用通用存储库模式消耗您的ORM存储库

 public virtual IEnumerable<T> GetAll()
    {
        IEnumerable<T> items = null;
        using (DbConnection cn = this.Connection)
        {
            cn.Open();
            items = cn.Query<T>("Select * from [TableName]");
        }

        return items;
    }
public虚拟IEnumerable GetAll()
{
IEnumerable items=null;
使用(DbConnection cn=this.Connection)
{
cn.Open();
items=cn.Query(“从[表名]中选择*”;
}
退货项目;
}
您可以在GitHub下找到使用ASP.NET核心进行通用实现的库