/// <summary> /// 将一个新的实体对象创建到库。 /// </summary> /// <param name="entity">要创建的实体对象。</param> public void Create(IEntity entity) { ValidationUnity.Validate(entity); EntityPersistentSubscribePublisher.OnBeforeCreate(entity); try { Batch(() => { var isSucc = false; entity.TryLockModifing(() => { isSucc = InternalCreate(entity); UpdateRelations(entity); }); if (isSucc) { EntityPersistentSubscribePublisher.OnAfterCreate(entity); entity.SetState(EntityState.Unchanged); } }); } catch (DbException exp) { throw new EntityPersistentException(SR.GetString(SRKind.FailInEntityCreate), exp); } }
/// <summary> /// 更新实体对象的修改。 /// </summary> /// <param name="entity">要更新的实体对象。</param> /// <exception cref="ArgumentNullException"><paramref name="entity"/> 参数为 null。</exception> public virtual void Update(TEntity entity) { Guard.ArgumentNull(entity, "entity"); ValidationUnity.Validate(entity); new QuerySet <TEntity>(Provider).UpdateEntity(entity); }
public void ValidateWithCustomAttributeTest2() { var p = new Products { ProductID = 1, ProductName = "1111111d11111111", Discontinued = true }; ValidationUnity.Validate(p); }
public void ValidateWithCustomAttributeTest() { ValidationUnity.RegisterValidation(typeof(Products), new ProductQuantityValidateAttribute()); var p = new Products { ProductID = 1, ProductName = "111111111111111", Discontinued = true }; ValidationUnity.Validate(p); }
private TEntity HandleValidate(TEntity entity) { if (options.ValidateEntity) { ValidationUnity.Validate(entity); } return(entity); }
/// <summary> /// 处理实体验证。 /// </summary> /// <param name="entity"></param> /// <returns></returns> protected virtual TEntity HandleValidate(TEntity entity) { if (_options.ValidateEntity) { ValidationUnity.Validate(entity); } return(entity); }
/// <summary> /// 将一个实体插入到参照实体的相应位置。 /// </summary> /// <param name="entity">插入的实体。</param> /// <param name="referEntity">参照的实体。</param> /// <param name="position">插入的位置。</param> /// <param name="isolation">数据隔离表达式。</param> public virtual void Insert(TEntity entity, TEntity referEntity, EntityTreePosition position = EntityTreePosition.Children, Expression <Func <TEntity> > isolation = null) { Guard.ArgumentNull(entity, nameof(entity)); if (referEntity == null) { var arg = CreateUpdatingArgument(entity); //获得新节点的Order值 arg.NewValue.Order = GetNewOrderNumber(null, EntityTreePosition.Children, 0, isolation); arg.NewValue.Level = 1; //生成新的InnerID arg.NewValue.FullName = arg.OldValue.Name; arg.NewValue.InnerId = GenerateInnerId(string.Empty, arg.NewValue.Order, EntityTreePosition.Children); UpdateEntityByArgument(entity, arg); repository.Insert(entity); return; } var arg1 = CreateUpdatingArgument(entity); var arg2 = CreateUpdatingArgument(referEntity); var keyId = arg2.OldValue.InnerId; //获得新节点的Order值 arg1.NewValue.Order = GetNewOrderNumber(arg2.OldValue, position); //获得参照节点的级别 arg1.NewValue.Level = arg2.OldValue.Level; //如果插入为孩子,级别则+1 if (position == EntityTreePosition.Children) { arg1.NewValue.Level += 1; } //生成新的InnerID arg1.NewValue.InnerId = GenerateInnerId(keyId, arg1.NewValue.Order, position); arg1.NewValue.FullName = GenerateFullName(arg1, arg2, position); UpdateEntityByArgument(entity, arg1); ValidationUnity.Validate(entity); try { repository.Insert(entity); //repository.Batch(brothers, (u, s) => u.Update(s)); } catch (Exception ex) { throw new EntityPersistentException(SR.GetString(SRKind.FailInEntityInsert), ex); } }
/// <summary> /// 将一个新的实体对象创建到库。 /// </summary> /// <param name="entity">要创建的实体对象。</param> public virtual void Create(object entity) { var e = entity as IEntity; var database = Database; ValidationUnity.Validate(e); var proxy = new EntityPersisterHelper(database, GetEntityType(), Environment); proxy.Create(e); }
public void ValidateWithCustomAttributeTest1() { var p = PropertyUnity.GetProperty(typeof(Orders), "CustomerID"); ValidationUnity.RegisterValidation(p, new AnyValidateAttribute()); var o = new Orders { CustomerID = "AA" }; ValidationUnity.Validate(o); }
public void TestBuild() { #if !NETCOREAPP2_0 var assBuilder = new DynamicAssemblyBuilder("test_dll", "e:\\test.dll"); var typeBuilder = new EntityTypeBuilder("TestEntity", assBuilder); #else var typeBuilder = new EntityTypeBuilder("testclass"); #endif var pName = new GeneralProperty() { Name = "Name", Info = new PropertyMapInfo { FieldName = "name" }, Type = typeof(string) }; typeBuilder.Properties.Add(pName); typeBuilder.Properties.Add(new GeneralProperty() { Name = "Age", Info = new PropertyMapInfo { FieldName = "age" }, Type = typeof(int?) }); typeBuilder.Properties.Add(new GeneralProperty() { Name = "Sex", Info = new PropertyMapInfo { FieldName = "sex" }, Type = typeof(Sex) }); typeBuilder.DefineValidateRule(pName, () => new System.ComponentModel.DataAnnotations.MaxLengthAttribute(15)); var type = typeBuilder.Create(); #if !NETCOREAPP2_0 assBuilder.Save(); #endif var e = type.New <IEntity>(); e.SetValue("Name", "fireasy"); e.SetValue("Age", 12); e.SetValue("Sex", Sex.M); Assert.AreEqual(e.GetValue("Name"), "fireasy"); Assert.AreEqual(e.GetValue("Age"), 12); Assert.AreEqual(e.GetValue("Sex"), Sex.M); ValidationUnity.Validate(e); var property = PropertyUnity.GetProperty(type, "Name"); Assert.IsNotNull(property); Assert.AreEqual(type.GetProperty("Name").GetValue(e), "fireasy"); }
/// <summary> /// 更新一个实体对象。 /// </summary> /// <param name="entity">实体对象。</param> /// <returns>影响的实体数。</returns> public int Update(TEntity entity) { Guard.ArgumentNull(entity, nameof(entity)); if (context.Options.ValidateEntity) { ValidationUnity.Validate(entity); } int func() => repositoryProxy.Update(entity); return(context.Options.NotifyEvents ? EntityPersistentSubscribeManager.OnUpdate(entity, func) : func()); }
/// <summary> /// 将一个新的实体对象创建到库。 /// </summary> /// <param name="entity">要创建的实体对象。</param> /// <returns>影响的实体数。</returns> public int Insert(TEntity entity) { Guard.ArgumentNull(entity, nameof(entity)); if (options.ValidateEntity) { ValidationUnity.Validate(entity); } int func() => repositoryProxy.Insert(entity); return(options.NotifyEvents ? EntityPersistentSubscribeManager.OnCreate(entity, func) : func()); }
public int Insert(TEntity entity) { EntityPersistentSubscribePublisher.OnBeforeCreate(entity); ValidationUnity.Validate(entity); var trans = CheckRelationHasModified(entity); if (trans) { context.Database.BeginTransaction(); } int result = 0; try { if ((result = Queryable.CreateEntity(entity)) > 0) { entity.As <IEntityPersistentEnvironment>(s => s.Environment = context.Environment); entity.As <IEntityPersistentInstanceContainer>(s => s.InstanceName = context.InstanceName); HandleRelationProperties(entity); EntityPersistentSubscribePublisher.OnAfterCreate(entity); } if (trans) { context.Database.CommitTransaction(); } } catch (Exception exp) { if (trans) { context.Database.RollbackTransaction(); } throw exp; } return(result); }
public int Update(TEntity entity) { EntityPersistentSubscribePublisher.OnBeforeUpdate(entity); ValidationUnity.Validate(entity); var trans = CheckRelationHasModified(entity); if (trans) { context.Database.BeginTransaction(); } int result = 0; try { if ((result = Queryable.UpdateEntity(entity)) > 0) { EntityPersistentSubscribePublisher.OnAfterUpdate(entity); } HandleRelationProperties(entity); if (trans) { context.Database.CommitTransaction(); } } catch (Exception exp) { if (trans) { context.Database.RollbackTransaction(); } throw exp; } return(result); }
/// <summary> /// 更新实体对象的修改。 /// </summary> /// <param name="entity">要更新的实体对象。</param> public void Update(IEntity entity) { ValidationUnity.Validate(entity); EntityPersistentSubscribePublisher.OnBeforeUpdate(entity); try { Batch(() => { var parameters = new ParameterCollection(); var context = CreateContext(parameters); var sql = EntityPersistentQueryBuilder.BuildUpdateQuery(context, entity); var isSucc = false; entity.TryLockModifing(() => { if (!sql.IsNullOrEmpty()) { isSucc = database.ExecuteNonQuery(sql, parameters) > 0; } UpdateRelations(entity); }); if (isSucc) { EntityPersistentSubscribePublisher.OnAfterUpdate(entity); entity.SetState(EntityState.Unchanged); } }); } catch (Exception exp) { throw new EntityPersistentException(SR.GetString(SRKind.FailInEntityUpdate), exp); } }
/// <summary> /// 更新一个实体对象。 /// </summary> /// <param name="entity">要更新的实体对象。</param> /// <returns>影响的实体数。</returns> public int Update(TEntity entity) { ValidationUnity.Validate(entity); var trans = CheckRelationHasModified(entity); if (trans) { context.Database.BeginTransaction(); } int result = 0; try { result = Queryable.UpdateEntity(entity); HandleRelationProperties(entity); if (trans) { context.Database.CommitTransaction(); } } catch (Exception exp) { if (trans) { context.Database.RollbackTransaction(); } throw exp; } return(result); }
public void ValidateTest() { var p = new Products(); ValidationUnity.Validate(p); }