private void SetMappingForReceiptModel() { var columnMaps = new Dictionary <string, string> { { "cheque_id", "Id" }, { "cheque_number", "Number" }, { "summ", "Summ" }, { "discount", "Discount" }, { "articles", "Articles" } }; var mapper = new Func <Type, string, PropertyInfo>((type, columnName) => { if (columnMaps.ContainsKey(columnName)) { return(type.GetProperty(columnMaps[columnName])); } else { return(type.GetProperty(columnName)); } }); var receiptMap = new CustomPropertyTypeMap(typeof(ReceiptModel), (type, columname) => mapper(type, columname) ); SqlMapper.SetTypeMap(typeof(ReceiptModel), receiptMap); }
public virtual void MappingModel <T>() { var map = new CustomPropertyTypeMap(typeof(T), (type, columnName) => type.GetProperties().FirstOrDefault(prop => GetDescriptionFromAttribute(prop) == columnName)); SqlMapper.SetTypeMap(typeof(T), map); }
public static IDbConnection CreateConnection(string connString) { var connection = new NpgsqlConnection(connString); // SqlMapper.AddTypeHandler(new DateTimeTypeHandler()); var mapper = new Func <Type, string, PropertyInfo>((type, columnName) => { if (columnName == "hour") { return(type.GetProperty("Timeslice")); } else { return(type.GetProperty(columnName)); } }); var KillmailModelMap = new CustomPropertyTypeMap( typeof(KillmailModel), (type, columnName) => mapper(type, columnName) ); SqlMapper.SetTypeMap(typeof(KillmailModel), KillmailModelMap); return(connection); }
public static void Mapper <T>() { var map = new CustomPropertyTypeMap(typeof(T), (type, columnName) => type.GetProperties().FirstOrDefault(prop => GetDescriptionFromAttribute(prop).ToLower() == columnName.ToLower())); SqlMapper.SetTypeMap(typeof(T), map); }
private void SetTypeMap(Type type) { var map = new CustomPropertyTypeMap(type, (t, columnName) => t.GetProperties().FirstOrDefault(prop => GetDescriptionFromAttribute(prop) == columnName)); SqlMapper.SetTypeMap(type, map); }
/// <summary> /// SQLs the mapper set type map. /// </summary> /// <typeparam name="TEntity">The type of the t entity.</typeparam> protected void SqlMapperSetTypeMap <TEntity>() { var map = new CustomPropertyTypeMap(typeof(TEntity), (type, columnName) => type.GetProperties().FirstOrDefault(prop => GetDescriptionFromAttribute(prop) == columnName)); SqlMapper.SetTypeMap(typeof(TEntity), map); }
public MapPair(Type type) { ClrType = type; TypeMap = new CustomPropertyTypeMap(type, Retrieval); Items = new Dictionary <Dictionary <string, string>, Dictionary <string, PropertyInfo> >(); Dapper.SqlMapper.SetTypeMap(type, TypeMap); }
public DpEntityRepositoryBase(IDbContext dbContext) : base(dbContext) { var map = new CustomPropertyTypeMap(typeof(TEntity), (type, columnName) => type.GetProperties().FirstOrDefault(prop => GetDescriptionFromAttribute(prop) == columnName)); SqlMapper.SetTypeMap(typeof(TEntity), map); }
public static CustomPropertyTypeMap GetRegionMap() { Dictionary <string, string> columnMaps = new Dictionary <string, string> { { "region_id", "region_id" }, { "region_name", "region_name" }, { "region_number_of_population", "region_number_of_population" }, { "state_id", "state_id" } }; var mapper = new Func <Type, string, PropertyInfo>((type, columnName) => { if (columnMaps.ContainsKey(columnName)) { return(type.GetProperty(columnMaps[columnName])); } else { return(type.GetProperty(columnName)); } }); var RegionMap = new CustomPropertyTypeMap(typeof(RegionModel), (type, columnName) => mapper(type, columnName)); return(RegionMap); }
/// <summary> /// カスタムマッピング用のメソッド。ただしdynamicで取得した場合は変換されない /// 参考:http://neue.cc/2012/12/11_390.html /// </summary> private static void SetSnakeToPascal <T>() { var mapper = new CustomPropertyTypeMap(typeof(T), (type, columnName) => { var propName = NamingHelper.MapFieldToProperty(columnName); return(type.GetProperty(propName)); }); SqlMapper.SetTypeMap(typeof(T), mapper); }
public UserRepository(IOptions <DatabaseOptions> databaseOptions) : base(databaseOptions) { // set up the mappers for handling the sql query id's being prefixed ( ie - UserId / AddressId) var userMap = new CustomPropertyTypeMap(typeof(User), (type, column) => this.ColumnMapper(type, column)); var addressMap = new CustomPropertyTypeMap(typeof(Address), (type, column) => this.ColumnMapper(type, column)); SqlMapper.SetTypeMap(typeof(User), userMap); SqlMapper.SetTypeMap(typeof(Address), addressMap); }
private void DapperInit() { var secConstrMap = new CustomPropertyTypeMap( typeof(SecondaryConstraintDTO), (type, columnName) => { if (columnName == "SecConstraintsId") { return(type.GetProperty("Id")); } if (columnName == "SecConstraintsName") { return(type.GetProperty("Name")); } if (columnName == "SecConstraintsUseAsAConstraint") { return(type.GetProperty("UseAsAConstraint")); } if (columnName == "SecConstraintsCalendarEffect") { return(type.GetProperty("CalendarEffect")); } if (columnName == "SecConstraintsMaxValue") { return(type.GetProperty("MaxValue")); } if (columnName == "SecConstraintsMinValue") { return(type.GetProperty("MinValue")); } if (columnName == "SecConstraintsReferenceDate") { return(type.GetProperty("ReferenceDate")); } if (columnName == "SecConstraintsFromDate") { return(type.GetProperty("FromDate")); } if (columnName == "SecConstraintsToDate") { return(type.GetProperty("ToDate")); } if (columnName == "SecConstraintsDim") { return(type.GetProperty("Dim")); } throw new InvalidOperationException($"No matching mapping for {columnName}"); } ); Dapper.SqlMapper.SetTypeMap(typeof(SecondaryConstraintDTO), secConstrMap); }
public RegisteredClass(object someClass) { SomeClass = someClass; TableName = GetTableName(someClass); Dictionary <string, string> columnMaps = new Dictionary <string, string>(); foreach (PropertyInfo property in someClass.GetType().GetProperties()) { if (property.IsStorable()) { RegisteredProperty registeredProperty = new RegisteredProperty { Property = property, ColumnName = GetColumnName(property), IsKey = IsKey(property), IsAutoIncrement = IsAutoIncrement(property), PropertyName = $"{property.Name}", PropertyType = property.PropertyType }; RegisteredProperties.Add(registeredProperty); if (registeredProperty.PropertyName != registeredProperty.ColumnName) { columnMaps.Add(registeredProperty.ColumnName, registeredProperty.PropertyName); } } } var mapper = new Func <Type, string, PropertyInfo>((type, columnName) => { if (columnMaps.ContainsKey(columnName)) { return(type.GetProperty(columnMaps[columnName])); } else { return(type.GetProperty(columnName)); } }); var map = new CustomPropertyTypeMap( someClass.GetType(), (type, columnName) => mapper(type, columnName) ); SqlMapper.SetTypeMap(someClass.GetType(), map); }
public static void MakeDapperMapping(string namspace) { foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var type in assembly.GetTypes().Where(t => t.FullName.Contains(namspace))) { var map = new CustomPropertyTypeMap(type, (t, columnName) => t.GetProperties().FirstOrDefault( prop => GetDescriptionFromAttribute(prop) == columnName || prop.Name.ToLower().Equals(columnName.ToLower()))); SqlMapper.SetTypeMap(type, map); } } }
static BuildAComplexModel() { var selectionMap = new CustomPropertyTypeMap(typeof(Selection), (Type t, string c) => { if (t != typeof(Selection)) { return(null); } return(t.GetProperty("Id")); }); SqlMapper.SetTypeMap(typeof(Selection), selectionMap); }
static Column2PropertyMapping() { var assCore = typeof(EMT.DoneNOW.Core.crm_account).Assembly.GetExportedTypes(); foreach (var t1 in assCore) { if (t1.IsClass && t1.GetCustomAttributes(typeof(TableAttribute), false).Count() > 0) { var map1 = new CustomPropertyTypeMap(t1, (type, columnName) => type.GetProperties().FirstOrDefault(prop => Utils.GetColumnAttributeName(prop) == columnName)); SqlMapper.SetTypeMap(t1, map1); } } }
public static void SetFor <T>(string prefix = null) { if (prefix == null) { prefix = typeof(T).Name + "_"; } var typeMap = new CustomPropertyTypeMap(typeof(T), (type, name) => { if (name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) { name = name.Substring(prefix.Length); } return(type.GetProperty(name)); }); SqlMapper.SetTypeMap(typeof(T), typeMap); }
public void TestCustomTypeMap() { // default mapping var item = connection.Query <TypeWithMapping>("Select 'AVal' as A, 'BVal' as B").Single(); Assert.Equal("AVal", item.A); Assert.Equal("BVal", item.B); // custom mapping var map = new CustomPropertyTypeMap(typeof(TypeWithMapping), (type, columnName) => type.GetProperties().FirstOrDefault(prop => GetDescriptionFromAttribute(prop) == columnName)); SqlMapper.SetTypeMap(typeof(TypeWithMapping), map); item = connection.Query <TypeWithMapping>("Select 'AVal' as A, 'BVal' as B").Single(); Assert.Equal("BVal", item.A); Assert.Equal("AVal", item.B); // reset to default SqlMapper.SetTypeMap(typeof(TypeWithMapping), null); item = connection.Query <TypeWithMapping>("Select 'AVal' as A, 'BVal' as B").Single(); Assert.Equal("AVal", item.A); Assert.Equal("BVal", item.B); }
public DapperTypeMap(IInsqlMapSection mapSection) { this.mapSection = mapSection; this.typeMap = new CustomPropertyTypeMap(mapSection.Type, this.GetProperty); }
void Mapping() { Dictionary <string, string> columnMaps = new Dictionary <string, string> { { "Id", "Id" }, { "Name", "Name" }, { "NodeId", "Id" }, { "NodeName", "Name" }, { "ParentId", "ParentId" }, { "AttributeId", "Id" }, { "AttributeName", "Name" }, }; Dictionary <string, string> nodeColumnMaps = new Dictionary <string, string> { { "NodeId", "Id" }, { "NodeName", "Name" }, { "ParentId", "ParentId" }, }; var mapper = new Func <Type, string, PropertyInfo>((type, columnName) => { if (columnMaps.ContainsKey(columnName)) { return(type.GetProperty(columnMaps[columnName])); } else { return(type.GetProperty(columnName)); } }); var mapper2 = new Func <Type, string, PropertyInfo>((type, columnName) => { if (nodeColumnMaps.ContainsKey(columnName)) { return(type.GetProperty(nodeColumnMaps[columnName])); } else { return(type.GetProperty(columnName)); } }); var customMap = new CustomPropertyTypeMap( typeof(CustomDataSet), (type, columnName) => mapper(type, columnName)); var serviceMap = new CustomPropertyTypeMap( typeof(Service), (type, columnName) => mapper(type, columnName)); var nodeMap = new CustomPropertyTypeMap( typeof(Node), (type, columnName) => mapper2(type, columnName)); var attrMap = new CustomPropertyTypeMap( typeof(Attribute), (type, columnName) => mapper(type, columnName)); SqlMapper.SetTypeMap(typeof(CustomDataSet), customMap); SqlMapper.SetTypeMap(typeof(Service), serviceMap); SqlMapper.SetTypeMap(typeof(Node), nodeMap); SqlMapper.SetTypeMap(typeof(Models.Attribute), attrMap); }
public void ShowSample(SqlConnection conn) { // For sake of simplicity use only one dictionary // since all column names are unique Dictionary <string, string> columnMaps = new Dictionary <string, string> { // Column => Property { "UserId", "Id" }, { "CompanyId", "Id" } }; // Create mapping function var mapper = new Func <Type, string, PropertyInfo>((type, columnName) => { if (columnMaps.ContainsKey(columnName)) { return(type.GetProperty(columnMaps[columnName])); } else { return(type.GetProperty(columnName)); } }); // Create customer mapper for User object var userMap = new CustomPropertyTypeMap( typeof(User), (type, columnName) => mapper(type, columnName) ); // Create customer mapper for Company object var companyMap = new CustomPropertyTypeMap( typeof(Company), (type, columnName) => mapper(type, columnName) ); // Notify Dapper to use the mappers SqlMapper.SetTypeMap(typeof(User), userMap); SqlMapper.SetTypeMap(typeof(Company), companyMap); // Use custom mapping Console.WriteLine(); Console.WriteLine("Single Object Test"); var user1 = conn.Query <User>("SELECT * FROM [dbo].[UsersCompanies] WHERE UserId = 5").First(); Console.WriteLine(user1); // Once mapping is set, it will be used // every time the target object is deserialized Console.WriteLine(); Console.WriteLine("Multiple Object Test"); var user2 = conn.Query <User, Company, Address, User>( "SELECT * FROM [dbo].[UsersCompanies] WHERE UserId = 5", (u, c, a) => { u.Company = c; u.Company.Address = a; return(u); }, splitOn: "CompanyId,Street").First(); Console.WriteLine(user2); Console.WriteLine(); }
public DapperTypeMap(IInsqlEntityMap entityMap) { this.entityMap = entityMap; this.typeMap = new CustomPropertyTypeMap(entityMap.EntityType, this.GetProperty); }
public ColumnTypeMapper(DbContext dbContext, Type type) { _columnMap = GetEFMapping(dbContext, type); _mapper = new CustomPropertyTypeMap(type, SelectProperty); }
public async Task <BarberShop> GetBarberShopById(int id) { using (var connection = new MySqlConnection(_connectionString)) { string command = $@" SELECT barbershop.id as Id, barbershop.name as Name, barbershop.path_logo as PathLogo, barbershop.is_active as IsActive, barbershop.id_admin_user as UserAdminId, address.id as IdAddress, address.public_place as PublicPlace, address.number as Number, address.neighborhood as Neighborhood, address.locality as Locality, address.state as State, address.latitude as Latitude, address.longitude as Longitude, barbershop_status.id as IdBarberShopStatus, barbershop_status.expiration_at as ExpirationDate, plan.Id as IdPlan, plan.name as Name, plan.amount_professional as AmountProfessinal, plan.value as Value, plan_type.id as IdPlanType, plan_type.description as Description, plan_type.days as Days FROM tb_barbershop barbershop INNER JOIN tb_address address ON barbershop.id_address = address.id INNER JOIN tb_barbershop_status barbershop_status ON barbershop.id_barbershop_status = barbershop_status.id INNER JOIN tb_plan plan ON barbershop_status.id_plan = plan.id INNER JOIN tb_plan_type plan_type ON plan.id_plan_type = plan_type.id WHERE barbershop.id = @{nameof(id)}"; var mapAddress = new CustomPropertyTypeMap(typeof(Address), (type, columnName) => { if (columnName == "IdAddress") { return(type.GetProperty("Id")); } throw new InvalidOperationException($"No matching mapping for {columnName}"); }); var mapBarberShopStatus = new CustomPropertyTypeMap(typeof(BarberShopStatus), (type, columnName) => { if (columnName == "IdBarberShopStatus") { return(type.GetProperty("Id")); } throw new InvalidOperationException($"No matching mapping for {columnName}"); }); var mapPlan = new CustomPropertyTypeMap(typeof(Plan), (type, columnName) => { if (columnName == "IdPlan") { return(type.GetProperty("Id")); } throw new InvalidOperationException($"No matching mapping for {columnName}"); }); var mapPlanType = new CustomPropertyTypeMap(typeof(PlanType), (type, columnName) => { if (columnName == "IdPlanType") { return(type.GetProperty("Id")); } throw new InvalidOperationException($"No matching mapping for {columnName}"); }); SqlMapper.SetTypeMap(typeof(Address), mapAddress); SqlMapper.SetTypeMap(typeof(BarberShopStatus), mapBarberShopStatus); SqlMapper.SetTypeMap(typeof(Plan), mapPlan); SqlMapper.SetTypeMap(typeof(PlanType), mapPlanType); var result = (await connection.QueryAsync <BarberShop, Address, BarberShopStatus, Plan, PlanType, BarberShop>( command, (barberShop, address, barberShopStatus, plan, planType) => { plan.PlanType = planType; barberShopStatus.Plan = plan; barberShop.Address = address; barberShop.BarberShopStatus = barberShopStatus; return(barberShop); }, splitOn: "IdAddress,IdBarberShopStatus,IdPlan,IdPlanType")).Distinct().ToList(); return(result.First()); } }