Search code examples
c#databaserelational-databaseobject-persistence

Saving any kind of object in relational database


It may be a little fancy but is there any pattern or something to store any kind of Object in Relational database (not nosql) in optimal way?

for example in none optimal way:

class Person{
    string FirstName {get;set;}
    string LastName {get;set;}
} 

class Product{
    string Name {get;set;}
    decimal Price {get;set;}
} 

and in database:

CREATE TABLE Data
   (Id int PRIMARY KEY, 
    TypeName nvarchar(50), 
    PropertyName nvarchar(50), 
    PropertyValue binary)

then records gets stored in database like this:

1    Person    FirstName   Jalal

2    Person    LastName    A.R

3    Product   Name        Apple

4    Product   Price       2

Solution

  • What you are describing is essentially an Entity-Attribute-Value table.

    It is appropriately used in those cases where the number of potential attributes is large, but the number of potential values is small. An example of such a use case is medical data (symptoms), where the number of potential symptoms a patient can have is large, but the number of actual symptoms is small.

    Improperly used, it is a classic example of Inner Platform Effect.