Пример #1
0
        private static DbContextAccessor CreateDbContextAccessor(Type type)
        {
            DbContextAccessor accessor = null;

            if (IsTypeOf(type, "System.Data.Entity.DbContext"))
            {
                Type type2 = type.GetInterface("System.Data.Entity.Infrastructure.IObjectContextAdapter", false);
                if (type2 != null)
                {
                    PropertyInfo property = type2.GetProperty("ObjectContext", BindingFlags.Public | BindingFlags.Instance);
                    if ((property != null) && (property.GetGetMethod() != null))
                    {
                        MethodInfo method = type.GetMethod("SaveChanges", BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null);
                        if (method != null)
                        {
                            accessor = new DbContextAccessor();
                            ParameterExpression expression = Expression.Parameter(typeof(object));
                            accessor.GetContext  = Expression.Lambda <Func <object, ObjectContext> >(Expression.Property(Expression.Convert(expression, type2), property.GetGetMethod()), new ParameterExpression[] { expression }).Compile();
                            accessor.SaveChanges = Expression.Lambda <Func <object, int> >(Expression.Call(Expression.Convert(expression, type), method), new ParameterExpression[] { expression }).Compile();
                        }
                    }
                }
            }
            return(accessor);
        }
Пример #2
0
 public JudgingFinishedHandler(
     DbContextAccessor db,
     IContestStore contests,
     IProblemsetStore probs)
 {
     Context  = db;
     Contests = contests;
     Problems = probs;
 }
Пример #3
0
    /// <summary>
    /// 创建审计模型集合。
    /// </summary>
    /// <param name="modelBuilder">给定的 <see cref="ModelBuilder"/>。</param>
    /// <param name="accessor">给定的 <see cref="DbContextAccessor"/>。</param>
    /// <returns>返回 <see cref="ModelBuilder"/>。</returns>
    public static ModelBuilder CreateAuditingModels(this ModelBuilder modelBuilder,
                                                    DbContextAccessor accessor)
    {
        var limitableMaxLength = accessor.DataOptions.Store.LimitableMaxLengthOfProperty;
        var mapRelationship    = accessor.DataOptions.Store.MapRelationship;

        modelBuilder.Entity <Audit>(b =>
        {
            b.ToTableWithSharding(accessor.ShardingManager, accessor);

            b.HasIndex(i => new { i.TableName, i.EntityId }).HasDatabaseName();

            b.HasKey(k => k.Id);

            b.Property(p => p.Id).ValueGeneratedNever()
            .Sharding <ModShardingStrategy>(accessor.ShardingManager);

            if (limitableMaxLength > 0)
            {
                b.Property(p => p.TableName).HasMaxLength(limitableMaxLength).IsRequired();
                b.Property(p => p.EntityId).HasMaxLength(limitableMaxLength).IsRequired();
                b.Property(p => p.StateName).HasMaxLength(limitableMaxLength);
                b.Property(p => p.EntityTypeName).HasMaxLength(limitableMaxLength);
            }
        });

        modelBuilder.Entity <AuditProperty>(b =>
        {
            b.ToTableWithSharding(accessor.ShardingManager, accessor);

            b.HasIndex(i => new { i.AuditId, i.PropertyName }).HasDatabaseName();

            b.HasKey(k => k.Id);

            b.Property(x => x.Id).ValueGeneratedNever()
            .Sharding <ModShardingStrategy>(accessor.ShardingManager);

            if (limitableMaxLength > 0)
            {
                b.Property(p => p.AuditId).HasMaxLength(limitableMaxLength).IsRequired();
                b.Property(p => p.PropertyName).HasMaxLength(limitableMaxLength);
                b.Property(p => p.PropertyTypeName).HasMaxLength(limitableMaxLength);
            }

            // MaxLength
            b.Property(p => p.OldValue);
            b.Property(p => p.NewValue);

            if (mapRelationship)
            {
                b.HasOne(f => f.Audit).WithMany(p => p.Properties).HasForeignKey(fk => fk.AuditId);
            }
        });

        return(modelBuilder);
    }
Пример #4
0
 public ContestFacade(
     DbContextAccessor context,
     IContestStore store1,
     IProblemsetStore store2,
     ITeamStore store3,
     ISubmissionStore store4)
 {
     Context     = context;
     Contests    = store1;
     Problemset  = store2;
     Teams       = store3;
     Submissions = store4;
 }
Пример #5
0
        public static ObjectContext GetObjectContext(object o)
        {
            ObjectContext context = o as ObjectContext;

            if (context == null)
            {
                DbContextAccessor dbContextAccessor = GetDbContextAccessor(o.GetType());
                if (dbContextAccessor != null)
                {
                    context = dbContextAccessor.GetContext(o);
                }
            }
            return(context);
        }
