예제 #1
0
        public static DomainProperty Register(string name,
                                              Type propertyType,
                                              Type ownerType,
                                              Func <DomainObject, DomainProperty, object> getDefaultValue,
                                              PropertyChangedMode changedMode,
                                              Type dynamicType = null)
        {
            lock (_properties)
            {
                var target = _properties.GetValue(ownerType, (p) =>
                {
                    return(p.Name.EqualsIgnoreCase(name));
                });
                if (target != null)
                {
                    throw new DomainDrivenException(string.Format(Strings.RepeatedDeclareProperty, ownerType.FullName, name));
                }

                var validators    = PropertyValidatorAttribute.GetValidators(ownerType, name);
                var repositoryTip = GetAttribute <PropertyRepositoryAttribute>(ownerType, name);

                var property = new DomainProperty()
                {
                    Id              = Guid.NewGuid(),
                    Name            = name,
                    PropertyType    = propertyType,
                    OwnerType       = ownerType,
                    GetDefaultValue = getDefaultValue,
                    ChangedMode     = changedMode,
                    Validators      = validators,
                    RepositoryTip   = repositoryTip,
                    PropertyInfo    = ownerType.ResolveProperty(name),
                    DynamicType     = dynamicType
                };

                if (repositoryTip != null)
                {
                    repositoryTip.Property = property; //赋值
                }
                {
                    //获取属性值的行为链
                    var chain = new PropertyGetChain(property);
                    chain.AddMethods(PropertyGetAttribute.GetMethods(ownerType, name));
                    property.GetChain = chain;
                }

                {
                    //设置属性值的行为链
                    var chain = new PropertySetChain(property);
                    chain.AddMethods(PropertySetAttribute.GetMethods(ownerType, name));
                    property.SetChain = chain;
                }


                {
                    //更改属性值的行为链
                    var chain = new PropertyChangedChain(property);
                    chain.AddMethods(PropertyChangedAttribute.GetMethods(ownerType, name));
                    property.ChangedChain = chain;
                }

                InitAccessLevel(property);

                _properties.Add(ownerType, property);
                return(property);
            }
        }