Ejemplo n.º 1
0
        /// <summary>
        /// 类似List(int)这样的值成员的集合所对应的表
        /// </summary>
        /// <param name="root"></param>
        /// <param name="master"></param>
        /// <param name="memberField"></param>
        /// <param name="objectType"></param>
        /// <returns></returns>
        private static DataTable CreateValueListTable(DataTable root, DataTable master, IDataField memberField, Type objectType)
        {
            var    valueListField = memberField as ValueListField;
            string tableName      = string.Format("{0}_{1}", master.Name, memberField.Name);

            IDataField[] fields        = null;
            var          reflectedType = valueListField.GetReflectedType(); //就算集合的成员类型各自不同,但是他们肯定继承至同一个根类,因此中间表是统一一个类型的

            if (root.IsEqualsOrDerivedOrInherited(master))
            {
                var rootField  = GetForeignKey(master, GeneratedFieldType.RootKey, DbFieldType.NonclusteredIndex);
                var indexField = GeneratedField.CreateOrderIndex(objectType, DbFieldType.NonclusteredIndex);
                var valueField = GeneratedField.CreatePrimitiveValue(reflectedType, valueListField);
                fields = new IDataField[] { rootField, indexField, valueField };
            }
            else
            {
                var rootField   = GetForeignKey(root, GeneratedFieldType.RootKey, DbFieldType.NonclusteredIndex); //中间表中追加根字段,可以有效防止数据重叠
                var masterField = GetForeignKey(master, GeneratedFieldType.MasterKey, DbFieldType.NonclusteredIndex);
                var indexField  = GeneratedField.CreateOrderIndex(objectType, DbFieldType.NonclusteredIndex);
                var valueField  = GeneratedField.CreatePrimitiveValue(reflectedType, valueListField);

                fields = new IDataField[] { rootField, masterField, indexField, valueField };
            }

            var middle = DataTable.CreateMiddle(root,
                                                master,
                                                tableName,
                                                DataTableType.Middle,
                                                fields,
                                                memberField);

            return(middle);
        }
