/// <summary> /// SetValue的最后一步 /// </summary> /// <param name="property"></param> /// <param name="value"></param> internal void SetValueLastStep(DomainProperty property, object value) { var oldValue = this.DataProxy.Load(property); bool isChanged = false; if (property.ChangedMode == PropertyChangedMode.Compare) { if (this.SetPropertyChanged(property, oldValue, value)) { isChanged = true; } } else if (property.ChangedMode == PropertyChangedMode.Definite) { this.SetPropertyChanged(property); isChanged = true; } if (isChanged) { var collection = value as IDomainCollection; if (collection != null) { collection.Parent = this; } this.DataProxy.Save(property, value); HandlePropertyChanged(property, value, oldValue); } }
private void RaisePropertyChanged <T>(DomainProperty property, T newValue, T oldValue) { if (IsConstructing) { return; //构造时,不触发任何事件 } if (!property.IsRegisteredChanged && this.PropertyChanged == null) { return; } var ctx = GetRunContext(property.RuntimeChangedId); if (!ctx.InCallBack) { var args = new DomainPropertyChangedEventArgs(property, newValue, oldValue); //先进行对象内部回调方法 ctx.InCallBack = true; property.ChangedChain.Invoke(this, args); args.NewValue = this.GetValue(property);//同步数据 ctx.InCallBack = false; //最后执行对象级别挂载的事件 if (this.PropertyChanged != null) { ctx.InCallBack = true; this.PropertyChanged(this, args); args.NewValue = this.GetValue(property);//同步数据 ctx.InCallBack = false; } } }
private DomainProperty GetValueProperty(RuntimeType objectType, ValueEntry entry) { var propertyName = entry.Name.FirstToUpper(); var propertyType = GetValueType(entry); var propertyInfo = objectType.AddProperty(propertyName, propertyType); propertyInfo.AddAttribute(new PropertyRepositoryAttribute()); object defaultValue = DataUtil.GetDefaultValue(propertyType); //字符串类型处理 if (entry.TypeName == "string" || entry.TypeName == "ascii") { if (entry.Descriptions.Count > 0) { if (int.TryParse(entry.Descriptions[0], out int max)) { propertyInfo.AddAttribute(new StringLengthAttribute(0, max)); } if (entry.TypeName == "ascii") { propertyInfo.AddAttribute(new ASCIIStringAttribute()); } } defaultValue = string.Empty; } return(DomainProperty.Register(propertyName, propertyType, objectType, (o, p) => { return defaultValue; }, PropertyChangedMode.Compare, propertyType)); }
private static void InitAccessLevel(DomainProperty property) { if (property.IsExtensions) { var method = ExtendedClassAttribute.GetPropertyMethod(property.OwnerType, property.Name); property.AccessLevelGet = GetAccessLevel(method.Get); property.AccessLevelSet = GetAccessLevel(method.Set); } else { var pi = property.PropertyInfo; if (property.IsDynamic) { //动态属性的访问是公开的 property.AccessLevelGet = PropertyAccessLevel.Public; property.AccessLevelSet = PropertyAccessLevel.Public; } else { property.AccessLevelGet = GetAccessLevel(pi.GetMethod); property.AccessLevelSet = GetAccessLevel(pi.SetMethod); } } }
/// <summary> /// 在有效的属性对象上执行方法,只有被加载了的对象才执行 /// </summary> /// <param name="action"></param> private void InvokeProperties(Action <DomainObject> action) { var properties = DomainProperty.GetProperties(this.ObjectType); foreach (var property in properties) { switch (property.DomainPropertyType) { case DomainPropertyType.EntityObject: case DomainPropertyType.ValueObject: { DomainObject obj = null; if (TryGetValue <DomainObject>(property, ref obj)) { action(obj); } } break; case DomainPropertyType.EntityObjectList: case DomainPropertyType.ValueObjectList: { IEnumerable list = null; if (TryGetValue <IEnumerable>(property, ref list)) { foreach (DomainObject obj in list) { action(obj); } } } break; } } }
protected override void Validate(DomainObject domainObject, DomainProperty property, IList propertyValue, ValidationResult result) { var list = propertyValue as IList; if (list != null) //为null的情况不检查,交由别的对象检查 { int count = list.Count; if (count < this.Min) { result.AddError(property.Name, ListCountError, string.Format(Strings.ListCountLessThan, property.Name, this.Min)); } else if (count > this.Max) { result.AddError(property.Name, ListCountError, string.Format(Strings.ListCountMoreThan, property.Name, this.Max)); } if (this.ValidateItem) { foreach (var item in list) { ISupportFixedRules support = item as ISupportFixedRules; if (support != null) { ValidationResult t = support.Validate(); if (!t.IsSatisfied) { result.AddError(property.Name, ListItemError, string.Format(Strings.ListItemError, property.Name, t.Message)); } } } } } }
/// <summary> /// 要通过数据判断值对象相等 /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { var target = obj as ValueObject; if (target == null) { return(false); } var targetType = target.ObjectType; if (targetType != ObjectType) { return(false); } //对比所有领域属性 var properties = DomainProperty.GetProperties(ObjectType); foreach (var property in properties) { if (!EqualsHelper.ObjectEquals(this.GetValue(property), target.GetValue(property))) { return(false); } } return(true); }
/// <summary> /// 实现动态对象属性值设置的方法。 /// </summary> /// <param name="binder"></param> /// <param name="value"></param> /// <returns></returns> public override bool TrySetMember(System.Dynamic.SetMemberBinder binder, object value) { var propertyName = binder.Name; var property = DomainProperty.GetProperty(this.ObjectType, propertyName); SetValue(property, value); return(true); }
/// <summary> /// 实现动态对象属性成员访问的方法,得到返回指定属性的值 /// </summary> /// <param name="binder"></param> /// <param name="result"></param> /// <returns></returns> public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result) { var propertyName = binder.Name; var property = DomainProperty.GetProperty(this.ObjectType, propertyName); result = GetValue(property); return(true); }
private object GetPrimitiveValue(DomainProperty property, object value) { if (property.DomainPropertyType == DomainPropertyType.Primitive) { return(DataUtil.ToValue(value, property.PropertyType)); } throw new DomainDrivenException(string.Format(Strings.DynamicObjectLoadError, this.Define.TypeName)); }
private PropertyRepositoryAttribute GetPropertyTip() { var objectType = _constructorTip.Constructor.ReflectedType; var propertyName = this.Original.Name; var property = DomainProperty.GetProperty(objectType, propertyName); return(property.RepositoryTip); }
/// <summary> /// 当属性的值已经被加载,就获取数据,否则不获取 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="property"></param> /// <returns></returns> private bool TryGetValue <T>(DomainProperty property, ref T value) where T : class { if (this.DataProxy.IsLoaded(property)) { value = GetValue <T>(property); return(true); } return(false); }
private object FindObject(RemotableAttribute tip, DTObject arg) { var idProperty = DomainProperty.GetProperty(tip.ObjectType, EntityObject.IdPropertyName); var id = DataUtil.ToValue(arg.GetValue("id"), idProperty.PropertyType); var repository = Repository.Create(tip.ObjectType); return(repository.Find(id, QueryLevel.None)); }
protected override void Validate(DomainObject domainObject, DomainProperty property, string propertyValue, ValidationResult result) { if (propertyValue != null) { if (!propertyValue.IsASCII()) { result.AddError(property.Name, "NotASCII", string.Format(Strings.NotASCII, propertyValue)); } } }
/// <summary> /// 该方法虽然是公开的,但是<paramref name="property"/>都是由类内部或者扩展类定义的, /// 所以获取值和设置值只能通过常规的属性操作,无法通过该方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="property"></param> /// <returns></returns> public T GetValue <T>(DomainProperty property) { var value = GetValue(property); if (value == null) { throw new IsNullException(property.Name); } return((T)value); }
/// <summary> /// 外界可以手工指定数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <param name="value"></param> public void Save(DomainProperty property, object value) { if (property.IsQuoteAggreateRoot()) { SaveToAppSession(property, value); } else { SaveToShare(property, value); } }
protected override void Validate(DomainObject domainObject, DomainProperty property, DomainObject propertyValue, ValidationResult result) { if (propertyValue != null) { ValidationResult t = propertyValue.Validate(); if (!t.IsSatisfied) { result.AddError(ErrorCode, string.Format(Strings.DoNotMeetSpecifications, property.Name)); } } }
protected override void Validate(DomainObject domainObject, DomainProperty property, long propertyValue, ValidationResult result) { if (propertyValue < this.Min) { result.AddError(property.Name, ErrorCode, string.Format(Strings.ValueLessThan, property.Name, this.Min)); } else if (propertyValue > this.Max) { result.AddError(property.Name, ErrorCode, string.Format(Strings.ValueMoreThan, property.Name, this.Max)); } }
protected override void Validate(DomainObject domainObject, DomainProperty property, string propertyValue, ValidationResult result) { if (string.IsNullOrEmpty(propertyValue)) { return; } if (!string.IsNullOrEmpty(_pattern)) { if (!_regex.IsMatch(propertyValue)) { result.AddError(property.Name, StringFormatError, string.Format(Extensions.Strings.DoesNotMeetRule, property.Name, _pattern)); } } else { var value = propertyValue; if (_contains.Length > 0) { foreach (string temp in _contains) { value = value.Replace(temp, string.Empty); } } RegexPool reg = null; string message = null; if (IsChineseAndLetterAndNumber(_formatValue)) { reg = _chineseAndLetterAndNumberRegex; message = Extensions.Strings.CEN; } else if (IsLetterAndNumber(_formatValue)) { reg = _letterAndNumberRegex; message = Extensions.Strings.EN; } else if (IsChineseAndLetter(_formatValue)) { reg = _chineseAndLetterRegex; message = Extensions.Strings.CE; } else if (IsChineseAndNumber(_formatValue)) { reg = _chineseAndNumberRegex; message = Extensions.Strings.CN; } if (reg != null && !reg.IsMatch(value)) { result.AddError(property.Name, StringFormatError, string.Format(Extensions.Strings.CanOnlyInclude, property.Name, message)); } } }
protected override void Validate(DomainObject domainObject, DomainProperty property, string propertyValue, ValidationResult result) { if (string.IsNullOrEmpty(propertyValue)) { return; //是否能为空的验证由别的验证器处理 } if (!IsMatch(propertyValue)) { result.AddError(property.Name, "MobileNumber", string.Format(Strings.IncorrectMobileNumberFormat, property.Name)); } }
/// <summary> /// 根据对比结果,设置属性是否被更改 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="oldValue"></param> /// <param name="newValue"></param> /// <param name="propertyName"></param> /// <returns></returns> private bool SetPropertyChanged <T>(DomainProperty property, T oldValue, T newValue) { //要用Equals判断 if (object.Equals(oldValue, newValue)) { return(false); } _machine.SetPropertyChanged(property.Name); DuplicateMachineSetPropertyChanged(property.Name); return(true); }
/// <summary> /// 根据属性标记的验证器特性,得到验证器 /// </summary> /// <returns></returns> internal static IEnumerable <IPropertyValidator> GetValidators(Type objType, string propertyName) { var attributes = DomainProperty.GetAttributes <PropertyValidatorAttribute>(objType, propertyName); List <IPropertyValidator> validators = new List <IPropertyValidator>(attributes.Count()); foreach (var attr in attributes) { validators.Add(attr.CreateValidator()); } return(validators); }
internal static IEnumerable <Func <DomainObject, object> > GetMethods(Type objType, string propertyName) { var attributes = DomainProperty.GetAttributes <PropertyGetAttribute>(objType, propertyName); List <Func <DomainObject, object> > methods = new List <Func <DomainObject, object> >(attributes.Count()); foreach (var attribute in attributes) { methods.Add(attribute.GetAction()); } return(methods); }
/// <summary> /// 外界标记某个属性发生了变化,这常常是由于属性本身的成员发生了变化,而触发的属性变化 /// 该方法会触发属性被改变的事件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="property"></param> public void MarkPropertyChanged(DomainProperty property) { if (!this.IsPropertyLoaded(property)) { return; } this.SetPropertyChanged(property); var value = this.GetValue(property); HandlePropertyChanged(property, value, value); }
protected void UseDefines(DTObject arg, Action <AggregateRootDefine, object> action) { var typeName = arg.GetValue <string>("typeName"); var defines = RemoteType.GetDefines(typeName); foreach (var define in defines) { var idProperty = DomainProperty.GetProperty(define.MetadataType, EntityObject.IdPropertyName); var id = DataUtil.ToValue(arg.GetValue(EntityObject.IdPropertyName), idProperty.PropertyType); action((AggregateRootDefine)define, id); } }
private DomainProperty GetValueProperty(RuntimeType objectType, ValueEntry entry) { var propertyName = entry.Name.FirstToUpper(); var propertyType = GetValueType(entry); var propertyInfo = objectType.AddProperty(propertyName, propertyType); propertyInfo.AddAttribute(new PropertyRepositoryAttribute()); object defaultValue = entry.IsString ? string.Empty : DataUtil.GetDefaultValue(propertyType); AttachAttributes(propertyInfo, entry); return(DomainProperty.Register(propertyName, propertyType, objectType, (o, p) => { return defaultValue; }, PropertyChangedMode.Compare, propertyType)); }
private DomainProperty GetObjectProperty(RuntimeType objectType, ObjectEntry entry) { string propertyName = entry.Name.FirstToUpper(); var define = GetObjectDefine(objectType, entry); Type propertyType = define.MetadataType; var propertyInfo = objectType.AddProperty(propertyName, propertyType); propertyInfo.AddAttribute(new PropertyRepositoryAttribute()); return(DomainProperty.Register(propertyName, propertyType, objectType, (o, p) => { return define.EmptyInstance; }, PropertyChangedMode.Definite, propertyType)); }
/// <summary> /// 是否有脏的属性 /// </summary> /// <returns></returns> private bool HasDirtyProperty() { var properties = DomainProperty.GetProperties(this.ObjectType); foreach (var property in properties) { if (IsPropertyDirty(property)) { return(true); } } return(false); }
private void ValidateProperties(IDomainObject obj, ValidationResult result) { var properties = DomainProperty.GetProperties(obj.GetType()); foreach (var property in properties) { if (obj.IsPropertyDirty(property)) { //我们只用验证脏属性 ValidateProperty(obj, property, result); } } }
/// <summary> /// 属性是否为脏的 /// </summary> /// <param name="property"></param> /// <returns></returns> public bool IsPropertyDirty(DomainProperty property) { if (this.IsNew) { return(true); //如果是新建对象,那么属性一定是脏的 } var isChanged = IsPropertyChanged(property); if (isChanged) { return(true); //属性如果被改变,那么我们认为就是脏的,这是一种粗略的判断,因为就算属性改变了,有可能经过2次修改后变成与数据库一样的值,这样就不是脏的了,但是为了简单化处理,我们仅认为只要改变了就是脏的,这种判断不过可以满足100%应用 } //如果属性没有被改变,那么我们需要进一步判断属性的成员是否发生改变 switch (property.DomainPropertyType) { case DomainPropertyType.ValueObject: case DomainPropertyType.EntityObject: case DomainPropertyType.EntityObjectPro: { DomainObject obj = null; if (TryGetValue <DomainObject>(property, ref obj)) { //如果加载了,就进一步判断 return(obj.IsDirty); } } break; case DomainPropertyType.ValueObjectList: case DomainPropertyType.EntityObjectList: case DomainPropertyType.EntityObjectProList: { IEnumerable list = null; if (TryGetValue <IEnumerable>(property, ref list)) { //如果加载了,就进一步判断 foreach (DomainObject obj in list) { if (obj.IsDirty) { return(true); } } } } break; } //AggregateRoot和AggregateRootList 只用根据IsPropertyChanged判断即可,因为他们是外部内聚根对象,是否变脏与本地内聚根没有关系 return(false); }