Beispiel #1
0
        private EntityRepository FindOrCreateWithoutLock(Type entityType)
        {
            EntityRepository result = null;

            if (!this._repoByEntityType.TryGetValue(entityType, out result))
            {
                result = this.DoCreate(entityType);

                //仓库完成后,需要把仓库扩展都加入到仓库中。
                this.AddExtensions(result);

                this._repoByEntityType.Add(entityType, result);
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// 填充实体
        /// </summary>
        /// <param name="table">需要转换实体的 LiteDataTable</param>
        /// <param name="repo">实体的仓储</param>
        /// <param name="propertyToColumnMappings">属性和对应列的键值对集合</param>
        /// <param name="columnMapToProperty">
        /// 此参数表示表格中的列的数据类型和是否直接映射实体的属性数据类型。
        /// 如果传入 false,表示表格中的列数据类型和实体属性数据类型不一致。
        /// </param>
        /// <returns></returns>
        private static EntityList ConvertEntitiesIntoList(
            LiteDataTable table,
            EntityRepository repo,
            IList <PropertyToColumnMapping> propertyToColumnMappings,
            bool columnMapToProperty = true
            )
        {
            var resultList = repo.NewList();

            var rdbDataProvider = ORM.RdbDataProvider.Get(repo);
            var columns         = rdbDataProvider.DbTable.Columns;

            for (int i = 0, c = table.Rows.Count; i < c; i++)
            {
                var row = table.Rows[i];

                var entityItem = repo.New();

                for (int j = 0, c2 = propertyToColumnMappings.Count; j < c2; j++)
                {
                    var mapping  = propertyToColumnMappings[j];
                    var rowValue = row[mapping.ColumnIndex];
                    if (!columnMapToProperty)
                    {
                        for (int z = 0, z2 = columns.Count; z < z2; z++)
                        {
                            var column = columns[z];
                            //通过列名称相等匹配找到 rdbcolumn。
                            if (column.Name.EqualsIgnoreCase(mapping.ColumnName))
                            {
                                column.WritePropertyValue(entityItem, rowValue);
                                break;
                            }
                        }
                    }
                    else
                    {
                        entityItem.LoadProperty(mapping.Property, rowValue);
                    }
                }

                resultList.Add(entityItem);
            }

            return(resultList);
        }
Beispiel #3
0
        /// <summary>
        /// 把实体列表对应的 json 转换为 EntityList 对象。
        /// </summary>
        /// <param name="jEntityList"></param>
        /// <param name="repository">如果没有显式指定 Repository,则会根据 json 中的 _model 属性来查找对应的实体仓库。</param>
        /// <param name="resultEntityList">如果显式指定 Repository,则会把转换的实体对象都加入到这个列表中。</param>
        /// <returns></returns>
        internal static EntityList JsonToEntity(JObject jEntityList, EntityRepository repository = null, EntityList resultEntityList = null)
        {
            if (repository == null)
            {
                var modelProperty = jEntityList.Property("_model");
                if (modelProperty == null) { throw new NotSupportedException("实体列表对应的 Json 应该有 _model 属性。"); }
                var model = modelProperty.Value.CastTo<JValue>().Value.CastTo<string>();
                var clientEntity = ClientEntities.Find(model);
                repository = RF.Find(clientEntity.EntityType);
            }

            ListReader reader = repository.SupportTree ? new TreeEntityListReader() : new EntityListReader() as ListReader;
            reader.Repository = repository;
            reader.ChangeSet = jEntityList;
            reader.ResultEntityList = resultEntityList;

            reader.Read();

            return reader.ResultEntityList;
        }
Beispiel #4
0
 /// <summary>
 /// Initializes the specified data provider.
 /// </summary>
 /// <param name="dataProvider">The data provider.</param>
 internal protected virtual void Init(RepositoryDataProvider dataProvider)
 {
     _dataProvider = dataProvider;
     _repository   = dataProvider.Repository;
 }
        /// <summary>
        /// 通过仓库类型查找指定的仓库。
        /// </summary>
        /// <param name="repositoryType"></param>
        /// <returns></returns>
        public IRepositoryInternal Find(Type repositoryType)
        {
            var last = this._lastRepository;
            if (last != null && last.GetType() == repositoryType) return last;

            EntityRepository result = null;

            if (!this._repoByType.TryGetValue(repositoryType, out result))
            {
                lock (this._repoByType)
                {
                    if (!this._repoByType.TryGetValue(repositoryType, out result))
                    {
                        var matrix = EntityMatrix.FindByRepository(repositoryType);
                        var entityType = matrix.EntityType;
                        result = this.FindByEntity(entityType) as EntityRepository;

                        this._repoByType.Add(repositoryType, result);
                    }
                }
            }

            this._lastRepository = result;

            return result;
        }
        /// <summary>
        /// 为指定的仓库加载所有的仓库扩展类。
        /// </summary>
        /// <param name="repository"></param>
        private void AddExtensions(EntityRepository repository)
        {
            var list = new List<IRepositoryExt>();

            this.LoadAllExtensions();

            //把对该类及它的基类的扩展都加入进来。
            var baseTypes = TypeHelper.GetHierarchy(repository.GetType(), typeof(EntityRepositoryQueryBase)).Reverse().ToArray();
            foreach (var baseType in baseTypes)
            {
                List<Type> extList = null;
                if (this._extByType.TryGetValue(baseType, out extList))
                {
                    foreach (var extType in extList)
                    {
                        var ext = CreateExt(extType);
                        list.Add(ext);

                        (ext as IRepositoryExtInternal).BindRepository(repository);
                    }
                }
            }

            repository.Extensions = new ReadOnlyCollection<IRepositoryExt>(list);
        }
        /// <summary>
        /// 用于查找指定实体的仓库。
        /// 如果还没有创建,则直接创建一个。
        /// </summary>
        /// <param name="entityType"></param>
        /// <returns></returns>
        public IRepositoryInternal FindByEntity(Type entityType)
        {
            var last = this._lastRepository;
            if (last != null && last.EntityType == entityType) return last;

            EntityRepository result = null;

            if (!this._repoByEntityType.TryGetValue(entityType, out result))
            {
                //只有实体才能有对应的仓库类型。
                if (entityType.HasMarked<RootEntityAttribute>() ||
                    entityType.HasMarked<ChildEntityAttribute>())
                {
                    lock (this._repoByEntityType)
                    {
                        result = this.FindOrCreateWithoutLock(entityType);
                    }
                }
            }

            this._lastRepository = result;

            return result;
        }
Beispiel #8
0
 /// <summary>
 /// 通过数据库配置名的代理:实体仓库,构造一个 单连接事务块。
 /// </summary>
 /// <param name="dbDelegate"></param>
 /// <returns></returns>
 public static SingleConnectionTransactionScope TransactionScope(EntityRepository dbDelegate)
 {
     return(new SingleConnectionTransactionScope(RdbDataProvider.Get(dbDelegate).DbSetting));
 }
Beispiel #9
0
        /// <summary>
        /// 重新设置整个表的所有 TreeIndex。
        /// 注意,此方法只保证生成的 TreeIndex 有正确的父子关系,同时顺序有可能被打乱。
        /// </summary>
        /// <param name="repository"></param>
        public static void ResetTreeIndex(EntityRepository repository)
        {
            using (var tran = RF.TransactionScope(repository))
            {
                //先清空
                var dp     = repository.RdbDataProvider;
                var table  = dp.DbTable;
                var column = table.Columns.First(c => (c as DbColumn).Property == Entity.TreeIndexProperty);
                using (var dba = dp.CreateDbAccesser())
                {
                    dba.ExecuteText(string.Format("UPDATE {0} SET {1} = NULL", table.Name, column.Name));
                }

                var all = repository.GetAll();
                if (all.Count > 0)
                {
                    //如果加载的过程中,第一个节点刚好是根节点,
                    //则加载完成后是一棵完整的树,Index 也生成完毕,不需要再次处理。
                    if (all.IsTreeRootList)
                    {
                        all.ResetTreeIndex();
                        repository.Save(all);
                    }
                    else
                    {
                        var cloneOptions = CloneOptions.ReadSingleEntity();
                        var oldList      = new List <Entity>();
                        all.EachNode(e =>
                        {
                            var cloned = repository.New();
                            cloned.Clone(e, cloneOptions);
                            cloned.PersistenceStatus = PersistenceStatus.Unchanged;

                            oldList.Add(cloned);
                            return(false);
                        });

                        var newList = repository.NewList();
                        while (oldList.Count > 0)
                        {
                            foreach (var item in oldList)
                            {
                                var treePId = item.TreePId;
                                if (treePId == null)
                                {
                                    newList.Add(item);
                                    oldList.Remove(item);
                                    break;
                                }
                                else
                                {
                                    var parent = newList.EachNode(e => e.Id.Equals(treePId));
                                    if (parent != null)
                                    {
                                        parent.TreeChildren.LoadAdd(item);
                                        oldList.Remove(item);
                                        break;
                                    }
                                }
                            }
                        }
                        TreeHelper.MarkTreeFullLoaded(newList);
                        newList.ResetTreeIndex();
                        repository.Save(newList);
                    }
                }

                tran.Complete();
            }
        }
Beispiel #10
0
 protected RedundanciesUpdater(RepositoryDataProvider dataProvier)
 {
     _repository = dataProvier.Repository;
 }
Beispiel #11
0
 internal static EntityList JsonToEntityList(JObject jEntityList, EntityRepository repository = null)
 {
     return ListReader.JsonToEntity(jEntityList, repository);
 }
Beispiel #12
0
 /// <summary>
 /// Initializes the specified data provider.
 /// </summary>
 /// <param name="dataProvider">The data provider.</param>
 protected internal virtual void Init(RepositoryDataProvider dataProvider)
 {
     _dataProvider = dataProvider;
     _repository = dataProvider.Repository;
 }
Beispiel #13
0
 protected RedundanciesUpdater(RepositoryDataProvider dataProvier)
 {
     _repository = dataProvier.Repository;
 }
        internal void InitRepository(EntityRepository repository)
        {
            _repository = repository;

            if (_dataSaver != null) { _dataSaver.Init(this); }
            if (_dataQueryer != null) { _dataQueryer.Init(this); }
        }
        /// <summary>
        /// 填充实体
        /// </summary>
        /// <param name="liteDataTable">需要转换实体的 LiteDataTable</param>
        /// <param name="repo">实体的仓储</param>
        /// <param name="propertyToColumnMappings">属性和对应列的键值对集合</param>
        /// <returns></returns>
        private static EntityList ConvertEntitiesIntoList(LiteDataTable liteDataTable, EntityRepository repo, IList <PropertyToColumnMapping> propertyToColumnMappings)
        {
            var resultList = repo.NewList();

            for (int i = 0, c = liteDataTable.Rows.Count; i < c; i++)
            {
                var row = liteDataTable.Rows[i];

                var entityItem = repo.New();
                for (int j = 0, c2 = propertyToColumnMappings.Count; j < c2; j++)
                {
                    var mapping  = propertyToColumnMappings[j];
                    var rowValue = row[mapping.ColumnName];

                    entityItem.LoadProperty(mapping.Property, rowValue);
                }

                resultList.Add(entityItem);
            }

            return(resultList);
        }