Пример #6
0
        /// <summary>
        /// Returns the ObjectContext for a particular instance. Either:
        ///   1. The object is an ObjectContext, or
        ///   2. The object is a DbContext and the ObjectContext can be retrieved
        /// </summary>
        /// <param name="o">The data source instance</param>
        /// <returns>The ObjectContext instance or null</returns>
        public static ObjectContext GetObjectContext(object o)
        {
            ObjectContext objectContext = o as ObjectContext;

            if (objectContext == null)
            {
                DbContextAccessor accessor = GetDbContextAccessor(o.GetType());
                if (accessor != null)
                {
                    objectContext = accessor.GetContext(o) as ObjectContext;
                }
            }

            return(objectContext);
        }
Пример #7
0
 /// <summary>
 /// Returns the ObjectContext and SaveChanges method for a particular instance. Either:
 ///   1. The object is an ObjectContext, or
 ///   2. The object is a DbContext and the ObjectContext can be retrieved
 /// </summary>
 /// <param name="o">The data source instance</param>
 /// <param name="objectContext">The ObjectContext instance or null</param>
 /// <param name="saveChangesMethod">The SaveChanges method or null</param>
 public static void GetObjectContext(object o, out ObjectContext objectContext, out Func <int> saveChangesMethod)
 {
     objectContext     = o as ObjectContext;
     saveChangesMethod = null;
     if (objectContext == null)
     {
         DbContextAccessor accessor = GetDbContextAccessor(o.GetType());
         if (accessor != null)
         {
             objectContext     = accessor.GetContext(o);
             saveChangesMethod = () => { return(accessor.SaveChanges(o)); };
         }
     }
     else
     {
         saveChangesMethod = objectContext.SaveChanges;
     }
 }
Пример #8
0
        /// <summary>
        /// Creates a DbContextAccessor for a given type if it exists
        /// </summary>
        /// <param name="type">The type to create the accessor for</param>
        /// <returns>A DbContextAccessor, or null if one cannot be created for the Type (i.e. it does not inherit from DbContext)</returns>
        private static DbContextAccessor CreateDbContextAccessor(Type type)
        {
            const BindingFlags DbContextBindingFlags         = BindingFlags.Public | BindingFlags.Instance;
            const string       DbContextTypeName             = "System.Data.Entity.DbContext";
            const string       IObjectContextAdapterTypeName = "System.Data.Entity.Infrastructure.IObjectContextAdapter";

            DbContextAccessor accessor = null;
            bool derivesFromDbContext  = IsTypeOf(type, DbContextTypeName);

            if (derivesFromDbContext)
            {
                Type contextAdapterInterface = type.GetInterface(IObjectContextAdapterTypeName, false);
                if (contextAdapterInterface != null)
                {
                    PropertyInfo contextProperty = contextAdapterInterface.GetProperty("ObjectContext", DbContextBindingFlags);
                    if (contextProperty != null && contextProperty.GetGetMethod() != null)
                    {
                        MethodInfo saveChanges = type.GetMethod("SaveChanges", DbContextBindingFlags, null, Type.EmptyTypes, null);
                        if (saveChanges != null)
                        {
                            accessor = new DbContextAccessor();

                            // Generate the ObjectContext getter
                            ParameterExpression contextParameter = Expression.Parameter(typeof(object));

                            accessor.GetContext = Expression.Lambda <Func <object, ObjectContext> >(
                                Expression.Property(
                                    Expression.Convert(contextParameter, contextAdapterInterface),
                                    contextProperty.GetGetMethod()),
                                contextParameter).Compile();

                            // Generate the SaveChanges method
                            accessor.SaveChanges = Expression.Lambda <Func <object, int> >(
                                Expression.Call(
                                    Expression.Convert(contextParameter, type),
                                    saveChanges),
                                contextParameter).Compile();
                        }
                    }
                }
            }

            return(accessor);
        }
Пример #9
0
        public static ModelBuilder CreateUserModel(this ModelBuilder modelBuilder,
                                                   DbContextAccessor accessor)
        {
            modelBuilder.Entity <User>(b =>
            {
                b.ToTableWithSharding(accessor.ShardingManager);

                b.HasKey(k => k.Id);

                b.HasIndex(i => i.Name);

                b.Property(p => p.Id).ValueGeneratedNever();

                b.Property(p => p.Name).HasMaxLength(50);
                b.Property(p => p.Passwd).HasMaxLength(50);
                b.Property(p => p.CreatedTime).HasMaxLength(50)
                .Sharding <DateTimeOffsetShardingStrategy>(accessor.ShardingManager);
            });

            return(modelBuilder);
        }
Пример #10
0
 public static void GetObjectContext(object o, out ObjectContext objectContext, out Func <int> saveChangesMethod)
 {
     objectContext     = o as ObjectContext;
     saveChangesMethod = null;
     if (objectContext == null)
     {
         Func <int>        func     = null;
         DbContextAccessor accessor = GetDbContextAccessor(o.GetType());
         if (accessor != null)
         {
             objectContext = accessor.GetContext(o);
             if (func == null)
             {
                 func = () => accessor.SaveChanges(o);
             }
             saveChangesMethod = func;
         }
     }
     else
     {
         saveChangesMethod = new Func <int>(objectContext.SaveChanges);
     }
 }
