【问题标题】:Is there any way to create generic mapper to retrieve collection within object?有没有办法创建通用映射器来检索对象内的集合?
【发布时间】:2019-06-18 10:11:48
【问题描述】:

我已经使用 dapper 设置了一个通用查询执行器,但我想知道是否有办法使用存储过程为包含对象列表的对象创建映射器。

例如: 我需要检索公司和相关产品

public class company
{
    public List<Product> products {get;set;}
}


  public static async Task<List<DTO>> ExecuteQueryAsync<DTO>(string query , object param) where DTO : class, new()
    {
        List<DTO> result = null;

        try
        {
            var connection = new DbConnection().GetConnection();
            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }

            if (connection.State == ConnectionState.Open)
            {
                result = await SqlMapper.QueryAsync<DTO>(connection, query,param) as List<DTO>;
                connection.Close();

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }

【问题讨论】:

标签: c# .net .net-core


【解决方案1】:

您可以通过将处理 GridReader 的方法注入 Poco 来使您的代码更加“干净”。

这允许关注点分离。

“数据层”对象

using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace MyNamespace.DataLayer
{ 

public class MyCustomObjectData : IMyCustomObjectData
{

    public async Task<ICollection<MyCustomObject>> MyMethodAsync(Func<GridReader, ICollection<MyCustomObject>> handleFunction)
    {
        ICollection<MyCustomObject> returnItems = null;
        string sqlProcedureName = "dbo.uspMyCustomObjectSelectStuff";

        try
        {
            using (IDbConnection dbConnection = /* your code here */)
            {
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add(/* your code here */);
                GridReader gr = await dbConnection.QueryMultipleAsync(sqlProcedureName, parameters, commandType: CommandType.StoredProcedure, commandTimeout: 120);
                if (null != handleFunction)
                {
                    returnItems = handleFunction(gr);
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message, ex);
        }

        return returnItems;
    }
}
}

“DomainDataLayer”对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static Dapper.SqlMapper;

namespace MyNamespace.DomainDataLayer
{    
    public class MyCustomObjectDomainData : IMyCustomObjectDomainData
    {

        public MyCustomObjectDomainData(IMyCustomObjectData crgDataLayer)
        {
            this.MyCustomObjectData = crgDataLayer ?? throw new ArgumentNullException("IMyCustomObjectData is null");
        }

        public async Task<ICollection<MyCustomObject>> MyCustomObjectGetMethodAsync()
        {
            ICollection<MyCustomObject> returnCollection = null;
            /* CALL the datalayer, but INJECT the method to handle the GridReader */
            returnCollection = await this.MyCustomObjectData.MyMethodAsync(this.HandleMyCustomObjectGridReaderResult);
            return returnCollection;
        }

        public ICollection<MyCustomObject> HandleMyCustomObjectGridReaderResult(GridReader gr)
        {
            ICollection<MyCustomObject> returnCollection = null;

            using (gr)
            {
                /*  Get objects from each SELECT statement in the stored procedure */
                returnCollection = gr.Read<MyCustomObject>().ToList();

                /* this would be how to handle a SECOND "select" statement in the stored procedure */
                IEnumerable<MyOtherCustomObjectFromASecondStoredProcedureSelectStatement> otherThings = gr.Read<MyOtherCustomObjectFromASecondStoredProcedureSelectStatement>().ToList();

                /* optionally, you can hand-map the pocos to each other */
                //returnCollection = new MyCustomObjectObjectMapper().MapMultipleMyCustomObject(returnCollection, otherThings);
            }

            return returnCollection;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-02
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2021-09-01
    相关资源
    最近更新 更多