Exemple #1
0
        /// <summary>
        /// 获取对象数据
        /// </summary>        
        /// <param name="domainModel">领域模型</param>
        /// <param name="domainObject">领域对象</param>
        /// <param name="queryFilter">查询条件</param>        
        /// <returns>查询结果</returns>
        public QueryResultSet GetData(NSharding.DomainModel.Spi.DomainModel domainModel, DomainObject domainObject, QueryFilter queryFilter)
        {
            if (domainModel == null)
                throw new ArgumentNullException("DataQueryService.GetData.domainModel");
            if (queryFilter == null)
                throw new ArgumentNullException("DataQueryService.GetData.queryFilter");

            var sqls = SQLBuilderFactory.CreateSQLBuilder(domainModel).ParseQuerySqlByFilter(domainModel, domainObject, queryFilter);

            var db = DatabaseFactory.CreateDatabase(domainModel);
            var dts = db.GetDataCollection(sqls);

            return new QueryResultSet { ShardingInfo = sqls.ShardingInfo, DataTables = dts };
        }
Exemple #2
0
        /// <summary>
        /// 获取对象数据
        /// </summary>        
        /// <param name="domainModel">领域模型</param>
        /// <param name="dataID">数据唯一标识</param>
        /// <param name="shardingValue">分库分表键值对</param>
        /// <returns>对象数据</returns>
        public QueryResultSet GetData(NSharding.DomainModel.Spi.DomainModel domainModel, string dataID, ShardingValue shardingValue = null)
        {
            if (domainModel == null)
                throw new ArgumentNullException("DataQueryService.GetData.domainModel");
            if (string.IsNullOrWhiteSpace(dataID))
                throw new ArgumentNullException("DataQueryService.GetData.dataID");

            var sqls = SQLBuilderFactory.CreateSQLBuilder(domainModel).ParseQuerySqlByID(domainModel, dataID, shardingValue);

            var db = DatabaseFactory.CreateDatabase(domainModel);
            var dts = db.GetDataCollection(sqls);

            return new QueryResultSet { ShardingInfo = sqls.ShardingInfo, DataTables = dts };
        }
Exemple #3
0
        /// <summary>
        /// 领域对象校验
        /// </summary>
        /// <param name="model">领域对象</param>
        private void DomainModelValidate(NSharding.DomainModel.Spi.DomainModel model)
        {
            foreach (var domainObject in model.DomainObjects)
            {
                if (string.IsNullOrWhiteSpace(domainObject.DataObjectID))
                {
                    throw new Exception(string.Format("DomainObject:{0}, can not find DataObject.", model.Name));
                }

                if (domainObject.DataObject == null)
                {
                    domainObject.DataObject = DataObjectManageService.GetInstance().GetDataObject(domainObject.DataObjectID);
                }
            }
        }
Exemple #4
0
        private ResultMappingItem CreateInnerAssoMapping(NSharding.DomainModel.Spi.DomainModel model, DomainObject currentDomainObject, Association association)
        {
            var item = new ResultMappingItem()
            {
                Property              = association.PropertyName,
                TypeHandler           = association.PropertyType,
                ItemType              = ResultMappingItemType.SubResultMapping,
                ResultMapping         = CreateDomainObjectMapping(currentDomainObject.Name, model, currentDomainObject),
                ParentDomainObjectId  = currentDomainObject.ParentObjectID,
                CurrentDomainObjectId = currentDomainObject.ID,
                AssociationId         = association.ID
            };

            return(item);
        }
