1

我正在开发一个使用 Dapper、使用通用模型的 DapperExtensions 的项目,我想知道如何使用 DapperExtension.GetAll 方法填充模型?

下面是返回我尝试使用 DapperExtensions 过滤的记录的 sql 代码。

select f.*
from Item f
where f.CurrentStatus = 'Open'
AND f.ItemID not in (SELECT ItemID FROM ItemLog l WHERE f.ItemID = l.ItemID
AND l.Status != 'Escalated'
AND DateLogged <= DATEADD(mi, 25, GetDate())) //<- this value would be replaced with a variable

我做了一些研究,发现您可以使用 Split.on,但不确定这是否适合这种情况

GetAll 方法看起来像这样,所以我们确实有能力过滤记录

public virtual IEnumerable<TModel> GetAll(IList<DbFilter<TModel>> filters = null)
{
    filters = filters ?? new List<DbFilter<TModel>>();
    using (var db = Context)
    {
        var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
        foreach (var filter in filters)
        {
             pg.Predicates.Add(Predicates.Field(filter.FilterExpression, filter.FilterOperator, filter.FilterItem));
        }
             return db.GetList<TModel>(pg).ToList();
        }
}

任何帮助将不胜感激。我已经接受了创建一个 SPROC 来填充模型的想法。只是试图确定最有效的路线。

好吧,我设法使用以下方法填充我的模型,仍然希望听到反馈或可能的建议。

public async Task<IEnumerable<FormsFnol>> GetLateItems(DateTime responseTime)
{
     IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
     var items = await db.QueryAsync<FormsFnol>(@"
         SELECT f.*
         FROM Item f
         WHERE f.CurrentStatus = 'Open'
         AND f.ItemID not in (SELECT ItemID FROM ItemLog l WHERE f.ItemID = l.ItemID
         AND l.Status != 'Escalated'
         AND DateLogged <= @dateTime
     ", new { @dateTime = responseTime});

     return items;
 }  
4

2 回答 2

1

Dapper Extensions 不支持存储过程,但 Dapper 支持。

对于您的代码,SP 看起来像这样:

result = dbConnection.Query<FormsFnol>("FormsFnol_s", 
                                        new { dateTime = responseTime},
                                        null, 
                                        true, 
                                        null, 
                                        CommandType.StoredProcedure);

您的存储过程将执行代码中的选择查询。我会使用存储过程有一个非常简单的原因:如果您需要更改选择方法,在 Sql 中更改它比更改程序本身要容易得多。

由于缺乏关于如何使用 DapperExtensions 的文档,并且事实上它确实有一段时间没有更新,我自己一直在远离 DapperExtensions。

于 2017-09-01T16:00:21.917 回答
0

猜猜我会使用我的答案,因为我没有得到任何正面或负面的反馈

public async Task<IEnumerable<FormsFnol>> GetLateItems(DateTime responseTime)
{
 IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
 var items = await db.QueryAsync<FormsFnol>(@"
     SELECT f.*
     FROM Item f
     WHERE f.CurrentStatus = 'Open'
     AND f.ItemID not in (SELECT ItemID FROM ItemLog l WHERE f.ItemID = l.ItemID
     AND l.Status != 'Escalated'
     AND DateLogged <= @dateTime
 ", new { @dateTime = responseTime});

 return items;
}  
于 2017-08-21T22:14:04.227 回答