Example #1
0
        private void InitGetRuntimeTable()
        {
            _getRuntimeTable = LazyIndexer.Init <DataTable, Func <string, Func <Type, DataTable> > >((master) =>
            {
                return(LazyIndexer.Init <string, Func <Type, DataTable> >((propertyName) =>
                {
                    return LazyIndexer.Init <Type, DataTable>((propertyType) =>
                    {
                        var memberField = master.ObjectFields.FirstOrDefault((field) =>
                        {
                            return field.GetPropertyName().EqualsIgnoreCase(propertyName);
                        });

                        if (memberField == null)
                        {
                            //如果派生类中找不到子表,那么在基类中继续找
                            if (master.IsDerived)
                            {
                                return GetRuntimeTable(master.BaseTable, propertyName, propertyType);
                            }

                            throw new DataAccessException(string.Format(Strings.NotFoundTableField, master.Name, propertyName));
                        }

                        Type objectType = propertyType.IsList()
                                            ? memberField.Tip.GetElementType()
                                            : propertyType;

                        return CreateChildTable(master, memberField, objectType);
                    });
                }));
            });
        }
Example #2
0
 public AssetDirectory()
 {
     _getFile = LazyIndexer.Init <string, AssetFile>((path) =>
     {
         var assets = this.Assets;
         foreach (var item in assets)
         {
             var file = item as AssetFile;
             if (file != null)
             {
                 if (file.Path == path)
                 {
                     return(file);
                 }
             }
             else
             {
                 var package = item as AssetPackage;
                 if (package != null)
                 {
                     file = package.GetFile(path);
                     if (file != null)
                     {
                         return(file);
                     }
                 }
             }
         }
         return(null);
     });
 }
        public void LazyCache()
        {
            var indexer = new LazyIndexer <int, InternalObject>((id) =>
            {
                return(new InternalObject(id));
            });

            const int instanceCount = 20;

            Parallel.For(0, instanceCount, (id) =>
            {
                //以下代码是在并发状态下运行1000次获取实例的代码,主要测试
                //LazyIndexer.Init在并发状态下也能正常工作
                Parallel.For(0, 1000, (j) =>
                {
                    var obj = indexer.Get(id);
                });
            });

            Assert.AreEqual(indexer.Keys.Length, instanceCount);

            var halfInstanceCount = instanceCount / 2;

            //减去一半
            Parallel.For(0, halfInstanceCount, (id) =>
            {
                Parallel.For(0, 1000, (j) =>
                {
                    indexer.Rmove(id);
                });
            });

            Assert.AreEqual(indexer.Keys.Length, halfInstanceCount);
        }
Example #4
0
 public ObjectChain(IDataField source)
 {
     _source       = source;
     _path         = GetPath(source);
     this.PathCode = GetPathCode();
     _getPathCode  = LazyIndexer.Init <DataTable, string>(GetPathCodeImpl);
 }
 public DirectTable()
 {
     _getAddresses = LazyIndexer.Init <string, string[]>((fullName) =>
     {
         return(_data.GetValues(fullName).ToArray());
     });
 }
Example #6
0
 protected WebPage()
 {
     _getKeyMethod = LazyIndexer.Init <string, MethodInfo>((methodName) =>
     {
         return(this.GetType().ResolveMethod(methodName));
     });
 }
Example #7
0
 static TypeSchemaCodeInfo()
 {
     _getTypeInfo = LazyIndexer.Init <Type, Func <string, TypeSchemaCodeInfo> >((classType) =>
     {
         return(LazyIndexer.Init <string, TypeSchemaCodeInfo>((schemaCode) =>
         {
             return new TypeSchemaCodeInfo(classType, schemaCode);
         }));
     });
 }
