コード例 #1
0
ファイル: DbSetManager.cs プロジェクト: librame/extensions
        public virtual bool TryRemoveIds <TId>(IEnumerable <TId> ids,
                                               Action <IEnumerable <TId> > postAction = null)
            where TId : IEquatable <TId>
        {
            ids.NotNull(nameof(ids));

            if (!EntityType.IsImplementedInterfaceType <IIdentifier <TId> >())
            {
                throw new InvalidOperationException($"Unsupported remove entity type '{EntityType}'. The entity type needs to implement the identifier interface '{typeof(IIdentifier<TId>)}'.");
            }

            foreach (var id in ids)
            {
                var entity = ObjectExtensions.EnsureCreate <TEntity>();

                if (entity is IIdentifier <TId> identifier)
                {
                    identifier.Id = id;
                    DbSet.Remove(entity);
                }
            }

            postAction?.Invoke(ids);
            return(true);
        }
コード例 #2
0
        public void PropertyValuesEqualsTest()
        {
            var source  = new TestClass();
            var compare = ObjectExtensions.EnsureCreate <TestClass>();

            Assert.True(source.PropertyValuesEquals(compare));

            var equals = source.YieldEnumerable()
                         .SequencePropertyValuesEquals(compare.YieldEnumerable());

            Assert.True(equals);
        }
コード例 #3
0
        protected virtual void AddMigration
            (DataDbContextAccessor <TAudit, TAuditProperty, TMigration, TTabulation, TTenant, TGenId, TIncremId, TCreatedBy> dbContextAccessor)
        {
            if (!dbContextAccessor.IsWritingConnectionString())
            {
                return;
            }

            (var body, var hash) = CreateModelSnapshot(dbContextAccessor, out var typeName);

            dbContextAccessor.MigrationsManager.TryAdd(p => p.ModelHash == hash,
                                                       () =>
            {
                var identifierGenerator = (IDataStoreIdentificationGenerator <TGenId>)dbContextAccessor
                                          .GetService <IStoreIdentificationGenerator>();

                var migration = ObjectExtensions.EnsureCreate <TMigration>();

                migration.Id = identifierGenerator.GenerateMigrationId();

                migration.PopulateCreation(identifierGenerator.Clock);

                migration.AccessorName      = dbContextAccessor.CurrentType.GetDisplayNameWithNamespace();
                migration.ModelSnapshotName = typeName;
                migration.ModelBody         = body;
                migration.ModelHash         = hash;

                return(migration);
            },
                                                       addedPost =>
            {
                if (!dbContextAccessor.RequiredSaveChanges)
                {
                    dbContextAccessor.RequiredSaveChanges = true;
                }

                // 移除当前缓存
                var cacheKey = DbContextAccessorHelper.GetMigrationCacheKey(dbContextAccessor);
                MemoryCache.Remove(cacheKey);

                // 发送迁移通知
                var mediator = dbContextAccessor.GetService <IMediator>();
                mediator.Publish(new MigrationNotification <TMigration>
                {
                    Migration = addedPost.Entity
                })
                .ConfigureAwaitCompleted();
            });
        }
コード例 #4
0
        protected virtual TTabulation CreateTabulation(IEntityType entityType, string tableName, string schema)
        {
            var entity = ObjectExtensions.EnsureCreate <TTabulation>();

            entity.Id = DataGenerator.GenerateTabulationId();

            entity.TableName = tableName;
            entity.Schema    = schema;

            entity.EntityName   = GetEntityName(entityType);
            entity.AssemblyName = GetEntityAssemblyName(entityType);
            entity.Description  = GetEntityDescription(entityType);
            entity.IsSharding   = IsEntitySharding(entityType);

            entity.PopulateCreation(Clock);

            return(entity);
        }
コード例 #5
0
        public void EnsureCreateTest()
        {
            var test = (TestClass)typeof(TestClass).EnsureCreateObject();

            Assert.NotNull(test);

            var testSub = ObjectExtensions.EnsureCreate <TestSubClass>();

            Assert.NotNull(testSub);

            Assert.Equal(test.Property3, testSub.Property3);

            // Change Property
            testSub.Property3 = nameof(EnsureCreateTest);

            var testSubReference = testSub.EnsureConstruct <TestSubClassReference>();

            Assert.NotNull(testSubReference);

            Assert.Equal(testSubReference.TestSub.Property3, testSub.Property3);
        }
コード例 #6
0
 /// <summary>
 /// 创建审计属性。
 /// </summary>
 /// <returns>返回 <typeparamref name="TAuditProperty"/>。</returns>
 protected virtual TAuditProperty CreateAuditProperty()
 => ObjectExtensions.EnsureCreate <TAuditProperty>();
コード例 #7
0
 /// <summary>
 /// 创建审计。
 /// </summary>
 /// <returns>返回 <typeparamref name="TAudit"/>。</returns>
 protected virtual TAudit CreateAudit()
 => ObjectExtensions.EnsureCreate <TAudit>();