Last active
December 15, 2015 12:19
-
-
Save luisrudge/5259660 to your computer and use it in GitHub Desktop.
some ideas around petapoco external mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Brand | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public int? ParentBrandId { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE [dbo].[tbBrand]( | |
[idBrand] [smallint] IDENTITY(1,1) NOT NULL, | |
[braName] [varchar](100) NOT NULL, | |
[idParentBrand] [smallint] NULL, | |
CONSTRAINT [PK__tbBrand__E353A48E005FFE8A] PRIMARY KEY CLUSTERED | |
( | |
[idBrand] ASC | |
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
) ON [PRIMARY] | |
ALTER TABLE [dbo].[tbBrand] WITH NOCHECK ADD CONSTRAINT [FK_tbBrand_tbBrand] FOREIGN KEY([idParentBrand]) | |
REFERENCES [dbo].[tbBrand] ([idBrand]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BrandMapper : IPetaPocoMapper<Brand> | |
{ | |
public TableInfo GetTableInfo() | |
{ | |
return new TableInfo | |
{ | |
AutoIncrement = true, | |
PrimaryKey = "idBrand", | |
TableName = "tbBrand" | |
}; | |
} | |
public IEnumerable<EntityMapping> GetMapping() | |
{ | |
return new[] | |
{ | |
new EntityMapping { ClassPropertyName = "Id", DatabaseColumnName = "idBrand" }, | |
new EntityMapping { ClassPropertyName = "Name", DatabaseColumnName = "braName"}, | |
new EntityMapping { ClassPropertyName = "ParentBrandId", DatabaseColumnName = "idParentbrand" } | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BrandRepository : PetaPocoRepositoryBase, IBrandRepository | |
{ | |
public Brand GetById(string campaignId, int id) | |
{ | |
//Db = Petapoco.Database | |
return Db.FirstOrDefault<Brand>("WHERE idBrand = @id ", new { id }); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IPetaPocoMapper<T> where T : class | |
{ | |
TableInfo GetTableInfo(); | |
IEnumerable<EntityMapping> GetMapping(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PetaPocoMapper : PetaPoco.IMapper | |
{ | |
private readonly Dictionary<Type, IEnumerable<EntityMapping>> _columnMapping; | |
private readonly Dictionary<Type, TableInfo> _tableInfoMapping; | |
public PetaPocoMapper() | |
{ | |
_columnMapping = new Dictionary<Type, IEnumerable<EntityMapping>>(); | |
_tableInfoMapping = new Dictionary<Type, TableInfo>(); | |
var brandMapper = new BrandMapper(); | |
_tableInfoMapping.Add(typeof(Brand), brandMapper.GetTableInfo()); | |
_columnMapping.Add(typeof(Brand), brandMapper.GetMapping()); | |
} | |
public void GetTableInfo(Type t, TableInfo ti) | |
{ | |
if (!_tableInfoMapping.ContainsKey(t)) return; | |
var tableInfo = _tableInfoMapping[t]; | |
ti.AutoIncrement = tableInfo.AutoIncrement; | |
ti.PrimaryKey = tableInfo.PrimaryKey; | |
ti.SequenceName = tableInfo.SequenceName; | |
ti.TableName = tableInfo.TableName; | |
} | |
public bool MapPropertyToColumn(PropertyInfo pi, ref string columnName, ref bool resultColumn) | |
{ | |
if (pi.DeclaringType == null) return true; | |
if (_columnMapping.ContainsKey(pi.DeclaringType)) | |
{ | |
var mapping = _columnMapping[pi.DeclaringType].FirstOrDefault(m => m.ClassPropertyName.Equals(pi.Name)); | |
if (mapping == null) return true; | |
if (mapping.ShouldBeIgnored) return true; | |
columnName = mapping.DatabaseColumnName; | |
} | |
return true; | |
} | |
public Func<object, object> GetFromDbConverter(PropertyInfo pi, Type SourceType) | |
{ | |
return null; | |
} | |
public Func<object, object> GetToDbConverter(Type SourceType) | |
{ | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment