jeudi 29 décembre 2016

When and how to use attribute based programming

I have seen many ORM mapping tables/columns to corresponding classes in C# using Custom Attributes. Even entity framework do in a similar way. I am trying to figure out when and how to use Attributes to make my solution more manageable efficient and scalable.

Some of the problem statements I assume it might be useful are 1. Mapping Tables/Views to Classes

Table Employee{ Name, Age, salary, height}...
class Employee{Name, Age, Salary, Height}...

  1. Mapping KeyValue pair/dictionary to class

Table

PersonAttributes{
Column1, Column2
"Age", "25"
"Salary", "3000"
"Weight", "80"
...
}

Class Person{
string Name;

[MapToTable(TableName="PersonAttributes", Key="Column1", Value="Column2",DataType="Int", Field="Age", )]
int Age;

[MapToTable(TableName="PersonAttributes", Key="Column1", Value="Column2",DataType="Int", Field="Weight", )]
int Weight;

[MapToTable(TableName="PersonAttributes", Key="Column1", Value="Column2",DataType="Int", Field="Salary", )]
int Salary;
}





Aucun commentaire:

Enregistrer un commentaire