Example #1
0
        internal IDbSet GetDbSet(Type type)
        {
            IDbSet set;

            if (!dbSetTable.TryGetValue(type, out set))
            {
                set = this.GetDbSet(dbConfiguration.GetMapping(type));
                lock (dbSetTable)
                    dbSetTable[type] = set;
            }
            return(set);
        }
Example #2
0
        private void InitializeDbSets()
        {
            var type = GetType();

            if (type == typeof(InternalDbContext))
            {
                return;
            }

            List <MemberInitializer> items;

            if (!MemberInitializersGroup.TryGetValue(DbConfiguration.Name, out items))
            {
                items = new List <MemberInitializer>();
                foreach (var item in type.GetFields().Where(p => typeof(IDbSet).IsAssignableFrom(p.FieldType)))
                {
                    var initiazer = new MemberInitializer();
                    initiazer.EntityModel     = DbConfiguration.GetMapping(ReflectionHelper.GetElementType(item.FieldType));
                    initiazer.Setter          = item.GetSetter();
                    initiazer.DbSetConstrctor = typeof(DbSet <>).MakeGenericType(initiazer.EntityModel.EntityType).GetConstructors().FirstOrDefault().GetCreator();
                    items.Add(initiazer);
                }
                foreach (var item in type.GetProperties().Where(p => typeof(IDbSet).IsAssignableFrom(p.PropertyType)).Where(p => p.CanWrite))
                {
                    var initiazer = new MemberInitializer();
                    initiazer.EntityModel     = DbConfiguration.GetMapping(ReflectionHelper.GetElementType(item.PropertyType));
                    initiazer.Setter          = item.GetSetter();
                    initiazer.DbSetConstrctor = typeof(DbSet <>).MakeGenericType(initiazer.EntityModel.EntityType).GetConstructors().FirstOrDefault().GetCreator();
                    items.Add(initiazer);
                }
                lock (MemberInitializersGroup)
                    MemberInitializersGroup[DbConfiguration.Name] = items;
            }

            foreach (var item in items)
            {
                item.Init(this);
            }
        }