I have a base repository with a Generic Get method to return Data using Dapper like
public T Get<T>(Func<IDbConnection, T> query)
{
using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
{
return query.Invoke(db);
}
}
However I now have the need to return multiple Data. The DAL query is as below:
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();
}
Would it be possible to have a Generic GetMultiple in my BaseRepository or would it be a matter of wrapping the get multi in my Get method and then cars and options in there own separate Get calls?
You could do something like this which will return a tuple containing IEnumerables of each type you are after.
In Base Repository
(It's basically a bunch of overloads...you can add more overloads if you have more types).
In Derived Repository
(pretty clean and more importantly, typed!)