Пример #11
0
 private static DbContextAccessor CreateDbContextAccessor(Type type)
 {
     DbContextAccessor accessor = null;
     if (IsTypeOf(type, "System.Data.Entity.DbContext"))
     {
         Type type2 = type.GetInterface("System.Data.Entity.Infrastructure.IObjectContextAdapter", false);
         if (type2 != null)
         {
             PropertyInfo property = type2.GetProperty("ObjectContext", BindingFlags.Public | BindingFlags.Instance);
             if ((property != null) && (property.GetGetMethod() != null))
             {
                 MethodInfo method = type.GetMethod("SaveChanges", BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null);
                 if (method != null)
                 {
                     accessor = new DbContextAccessor();
                     ParameterExpression expression = Expression.Parameter(typeof(object));
                     accessor.GetContext = Expression.Lambda<Func<object, ObjectContext>>(Expression.Property(Expression.Convert(expression, type2), property.GetGetMethod()), new ParameterExpression[] { expression }).Compile();
                     accessor.SaveChanges = Expression.Lambda<Func<object, int>>(Expression.Call(Expression.Convert(expression, type), method), new ParameterExpression[] { expression }).Compile();
                 }
             }
         }
     }
     return accessor;
 }
Пример #12
0
 public TeamStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #13
0
 public RejudgingStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #14
0
 public ContestStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #15
0
 public StudentStore(DbContextAccessor context) => Context = context;
Пример #16
0
 public PrintingStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #17
0
 public BalloonStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #18
0
        /// <summary>
        /// Creates a DbContextAccessor for a given type if it exists
        /// </summary>
        /// <param name="type">The type to create the accessor for</param>
        /// <returns>A DbContextAccessor, or null if one cannot be created for the Type (i.e. it does not inherit from DbContext)</returns>
        private static DbContextAccessor CreateDbContextAccessor(Type type)
        {
            const BindingFlags DbContextBindingFlags = BindingFlags.Public | BindingFlags.Instance;
            const string DbContextTypeName = "System.Data.Entity.DbContext";
            const string IObjectContextAdapterTypeName = "System.Data.Entity.Infrastructure.IObjectContextAdapter";
           
            DbContextAccessor accessor = null;
            bool derivesFromDbContext = IsTypeOf(type, DbContextTypeName);

            if (derivesFromDbContext)
            {
                Type contextAdapterInterface = type.GetInterface(IObjectContextAdapterTypeName, false);
                if (contextAdapterInterface != null)
                {
                    PropertyInfo contextProperty = contextAdapterInterface.GetProperty("ObjectContext", DbContextBindingFlags);
                    if (contextProperty != null && contextProperty.GetGetMethod() != null)
                    {
                        MethodInfo saveChanges = type.GetMethod("SaveChanges", DbContextBindingFlags, null, Type.EmptyTypes, null);
                        if (saveChanges != null)
                        {
                            accessor = new DbContextAccessor();

                            // Generate the ObjectContext getter
                            ParameterExpression contextParameter = Expression.Parameter(typeof(object));
                            
                            accessor.GetContext = Expression.Lambda<Func<object, ObjectContext>>(
                                Expression.Property(
                                    Expression.Convert(contextParameter, contextAdapterInterface),
                                    contextProperty.GetGetMethod()),
                                contextParameter).Compile();

                            // Generate the SaveChanges method
                            accessor.SaveChanges = Expression.Lambda<Func<object, int>>(
                                Expression.Call(
                                    Expression.Convert(contextParameter, type),
                                    saveChanges),
                                contextParameter).Compile();
                        }
                    }
                }
            }

            return accessor;
        }
Пример #19
0
 public ClarificationStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #20
0
 public CategoryStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #21
0
 public TestcaseStore(DbContextAccessor context, IProblemFileRepository fs)
 {
     Context = context;
     Files   = fs;
 }
Пример #22
0
 public RefreshScoreboardCacheHandler(DbContextAccessor db)
 {
     Context = db;
 }
Пример #23
0
 public ContestEventNotifier(DbContextAccessor context)
 {
     Context = context;
 }
Пример #24
0
 public ConfigurationRegistry(DbContextAccessor context)
 {
     Context = context;
 }
Пример #25
0
 public JudgehostStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #26
0
 public SubmissionStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #27
0
 public AffiliationStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #28
0
 public LanguageStore(DbContextAccessor context)
 {
     Context = context;
 }
Пример #29
0
 public TrainingStore(DbContextAccessor context) => Context = context;
Пример #30
0
 public JudgingStore(DbContextAccessor context, Features.Storage.IRunFileRepository runs)
 {
     Context = context;
     Files   = runs;
 }
Пример #31
0
 public ProblemsetStore(DbContextAccessor context, IProblemStore problem)
 {
     Context = context;
     Parent  = problem;
 }
Пример #32
0
 public ArchiveStore(DbContextAccessor context)
 {
     Context = context;
 }