How to effectively / efficiently create polymorphic associations in .NET?
Having said that, I have a few more detailed questions that I would like to see as part of a broader answer.
Technology
- .NET 4.0
- ASP.NET MVC 3
- MS SQL 2008
- C # (last)
- ADO.NET Entity Framework / LINQ-to-Entities
Context
I am developing a consumer-oriented application consisting of a DAL layer, a business object, a service broker layer (for REST services) and, ultimately, a web application, tablet, mobile phone and desktop.
This application includes hundreds of products that correspond to various classifications. In addition, products are made up of various attributes, which may also be an attribute of their broader classifications.
Example:
"Widget A" and "Widget B" are both red, so they can be grouped in the form under "Things that are red." However, “Widget A” is a toy car, while “Widget B” is a red bike, therefore, although both are red objects, they are objects of different types. Thus, they can be grouped differently in other views (for example, "Bicycles", which will show red bicycles, blue bicycles, etc.).
goal
Create an efficient core and service level that responds to the caller and is easily maintained.
What i think to do
To easily manage all of these various attributes and relationships, I was thinking of creating a “global” attribute table where attributes could be registered for objects of different types:
GLOBAL_ATTRIBUTES_TABLE
ID (int)ObjectType (int) - FK to ObjectTypes A table that contains a list of types (for example, bicycle, toy car, etc.)ObjectId (int) - the identifier of the object in its own table (for example, "Bicycle Table")AttributeType (int) - FK to AttributeTypes A table that contains various types of attributes (for example, "Color", "Material", "Age Group").AttributeId (int) - identifier of the attribute in its own table (for example, "Color Table")
So, columns 3 and 5 ( ObjectId and AttributeId ) should ideally have a dynamic foreign key for the table corresponding to their types.
My thinking is that this will speed up the search, building the model is simpler and less detailed (by code), adding future attributes and types of objects is easier, simplifying maintenance, etc.
Questions
Is this an acceptable or good method for tracking (as opposed to creating, for example, product tables, grid tables, etc. with a long list of columns)?
Is there a way to execute dynamic foreign keys / polymorphic associations in .NET by simply making a query, building a model with the results, querying this model, etc.?
Are there any other suggestions for a better data architecture?