This is a post that builds on from my first post on Dapper.
Sometimes, we need self contained queries, without wanting to change a lot of areas of the standard code base.
I managed to get Dapper up and running for Ls One. now instead of creating providers etc for simple queries, I use my approach.
Ls One does not inherit the standard IDbConnection, it instead has its own IConnectionManager, and IConnection. however, we are in luck because IConnection has a definition called ExecuteReader that returns an IDataReader.
The previous process was to manually map whatever IDataReader returns, field by field.
Instead, We can use the SqlMapper Parse feature documented here -
public class ShippingType
{
public int Code { get; set; }
public string Name { get; set; }
}
public interface IShippingTypeRepository
{
List<ShippingType> GetAll(IConnectionManager entry);
}
public class ShippingTypeRepository: IShippingTypeRepository
{
public List<ShippingType> GetAll(IConnectionManager entry)
{
List<ShippingType> ShippingTypes = new List<ShippingType>();
using (System.Data.SqlClient.SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM TransportDetails";
System.Data.IDataReader _dr = null;
try
{
_dr = entry.Connection.ExecuteReader(cmd, System.Data.CommandType.Text);
**ShippingTypes = (SqlMapper.Parse<ShippingType>(_dr)).ToList();**
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
finally
{
if (_dr != null)
{
_dr.Close();
_dr.Dispose();
}
}
return ShippingTypes;
}
}
}
I think I will be using this approach for the foreseeable future :)
Post Picture: Trees in Toorak , Fiji Islands. Shot by: VijeshDattPhotography