Ejemplo n.º 2
0
        private static List <IDataField> GetObjectFieldsByNoDerived(Type objectType, bool isSnapshot)
        {
            var domainProperties = Util.GetProperties(objectType);
            var fields           = GetFields(domainProperties, isSnapshot);

            fields.Add(GeneratedField.CreateTypeKey(objectType));
            fields.Add(GeneratedField.CreateDataVersion(objectType));
            return(fields);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="master"></param>
        /// <param name="slave"></param>
        /// <param name="memberField"></param>
        /// <returns></returns>
        private static DataTable CreateMiddleTable(DataTable slave, IDataField memberField)
        {
            var root   = slave.Root;
            var master = slave.Master;

            string tableName = string.Format("{0}_{1}", master.Name, memberField.Name);

            IDataField[] fields     = null;
            var          objectType = (memberField as ObjectField).GetReflectedType(); //就算集合的成员类型各自不同,但是他们肯定继承至同一个根类,因此中间表是统一一个类型的

            if (root.IsEqualsOrDerivedOrInherited(master))
            {
                var rootField  = GetForeignKey(master, GeneratedFieldType.RootKey, DbFieldType.NonclusteredIndex);
                var slaveField = GetForeignKey(slave, GeneratedFieldType.SlaveKey, DbFieldType.NonclusteredIndex);
                slaveField.ParentMemberField = memberField;

                var indexField = GeneratedField.CreateOrderIndex(objectType, DbFieldType.NonclusteredIndex);

                //注意,大多数查询都是以rootField 为条件, indexField为排序,输出slaveField字段,
                //所以slaveField的位置在最后
                fields = new IDataField[] { rootField, indexField, slaveField };
            }
            else
            {
                var rootField   = GetForeignKey(root, GeneratedFieldType.RootKey, DbFieldType.NonclusteredIndex); //中间表中追加根字段,可以有效防止数据重叠
                var masterField = GetForeignKey(master, GeneratedFieldType.MasterKey, DbFieldType.NonclusteredIndex);
                var slaveField  = GetForeignKey(slave, GeneratedFieldType.SlaveKey, DbFieldType.NonclusteredIndex);
                slaveField.ParentMemberField = memberField;

                var indexField = GeneratedField.CreateOrderIndex(objectType, DbFieldType.NonclusteredIndex);

                //注意,大多数查询都是以rootField, masterField为条件, indexField为排序,输出slaveField字段,
                //所以slaveField的位置在最后
                fields = new IDataField[] { rootField, masterField, indexField, slaveField };
            }

            var middle = DataTable.CreateMiddle(root,
                                                master,
                                                tableName,
                                                DataTableType.Middle,
                                                fields,
                                                memberField);

            middle.Slave = slave;

            //如果从表是根,那么需要记录从表和中间表的联系,当删除根对象时,会删除该中间表的数据
            RootIsSlaveIndex.TryAdd(middle);

            return(middle);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 创建实体对象的表
        /// </summary>
        /// <param name="root"></param>
        /// <param name="master"></param>
        /// <param name="memberField"></param>
        /// <param name="isMultiple"></param>
        /// <returns></returns>
        private static DataTable CreateEntityObjectTable(DataTable root, DataTable master, IDataField memberField, Type objectType)
        {
            //注意,在内聚模型中,只要是实体对象,那么它就是相对于内聚根的实体对象,而不是所在对象的实体对象
            //因此,所有的实体对象,外键存放的都是内聚根的编号
            var fields = new List <IDataField>();

            fields.AddRange(DataModel.GetObjectFields(objectType, master.IsSnapshot)); //增加对象定义的领域属性
            if (!objectType.IsDerived())
            {
                //派生类不必有引用次数
                fields.Add(GeneratedField.CreateAssociatedCount(objectType));//追加被引用次数
            }

            return(DataTable.Create(root,
                                    master,
                                    objectType,
                                    master.IsSnapshot,//根据从表,判断是否为快照
                                    DataTableType.EntityObject, fields, memberField));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 创建值对象的表
        /// </summary>
        /// <param name="root"></param>
        /// <param name="master"></param>
        /// <param name="memberField"></param>
        /// <param name="isMultiple"></param>
        /// <returns></returns>
        private static DataTable CreateValueObjectTable(DataTable root, DataTable master, IDataField memberField, Type objectType)
        {
            var fields = new List <IDataField>();

            fields.Add(GeneratedField.CreateValueObjectPrimaryKey(objectType));        //追加主键
            fields.AddRange(DataModel.GetObjectFields(objectType, master.IsSnapshot)); //增加对象定义的领域属性

            if (!objectType.IsDerived())
            {
                //派生类不必追加引用次数
                fields.Add(GeneratedField.CreateAssociatedCount(objectType));//追加被引用次数
            }

            return(DataTable.Create(root,
                                    master,
                                    objectType,
                                    master.IsSnapshot,//根据主表,判断是否为快照
                                    DataTableType.ValueObject,
                                    fields,
                                    memberField));
        }
Ejemplo n.º 6
0
        private IEnumerable <IDataField> MapFields(Type objectType, IEnumerable <DbField> attachedFields)
        {
            List <IDataField> fields = new List <IDataField>();

            foreach (var attachedField in attachedFields)
            {
                var stringField = attachedField as StringField;
                if (stringField != null)
                {
                    var field = GeneratedField.CreateString(objectType, stringField.Name, stringField.MaxLength, stringField.ASCII);
                    field.IsAdditional = true;
                    fields.Add(field);
                }
                else
                {
                    var field = GeneratedField.Create(objectType, attachedField.ValueType, attachedField.Name);
                    field.IsAdditional = true;
                    fields.Add(field);
                }
            }
            return(fields);
        }
Ejemplo n.º 7
0
        private static bool FillFields(List <IDataField> fields, IDataField current)
        {
            string name = string.IsNullOrEmpty(current.Name) ? current.GetPropertyName() : current.Name;

            switch (current.FieldType)
            {
            case DataFieldType.GeneratedField:
            {
                fields.Add(current);         //对于生成的键,直接追加
                return(true);
            }

            case DataFieldType.Value:
            {
                var valueField = current as ValueField;
                //存值
                var field = new ValueField(current.Tip, valueField.DbFieldTypes.ToArray())
                {
                    Name = name,
                    ParentMemberField = current.ParentMemberField
                };
                fields.Add(field);
                return(true);
            }

            //case DataFieldType.ValueList:
            //    {
            //        //普通值的集合,会被转换成逗号分隔的字符串存放,因此作为字段输入
            //        var vlf = current as ValueListField;
            //        var field = new ValueListField(vlf.Tip)
            //        {
            //            Name = name
            //        };
            //        fields.Add(field);
            //        return true;
            //    }
            case DataFieldType.EntityObject:
            case DataFieldType.EntityObjectPro:
            case DataFieldType.AggregateRoot:
            {
                //存外键即可
                var idAttr = DomainProperty.GetProperty(current.Tip.PropertyType, EntityObject.IdPropertyName).RepositoryTip;

                var field = new ValueField(idAttr)
                {
                    Name = _getIdName(name),
                    ParentMemberField = current
                };
                fields.Add(field);
                return(true);
            }

            case DataFieldType.ValueObject:
            {
                var primaryKey = GeneratedField.CreateValueObjectPrimaryKey(current.Tip.PropertyType);
                var field      = new ValueField(primaryKey.Tip)
                {
                    Name = _getIdName(name),
                    ParentMemberField = current
                };
                fields.Add(field);
                return(true);
            }

            default:
            {
                current.Name = name;         //对于其他的类型,只是赋值字段名称
                break;
            }
            }
            return(false);
        }