0

我正在使用实体框架 + mvc 实现域树结构。我使用 JSTree 来呈现组织结构。

到目前为止我的模型方案: 请注意,在该模型方案中,我使用 TypeID 属性作为继承的条件和 DomainEntity 的属性。这当然会引发以下错误:

错误 3 错误 3032:从第 139 行开始映射片段时出现问题:条件成员 'DomainEntities.EntityTypeID' 的条件不是 'IsNull=False' 被映射。删除 DomainEntities.EntityTypeID 上的条件或将其从映射中删除。C:\Code\CamelotShiftManagement\CamelotShiftManagement\Models\CamelotDB.edmx 140 15 CamelotShiftManagement

假设我不会使用 TypeID 作为属性并将其作为继承关联的条件,当我尝试填充我的域实体树时,这将导致以下结果:

            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);
            }

此代码不可维护,因为当引入新实体时,我将不得不更改此代码,如果我只能访问一个属性,该属性将告诉我我正在处理的实体类型.. :) 。

这是一个难题: 如果我使用继承和 TypeID 作为每个继承的条件,我无法将其作为 DomainEntity 的属性访问,这将需要我使用 switch-case 来对抗typof(entity) 来确定我应该发送到什么类型我的 JSTree 插件,因为他希望他的 JSON 中的每个节点都有一个类型标识,如果我不使用继承,我将失去多态能力。

这不仅仅是我所追求的多态性..还有其他方法和属性仅与继承的实体相关,我可以在未来看到一些注入点......

4

0 回答 0