Exemple #5
0
        /// <summary>
        /// 循环构造结果集映射
        /// </summary>
        /// <param name="parentObject">父对象</param>
        /// <param name="mapping">已有结果集映射</param>
        /// <param name="currentObject">当前对象</param>
        private void LoopCreateResultMapping(NSharding.DomainModel.Spi.DomainModel model, DomainObject parentObject, ResultMapping mapping, DomainObject currentObject)
        {
            var subMapping     = CreateDomainObjectMapping(currentObject.Name, model, currentObject);
            var subMappingItem = new ResultMappingItem()
            {
                //Property = currentObject.PropertyName,
                LazyLoad      = currentObject.IsLazyLoad,
                ResultMapping = subMapping,
                ItemType      = ResultMappingItemType.SubResultMapping
            };
            var innerAsso = parentObject.Associations.FirstOrDefault(
                i => i.AssociateType == AssociateType.InnerJoin && i.AssoDomainObjectID == currentObject.ID);

            if (innerAsso != null)
            {
                var parentElementID = innerAsso.Items.FirstOrDefault().SourceElementID;
                subMappingItem.Property      = innerAsso.PropertyName;
                subMappingItem.GroupbyColumn = parentObject.DataObject.Columns.FirstOrDefault(c =>
                                                                                              c.ID == parentObject.Elements.FirstOrDefault(i => i.ID == parentElementID).DataColumnID).ColumnName;
            }
            mapping.MappingItems.Add(subMappingItem);

            //外键关联
            var leftAssociations = parentObject.Associations.Where(i => i.AssociateType == AssociateType.OuterLeftJoin);

            foreach (var asso in leftAssociations)
            {
                var assoMapping = CreateAssociationMapping(asso);
                mapping.MappingItems.Add(new ResultMappingItem()
                {
                    Property      = asso.PropertyName,
                    LazyLoad      = asso.IsLazyLoad,
                    ResultMapping = assoMapping,
                    ItemType      = ResultMappingItemType.ForeignResultMapping
                });
            }

            if (currentObject.ChildDomainObjects.Count > 0)
            {
                foreach (var obj in currentObject.ChildDomainObjects)
                {
                    LoopCreateResultMapping(model, currentObject, subMapping, obj);
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// 构造领域对象结果集映射
        /// </summary>
        /// <param name="model">领域对象</param>
        /// <returns>结果集映射</returns>
        private ResultMapping CreateResultMapping(NSharding.DomainModel.Spi.DomainModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("ResultMappingService.GetResultMapping.model");
            }

            DomainModelValidate(model);

            var rootObject = model.RootDomainObject;
            var mapping    = CreateDomainObjectMapping(model.Name, model, rootObject);

            foreach (var domainObject in rootObject.ChildDomainObjects)
            {
                LoopCreateResultMapping(model, rootObject, mapping, domainObject);
            }

            return(mapping);
        }
Exemple #7
0
        public IORMPlugin GetOrCreatePlugin(NSharding.DomainModel.Spi.DomainModel domainModel)
        {
            if (domainModel == null)
            {
                throw new ArgumentNullException("ORMPluginFactory.GetOrCreatePlugin.domainModel");
            }
            if (string.IsNullOrEmpty(domainModel.DataLoaderConfig))
            {
                return(defaultPlugin);
            }

            Type type = null;

            if (!plugins.ContainsKey(domainModel.ID))
            {
                lock (syncObj)
                {
                    if (!plugins.ContainsKey(domainModel.ID))
                    {
                        type = Type.GetType(domainModel.DataLoaderConfig);
                        if (type == null)
                        {
                            throw new TypeAccessException(domainModel.DataLoaderConfig);
                        }

                        var pluginObj = type.Assembly.CreateInstance(type.FullName, true);
                        if (pluginObj == null)
                        {
                            throw new Exception("反射创建类型为空:" + type.FullName);
                        }
                        var plugin = pluginObj as IORMPlugin;
                        if (plugin == null)
                        {
                            throw new Exception("反射创建对象未实现IORMPlugin:" + pluginObj.GetType().FullName);
                        }

                        plugins.Add(domainModel.ID, plugin);
                    }
                }
            }

            return(plugins[domainModel.ID]);
        }
Exemple #8
0
        /// <summary>
        /// 获取领域模型的结果集映射
        /// </summary>
        /// <param name="model">领域模型</param>
        /// <returns>结果集映射</returns>
        public ResultMapping CreateOrGetResultMapping(NSharding.DomainModel.Spi.DomainModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("ResultMappingFactory.CreateOrGetResultMapping.model");
            }

            var           key     = string.Format("{0}_{1}", model.ID, model.Version);
            ResultMapping mapping = null;

            if (!resultMappingDic.ContainsKey(key))
            {
                lock (syncObj)
                {
                    if (!resultMappingDic.ContainsKey(key))
                    {
                        mapping = CreateResultMapping(model);
                        if (mapping == null)
                        {
                            throw new Exception(key + " ResultMapping is null!");
                        }

                        resultMappingDic.TryAdd(key, mapping);
                    }
                    else
                    {
                        mapping = resultMappingDic[key];
                    }
                }
            }
            else
            {
                mapping = resultMappingDic[key];
            }

            if (mapping == null)
            {
                throw new Exception(key + " ResultMapping is null!");
            }

            return(mapping);
        }
Exemple #9
0
        public object GetForeignObjects(DataRow row, NSharding.DomainModel.Spi.DomainModel model, ResultMappingItem mappingItem, DomainObject domainObject, Association forAssociation)
        {
            var type  = ORMAssemblyContainer.GetInstance().GetObjectType(forAssociation.AssoDomainObject.ClazzReflectType);
            var obj   = ORMAssemblyContainer.GetInstance().CreateInstance(forAssociation.AssoDomainObject.ClazzReflectType);
            var props = type.GetProperties();

            foreach (var item in forAssociation.Items)
            {
                var targetElement = domainObject.Elements.FirstOrDefault(i => i.ID == item.TargetElementID);
                MappingCommonProperty(row, obj, props, mappingItem.ResultMapping.MappingItems.FirstOrDefault(i => i.Column == targetElement.Alias), domainObject);
            }

            foreach (var refElement in forAssociation.RefElements)
            {
                var targetElement = domainObject.Elements.FirstOrDefault(i => i.ID == refElement.ElementID);
                MappingCommonProperty(row, obj, props, mappingItem.ResultMapping.MappingItems.FirstOrDefault(i => i.Column == targetElement.Alias), domainObject);
            }

            return(obj);
        }
Exemple #10
0
        public List <object> GetSubObjects(QueryResultSet resultSet, NSharding.DomainModel.Spi.DomainModel model, DomainObject domainObject, ResultMappingItem mappingItem)
        {
            var result = new List <object>();
            var type   = ORMAssemblyContainer.GetInstance().GetObjectType(domainObject.ClazzReflectType);
            var props  = type.GetProperties();

            var resultMapping = mappingItem.ResultMapping;
            var dataTable     = resultSet.GetDataTable(domainObject.DataObjectID);

            for (int r = 0; r < dataTable.Rows.Count; r++)
            {
                var row = dataTable.Rows[r];
                var obj = ORMAssemblyContainer.GetInstance().CreateInstance(domainObject.ClazzReflectType);
                foreach (var item in resultMapping.MappingItems)
                {
                    switch (item.ItemType)
                    {
                    case ResultMappingItemType.Normal:
                        MappingCommonProperty(row, obj, props, item, domainObject);
                        break;

                    case ResultMappingItemType.Enum:
                        var element = domainObject.Elements.FirstOrDefault(i => i.PropertyName == item.Property);
                        MappingEnumProperty(row, obj, props, item, element.PropertyType);
                        break;

                    case ResultMappingItemType.Virtual:
                        var virtualElement = domainObject.Elements.FirstOrDefault(i => i.PropertyName == item.Property);
                        MappingVirtualProperty(virtualElement, obj, props, item);
                        break;
                    }

                    result.Add(obj);
                }
            }

            return(result);
        }
Exemple #11
0
        /// <summary>
        /// 创建领域对象结果集映射
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="domainObject">领域对象</param>
        /// <returns>结果集映射</returns>
        private ResultMapping CreateDomainObjectMapping(string name, NSharding.DomainModel.Spi.DomainModel model, DomainObject domainObject)
        {
            var mapping = new ResultMapping()
            {
                DomainObject = name,
                ClassType    = domainObject.ClazzReflectType
            };

            //遍历对象自身的元素
            foreach (var element in domainObject.Elements)
            {
                switch (element.ElementType)
                {
                case ElementType.Normal:
                    var item = CreateCommonMappingItem(domainObject, element);
                    mapping.MappingItems.Add(item);
                    break;

                case ElementType.Virtual:
                    var virtualItem = CreateVirtualMappingItem(domainObject, element);
                    mapping.MappingItems.Add(virtualItem);
                    break;

                case ElementType.Enum:
                    var enumItem = CreateEnumMappingItem(domainObject, element);
                    mapping.MappingItems.Add(enumItem);
                    break;

                default:
                    break;
                }
            }

            //主子关联
            var childObjects = model.DomainObjects.Where(i => i.ParentObjectID == domainObject.ID);

            if (childObjects.Count() > 0)
            {
                foreach (var child in childObjects)
                {
                    var asso     = child.Associations.FirstOrDefault(i => i.AssociateType == AssociateType.InnerJoin && i.AssoDomainObjectID == domainObject.ID);
                    var assoItem = CreateInnerAssoMapping(model, child, asso);
                    mapping.MappingItems.Add(assoItem);
                }
            }

            //外键关联 TODO
            var leftAssociations = domainObject.Associations.Where(i => i.AssociateType == AssociateType.OuterLeftJoin && i.AssoDomainObjectID == domainObject.ID);

            foreach (var asso in leftAssociations)
            {
                var assoMapping = CreateAssociationMapping(asso);
                mapping.MappingItems.Add(new ResultMappingItem()
                {
                    Property      = asso.PropertyName,
                    LazyLoad      = asso.IsLazyLoad,
                    ResultMapping = assoMapping,
                    ItemType      = ResultMappingItemType.ForeignResultMapping
                });
            }

            return(mapping);
        }
Exemple #12
0
        public object MapToObject(QueryResultSet resultSet, NSharding.DomainModel.Spi.DomainModel model, DomainObject domainObject)
        {
            if (domainObject == null)
            {
                domainObject = model.RootDomainObject;
            }

            var obj           = ORMAssemblyContainer.GetInstance().CreateInstance(domainObject.ClazzReflectType);
            var type          = ORMAssemblyContainer.GetInstance().GetObjectType(domainObject.ClazzReflectType);
            var props         = type.GetProperties();
            var resultMapping = resultMappingService.GetResultMapping(model);

            var dataTable = resultSet.GetDataTable(domainObject.DataObjectID);
            var firstRow  = dataTable.Rows[0];

            var mappingItems = new List <ResultMappingItem>();

            if (domainObject.IsRootObject)
            {
                mappingItems = resultMapping.MappingItems.Where(i => i.ParentDomainObjectId == domainObject.ID).ToList();
            }
            else
            {
                var items = resultMapping.MappingItems.Where(i => i.CurrentDomainObjectId == domainObject.ID);
                foreach (var item in items)
                {
                    if (item.ItemType == ResultMappingItemType.SubResultMapping)
                    {
                        mappingItems.AddRange(item.ResultMapping.MappingItems);
                    }
                    else
                    {
                        mappingItems.Add(item);
                    }
                }
            }

            foreach (var item in mappingItems)
            {
                switch (item.ItemType)
                {
                case ResultMappingItemType.Normal:
                    MappingCommonProperty(firstRow, obj, props, item, domainObject);
                    break;

                case ResultMappingItemType.Enum:
                    var element = domainObject.Elements.FirstOrDefault(i => i.PropertyName == item.Property);
                    MappingEnumProperty(firstRow, obj, props, item, element.PropertyType);
                    break;

                case ResultMappingItemType.Virtual:
                    var virtualElement = domainObject.Elements.FirstOrDefault(i => i.PropertyName == item.Property);
                    MappingVirtualProperty(virtualElement, obj, props, item);
                    break;

                case ResultMappingItemType.SubResultMapping:
                    var currentObject = model.DomainObjects.FirstOrDefault(i => i.ID == item.CurrentDomainObjectId);
                    var subObjects    = GetSubObjects(resultSet, model, currentObject, item);
                    var prop          = props.FirstOrDefault(i => i.Name == item.Property);
                    var itemType      = Type.GetType(currentObject.ClazzReflectType);
                    var listType      = typeof(List <>).MakeGenericType(itemType);
                    var objectList    = Activator.CreateInstance(listType);
                    var method        = listType.GetMethod("Add", new Type[] { itemType });
                    foreach (var o in subObjects)
                    {
                        method.Invoke(objectList, new object[] { o });
                    }

                    prop.SetValue(obj, objectList);
                    break;

                case ResultMappingItemType.ForeignResultMapping:
                    var forasso   = domainObject.Associations.FirstOrDefault(i => i.PropertyName == item.Property);
                    var forObject = GetForeignObjects(firstRow, model, item, model.RootDomainObject, forasso);
                    props.FirstOrDefault(i => i.Name == item.Property).SetValue(obj, forObject);
                    break;

                default:
                    break;
                }
            }

            return(obj);
        }