views:

45

answers:

1

Beyond work, some friends and I are trying to build a game of sorts; The way we structure some of it works pretty well for a normal object oriented approach, but as most developers will attest this does not always translate itself well into a database persistent approach.

This is not the absolute layout of what we have, it is just a sample model given for sake of representation.

The whole project is being done in C# 4.0, and we have every intention of using Entity Framework 4.0 (unless Fluent nHibernate can really offer us something we outright cannot do in EF).

One of the problems we keep running across is inheriting things in database models. Using the Entity Framework designer, I can draw the same code I have below; but I'm sure it is pretty obvious that it doesn't work like it is expected to.

To clarify a little bit; 'Items' have bonuses, which can be of anything. Therefore, every part of the game must derive from something similar so that no matter what is 'changed' it is all at a basic enough level to be hooked into. Sounds fairly simple and straightforward, right?

So then, we inherit everything that pertains to the game from 'Unit'. Weights, Measures, Random (think like dice, maybe?), and there will be other such entities. Some of them are similar, but in code they will each react differently.

We're having a really big problem with abstracting this kind of thing into a database model. Without 'Enum' support, it is proving difficult to translate into multiple tables that still share a common listing. One solution we've depicted is to use a 'key ring' type approach, where everything that attaches to a character is stored on a 'Ring' with a 'Key', where each Key has a Value that represents a type. This works functionally but we've discovered it becomes very sluggish and performs poorly. We also dislike this approach because it begins to feel as if everything is 'dumped' into one class; which makes management and logical structure difficult to adhere to.

I was hoping someone else might have some ideas on what I could do with this problem. It's really driving me up the wall; To summarize; the goal is to build a type (Unit) that can be used as a base type (Table per Type) for generic reference across a relatively global scope, without having to dump everything into a single collection. I can use an Interface to determine actual behavior so that isn't too big of an issue.

alt text

This is 'roughly' the same idea expressed in the Entity Framework.

alt text

+1  A: 

Have you tried a Table per Concrete Type mapping for Unit? i.e. using the Id field to link the Measure, Weights and Random tables? (each has an Id column and they are related through this.)

I think the relationship you show between Sheet and Measure is going to be problematic in a Table per class mapping. Sheet can't map to just Measures if the type type of unit 'measure' is determined by a discriminator column in Units. With TPCT you can map Sheet to Measures successfully because it can use the Id to 'get back' to the Units table.

Unfortunately there's very little by way of examples for TPCT: sometimes it seems to take a few iterations of {edit model, export database, make changes in SSMS, import database} ... to get it right.

Hightechrider
Hrnm, I have examined this and it does actually solve the initial problem. However I am concerned with how this will play out in performance. I think I will make a new topic and elaborate how I am going to do it and ask for input on performance implications. Thank you very much, this is useful.
Stacey