簡體   English   中英

有什么方法可以創建通用映射器來檢索對象內的集合?

[英]Is there any way to create generic mapper to retrieve collection within object?

我已經使用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;
    }

通過向Poco注入處理GridReader的方法,可以使代碼更“干凈”。

這允許分離關注點。

“ DataLayer”對象

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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM