通用QueryMultiple与Dapper

时间:2017-04-03 19:22:42

标签: c# .net generics dapper

我有一个带有Generic Get方法的基本存储库,可以使用Dapper返回Data,如

  public T Get<T>(Func<IDbConnection, T> query)
    {
        using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
        {
            return query.Invoke(db);
        }
    }

但是我现在需要返回多个数据。 DAL查询如下:

var multi = db.QueryMultiple(getCarDataSp , new { CustomerID = customerId, Year = year },
                                commandType: CommandType.StoredProcedure));

var cars = multi.Read<CarDTO>();
var options = multi.Read<CarOptionDTO>();

//wire the options to the cars
foreach(var car in cars){
    var carOptions = options.Where(w=>w.Car.CarID == car.CarID);        //I would override Equals in general so you can write w.Car.Equals(car)...do this on a common DataModel class
    car.Options = carOptions.ToList();
}

是否可以在我的BaseRepository中使用Generic GetMultiple,或者将get多重包装在我的Get方法中,然后将汽车和选项包含在单独的Get调用中?

1 个答案:

答案 0 :(得分:1)

你可以做类似这样的事情,它会返回一个元组,其中包含你所追求的每种类型的IEnumerables。

在Base Repository中

(它基本上是一堆重载......如果你有更多的类型,你可以添加更多的重载。)

public Tuple<IEnumerable<T1>, IEnumerable<T2>> GetMultiple<T1, T2>(string sql, object parameters,
                                        Func<GridReader, IEnumerable<T1>> func1,
                                        Func<GridReader, IEnumerable<T2>> func2)
        {
            var objs = getMultiple(sql, parameters, func1, func2);
            return Tuple.Create(objs[0] as IEnumerable<T1>, objs[1] as IEnumerable<T2>);
        }

        public Tuple<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>> GetMultiple<T1, T2, T3>(string sql, object parameters,
                                        Func<GridReader, IEnumerable<T1>> func1,
                                        Func<GridReader, IEnumerable<T2>> func2,
                                        Func<GridReader, IEnumerable<T3>> func3)
        {
            var objs = getMultiple(sql, parameters, func1, func2, func3);
            return Tuple.Create(objs[0] as IEnumerable<T1>, objs[1] as IEnumerable<T2>, objs[2] as IEnumerable<T3>);
        }

        private List<object> getMultiple(string sql, object parameters,params Func<GridReader,object>[] readerFuncs )
        {
            var returnResults = new List<object>();
            using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
            {
                var gridReader = db.QueryMultiple(sql, parameters);

                foreach(var readerFunc in readerFuncs)
                {
                    var obj = readerFunc(gridReader);
                    returnResults.Add(obj);
                }
            }

            return returnResults;
        }

在派生存储库中

(非常干净,更重要的是,键入!)

public class Foo { }

        public class Bar { }

        public void sample()
        {
            var sql = "select * from Foo; select * from Bar";
            var foosAndBars = this.GetMultiple(sql, new { param = "baz" }, gr => gr.Read<Foo>(), gr => gr.Read<Bar>());
            var foos = foosAndBars.Item1;
            var bars = foosAndBars.Item2;
        }