0

I'm implementing a domain tree structure using entity framework + mvc. I use JSTree to present the org structure.

My model scheme so far: Note that in that model scheme i use the TypeID property both as a condition of my inheritance and as a property for DomainEntity. This of corse throw the following error:

Error 3 Error 3032: Problem in mapping fragments starting at line 139:Condition member 'DomainEntities.EntityTypeID' with a condition other than 'IsNull=False' is mapped. Either remove the condition on DomainEntities.EntityTypeID or remove it from the mapping. C:\Code\CamelotShiftManagement\CamelotShiftManagement\Models\CamelotDB.edmx 140 15 CamelotShiftManagement

lets say i will not use TypeID as a property and keep it as a condition for the inheritance association that will result in the following when i'll try to populate a tree of my domain entities:

            foreach (var entity in entities)
            {
                JsTreeModel tree = new JsTreeModel()
                {
                    attr = new JsTreeAttribute() 
                    { 
                        id = entity.EntityID.ToString(),

                    },
                    data = entity.EntityName
                };

                if (entity is OrganizatioanlUnit)
                {
                    tree.attr.type = eNodeType.OrganizationalUnit;
                }
                if (entity is Calendar)
                {
                    tree.attr.type = eNodeType.Calendar;
                }

                PopulateTree(entity, tree);
                io_Node.children.Add(tree);
            }

this code is not maintainable because when a new entity will be introduced i will have to change this code, if i could only access a property that will tell me the type of entity i'm dealing with .. :) .

Here is the dilemma: If i use inheritances and TypeID as a condition for each inheritance i can not access it as a property of DomainEntity, that will require me to use switch-case against typof(entity) to determine what type i should send to my JSTree plugin because he expects a type identification for each node in his JSON, if i will not use inheritance i will loose the polymorphic capabilities.

It is not only polymorphism i am after.. there will be other methods and properties relevant only for the inherited entities and i can see some injections points in the future...

Mortalus
  • 10,574
  • 11
  • 67
  • 117
  • How is the example you give related to the choice for types or type Id's? – Gert Arnold Sep 17 '12 at 14:20
  • OMG ... your right thats a mistake... i'll chage it immediately !!! sorry .. – Mortalus Sep 17 '12 at 17:59
  • What is `eNodeType`? It looks like in this case you could simply do `tree.attr.type = entity.GetType().Name;`. – Gert Arnold Sep 17 '12 at 23:15
  • Well eNodeType is an enum representing the diffrent node types i have '{Root, OrganizationalUnit, Calendar}'... you are suggesting that I name my types according to the actual classes representig the entity models ?.. that's intersting it could work.. is this a common practice ? – Mortalus Sep 18 '12 at 06:36
  • I don't know about common practise in this case, but if you need an identifier to keep the types apart this one would certainly do. – Gert Arnold Sep 18 '12 at 06:53
  • What about the approach of Table per hierarchy is this right for the current case, or is there another approach I should consider that will make my life easier.? – Mortalus Sep 18 '12 at 07:00
  • TPT or TPC is usually better than TPH. It depends greatly on the number of shared properties between types (see e.g. http://blogs.msdn.com/b/adonet/archive/2010/10/25/inheritance-mapping-a-walkthrough-guide-for-beginners.aspx) – Gert Arnold Sep 18 '12 at 07:08

0 Answers0