benjaminvr

Mapping class & properties to DB representation w/ Attributes & Reflection

Oct 8th, 2021 (edited)
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. // Using Attributes & Reflection to create a table map of a class (Class name: table name + Property identifier : column name), for when you don't want to use something better like an ORM / Entity Framework, I guess?
  2. // Example output at bottom
  3.  
  4. // Attribute to import into your classes you want to make a tablemap for
  5. // for example: using DomainLayer.Attributes;
  6. public sealed class TableMapAttribute : Attribute
  7.     {
  8.         public string ColumnName { get; set; }
  9.         public string TableName { get; set; }
  10.  
  11.         public TableMapAttribute(string? Name)
  12.         {
  13.             if(Name != null) ColumnName = Name;
  14.         }
  15.  
  16.         public TableMapAttribute(string? tableName, bool isTableName=true)
  17.         {
  18.             if (tableName != null) TableName = tableName;
  19.         }
  20.     }
  21.  
  22. // On the class level, define the table name
  23. // For each property you want to include, define the column name
  24. namespace BusinessLaag
  25. {
  26.     [TableMap(tableName:"bestuurder")]
  27.     public class Bestuurder
  28.     {
  29.         [TableMap(Name:"id")] // this is the column identifier
  30.         public int? Id { get; set; }
  31.     }
  32. }
  33.  
  34. // Implement where deemed purposeful, pass any object into the function and it will give you the mappings (if you added any)
  35. // Example output:
  36. // {ClassName : Bestuurder, Bestuurder : bestuurder, Id : id, Naam : naam, Voornaam : voornaam, Adres : adres}
  37. public Dictionary<string, string> GetTableMapping(object myClass)
  38.         {
  39.             Dictionary<string, string> _mapping = new Dictionary<string, string>();
  40.             string ClassName = myClass.GetType().Name;
  41.  
  42.             object[] classAttrs = myClass.GetType().GetCustomAttributes(true);
  43.             foreach(object attr in classAttrs)
  44.             {
  45.                 TableMapAttribute classAttr = attr as TableMapAttribute;
  46.                 try { _mapping.Add(ClassName, classAttr.TableName); } catch (Exception e) { };
  47.             }
  48.  
  49.             PropertyInfo[] props = myClass.GetType().GetProperties();
  50.             foreach (PropertyInfo prop in props)
  51.             {
  52.                 object[] attrs = prop.GetCustomAttributes(true);
  53.                 foreach (object attr in attrs)
  54.                 {
  55.                     TableMapAttribute colAttr = attr as TableMapAttribute;
  56.                     if (colAttr != null)
  57.                     {
  58.                         if (colAttr.ColumnName != null)
  59.                         {
  60.                             string propName = prop.Name;
  61.                             string auth = colAttr.ColumnName;
  62.                             _mapping.Add(propName, auth);
  63.                         }                        
  64.                     }
  65.                 }
  66.             }
  67.  
  68.             foreach (KeyValuePair<string, string> entry in _mapping)
  69.             {
  70.                 if (entry.Key == ClassName) { _mapping.Add("ClassName", ClassName); break; }
  71.             }
  72.  
  73.             return _mapping;
  74. }
Add Comment
Please, Sign In to add comment