Example #8
0
 public TypeIndex()
 {
     _getCompleteName = LazyIndexer.Init <string, string>((typeName) =>
     {
         if (typeName.StartsWith(_rootTypeNameDot))
         {
             return(typeName);                                        //已经包含根路径了
         }
         return(string.Format("{0}.{1}", _rootTypeName, typeName));
     });
 }
        //private Func<Type, MethodInfo> _getSaveData;

        private void InitDataAction()
        {
            _getLoadData = LazyIndexer.Init <Type, MethodInfo>((objectType) =>
            {
                return(Repository.GetMethodFromRepository(objectType, this.LoadMethod));
            });

            //_getSaveData = LazyIndexer.Init<Type, MethodInfo>((objectType) =>
            //{
            //    return Repository.GetMethodFromRepository(objectType, this.SaveMethod);
            //});
        }
 public ExpressionCache(Func <DataTable, string, QueryLevel, T> factory)
 {
     _getInstance = LazyIndexer.Init <DataTable, Func <string, Func <QueryLevel, T> > >((table) =>
     {
         return(LazyIndexer.Init <string, Func <QueryLevel, T> >((expression) =>
         {
             return LazyIndexer.Init <QueryLevel, T>((level) =>
             {
                 return factory(table, expression, level);
             });
         }));
     });
 }
        public void LazyFilter()
        {
            var indexer = new LazyIndexer <int, InternalFilterClass>((id) =>
            {
                return(new InternalFilterClass(id));
            }, (item) =>
            {
                if (item.Number % 2 == 0)
                {
                    return(false);                      //不保留2的倍数
                }
                return(true);
            });

            const int instanceCount = 20;

            Parallel.For(0, instanceCount, (id) =>
            {
                Parallel.For(0, 20, (j) =>
                {
                    var obj = indexer.Get(id);
                });
            });

            foreach (var value in indexer.Values)
            {
                Assert.IsTrue(value.Number % 2 > 0);
            }

            Assert.AreEqual(indexer.Keys.Length, instanceCount / 2);


            for (var i = 0; i < instanceCount; i++)
            {
                var obj = _getFilterInstance(i);
                if (i % 2 == 0)
                {
                    //由于是不缓存的项,所以两次编号不同
                    var obj2 = _getFilterInstance(i);
                    Assert.AreNotEqual(obj.Id, obj2.Id);
                }
                else
                {
                    //由于是缓存的项,所以两次编号相同
                    var obj2 = _getFilterInstance(i);
                    Assert.AreEqual(obj.Id, obj2.Id);
                }
            }
        }
 public ConstructorParameterInfo(ConstructorRepositoryAttribute constructorTip, ParameterInfo original)
 {
     _constructorTip  = constructorTip;
     this.Original    = original;
     this.Tip         = original.GetCustomAttribute <ParameterRepositoryAttribute>(true);
     this.PropertyTip = GetPropertyTip();
     _getLoadData     = LazyIndexer.Init <Type, MethodInfo>((objectType) =>
     {
         if (this.Tip == null || string.IsNullOrEmpty(this.Tip.LoadMethod))
         {
             return(null);
         }
         return(Repository.GetMethodFromRepository(objectType, this.Tip.LoadMethod));
     });
 }
        /// <summary>
        /// 初始化一个 <see cref="MethodGenerator"/> 类的实例.
        /// </summary>
        /// <param name="writer">指令写入器</param>
        private MethodGenerator(ICILWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            var header = writer.MethodHeader;

            //检查非静态方法是否传入了声明类型
            if (header.DeclaringType == null && NotStaticMethod(header))
            {
                throw new ArgumentException("必须为非静态方法定义一个声明类型(方法所在类的类型)");
            }

            //检查参数是否有null
            for (int i = 0; i < header.ParameterTypes.Length; ++i)
            {
                if (header.ParameterTypes[i] == null)
                {
                    throw new ArgumentException("writer.Header.Parameters 不能包含null值", "writer");
                }
            }

            _writer = writer;
            _header = header;
            //收集参数
            _parameters = LazyIndexer.Init <int, Parameter>(parameterIndex =>
            {
                if (NotStaticMethod(_header))
                {
                    ++parameterIndex;                          //如果不是静态方法,那么将参数索引值+1,因为第0个参数是this(也就是对象本身)
                }
                return(new Parameter(this, _writer.GetParameter(parameterIndex)));
            });
            _currentScope = new Scope(this); //创建代码范围
        }
Example #14
0
 private UpdateTable(DataTable target)
     : base(target)
 {
     _cache = new LazyIndexer <int, string>();
 }
Example #15
0
 private void InitSql()
 {
     _getUpdateLRForInsertSqlBySqlServer = LazyIndexer.Init <string, string>(GetUpdateLRForInsertSqlBySqlServer);
     _getUpdateLRForDeleteSqlBySqlServer = LazyIndexer.Init <string, string>(GetUpdateLRForDeleteSqlBySqlServer);
     _getFindParentsSqlBySqlServer       = LazyIndexer.Init <string, string>(GetFindParentsSqlBySqlServer);
 }
 public CalculateNumberCls(int waite)
 {
     _waite     = waite;
     _getNumber = LazyIndexer.Init <string, int>(CalculateNumber);
 }
Example #17
0
 static TypeSerializationInfo()
 {
     _getTypeInfo = LazyIndexer.Init <Type, TypeSerializationInfo>(classType => new TypeSerializationInfo(classType));
 }
 static TypeMakupInfo()
 {
     _getTypeInfo = LazyIndexer.Init <Type, TypeMakupInfo>(classType => new TypeMakupInfo(classType));
 }