hhh3112
hhh3112

Reputation: 2187

Class structure and database structure for a application

I want to allow the user to create his own entity, for example: - Student (Name, Age, School, Photo etc.) - Animal (Name, Type, Country etc.) - Car (Brand, Color, Price etc.) etc.

and then the user will be able to add records in the db according to the entity created.

All the properties will be strings.

My question is how can i save instances of these entities best in a database. Creating a new table for each entity created i think is out of the question. I was thinking saving in a table the properties for every entity created, and then in another table, instances of this entities, the properties will be separated be a comma for example;

Entity structure table (for student):

property_name    entity_key

name             student
age              student
school           student
photo            student

Entity instances table :

instance                entity_key

joe,19,nyhigh,joe.jpg   student

What about the classes with whom i would create instances of these records? (?auto generated classes?) (?a class with a List property in which i would separate the 'joe,12,nyhigh,joe.jpg' string?) Has anyone met with this type of problem before?

I will develop the application in asp.net C#.

Upvotes: 2

Views: 851

Answers (3)

Magnus
Magnus

Reputation: 46929

Serialize the entity and save it in the instance column. If you need to do DB queries against the values, use XML serialization.

public static string SerializeToString(object obj)
{
   using (var serializer = new XmlSerializer(obj.GetType()))
   using (var writer = new StringWriter())
   {
      serializer.Serialize(writer, obj);
      return writer.ToString();
   }
}

Upvotes: 1

Proviance
Proviance

Reputation: 59

You will need to create a name value pair table and have it point to a student table

Student

ID int
FirstName nvarchar(50)
LastName nvarchar(50)

NameValue

ID int PK
Name nvarchar(50)
StudentID int FK
Value nvarchar(50)

you will find quering the database will turn into a real hassle as you will need to pivot everything out of it. But if each of your objects are truly unique then pivoting will not help.

Upvotes: 0

Micah Armantrout
Micah Armantrout

Reputation: 6971

you could make a Entity type table and a Entity property table and relate the two of them together. If all your properties are strings. If they are not you can also store type in the entity property table.

Upvotes: 0

Related Questions