Exemple #1
0
 public ForeignObjectPropertyMapping(ForeignObjectMappingOptions options)
     : base(options)
 {
     _options = options;
 }
        public static MappingOptionsSet AddDefaultRules(this MappingOptionsSet mappingOptionsSet)
        {
            mappingOptionsSet.AddTypeRule(x => !x.GetTypeInfo().IsAbstract, o => { throw new NotSupportedException("InheritedPropertyMapping supports abstract classes and interfaces only."); });
            mappingOptionsSet.AddTypeRule(x => x.GetCustomAttribute <TableAttribute>() != null, o => {
                TableAttribute attribute = o.Type.GetCustomAttribute <TableAttribute>();
                o.TableName    = attribute.TableName;
                o.LoadBehavior = attribute.LoadBehavior;
            });

            mappingOptionsSet.AddTypeRule(x => x.GetTypeInfo().ImplementedInterfaces.Contains(typeof(INotifyPropertyChanged)), o => {
                o.RaisePropertyChangeMethod = o.Type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(x =>
                                                                                                                                            x.GetParameters().Length == 1 &&
                                                                                                                                            x.GetParameters()[0].ParameterType == typeof(PropertyChangedEventArgs) &&
                                                                                                                                            x.GetCustomAttributes(typeof(IsRaisePropertyChangeMethodAttribute), true).Any()).FirstOrDefault();
            });

            mappingOptionsSet.AddMemberTypeRule(x => x.GetCustomAttribute <ForeignObjectMappingAttribute>() != null, x => { x.MappingType = MappingType.ForeignObjectMapping; });
            mappingOptionsSet.AddMemberTypeRule(x => x.GetCustomAttribute <ReferenceListMappingAttribute>() != null, x => { x.MappingType = MappingType.ReferenceListMapping; });

            mappingOptionsSet.AddMemberMappingRule(x => x.GetCustomAttribute <ForeignObjectMappingAttribute>() != null, o => {
                ForeignObjectMappingOptions options     = (ForeignObjectMappingOptions)o;
                ForeignObjectMappingAttribute attribute = options.Member.GetCustomAttribute <ForeignObjectMappingAttribute>();
                options.DatabaseFieldName = attribute.Fieldname;

                if (attribute.ForeignObjectType != null)
                {
                    options.ForeignObjectType = attribute.ForeignObjectType;
                }

                if (attribute.ReadOnly)
                {
                    options.IsReadonly = true;
                }
                else
                {
                    options.IsInsertable = attribute.Insertable;
                    options.IsUpdateable = attribute.Updateable;
                }
                options.IsPrimaryKey = options.Member.GetCustomAttribute <IsPrimaryKeyAttribute>() != null;
            });

            mappingOptionsSet.AddMemberMappingRule(x => x.GetCustomAttribute <ReferenceListMappingAttribute>() != null, o => {
                ReferenceListMappingOptions options     = (ReferenceListMappingOptions)o;
                ReferenceListMappingAttribute attribute = options.Member.GetCustomAttribute <ReferenceListMappingAttribute>();

                options.SaveCascade        = attribute.SaveCascade;
                options.DeleteCascade      = attribute.DeleteCascade;
                options.DropChangesCascade = attribute.DropChangesCascade;

                options.ForeignProperty = attribute.ForeignProperty ?? attribute.ForeignType.GetProperties().Where(x => x.PropertyType == options.Member.DeclaringType).Single();
                foreach (EqualsObjectConditionAttribute equalsObjectAttribute in options.Member.GetCustomAttributes <EqualsObjectConditionAttribute>())
                {
                    options.Conditions.Add(attribute.ForeignType.GetProperty(equalsObjectAttribute.PropertyName), equalsObjectAttribute.Value);
                }
            });

            mappingOptionsSet.AddMemberMappingRule(x => x.GetCustomAttribute <ForeignObjectMappingAttribute>() == null && x.GetCustomAttribute <ReferenceListMappingAttribute>() == null, o => {
                FieldMappingOptions options = (FieldMappingOptions)o;
                MappingAttribute attribute  = options.Member.GetCustomAttribute <MappingAttribute>();
                if (attribute != null)
                {
                    options.DatabaseFieldName = attribute.FieldName ?? options.DatabaseFieldName;
                    if (attribute.ReadOnly)
                    {
                        options.IsReadonly = true;
                    }
                    else
                    {
                        options.IsInsertable = attribute.Insertable;
                        options.IsUpdateable = attribute.Updateable;
                    }
                }
                options.IsPrimaryKey = options.Member.GetCustomAttribute <IsPrimaryKeyAttribute>() != null;
            });

            return(mappingOptionsSet);
        }
        public bool Commit(bool withUndo)
        {
            if (!_isUncommitted)
            {
                if (withUndo && _isChanged)
                {
                    _isChanged    = false;
                    _changedValue = default(T);
                    return(true);
                }
                return(false);
            }

            _originalValue             = _uncommittedValue;
            _originalConvertedValue    = default(T);
            _isOriginalConvertedSetted = false;

            ParameterExpression paramT = Expression.Parameter(typeof(T), "T");

            if (object.Equals(_originalValue, default(T1)))
            {
                _originalQuery = null;
            }
            else if (_changedValue is IFillAbleObject &&
                     ((IFillAbleObject)_changedValue).Keys.Count == 1 &&
                     ((IFillAbleObject)_changedValue).Keys.Single() is T1 &&
                     object.Equals((T1)((IFillAbleObject)_changedValue).Keys.Single(), _originalValue))
            {
                _originalConvertedValue    = _changedValue;
                _isOriginalConvertedSetted = true;
            }
            else
            {
                Expression propertyAccess = null;
                if (ForeignKeyMemberOptions is ForeignObjectMappingOptions)
                {
                    propertyAccess = Expression.Property(paramT, ForeignKeyMemberOptions.Member);
                    ForeignObjectMappingOptions currentOptions = (ForeignObjectMappingOptions)ForeignKeyMemberOptions;
                    while (true)
                    {
                        propertyAccess = Expression.Property(propertyAccess, currentOptions.ForeignMember.Member);
                        if (currentOptions.ForeignMember is ForeignObjectMappingOptions)
                        {
                            currentOptions = (ForeignObjectMappingOptions)currentOptions.ForeignMember;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    propertyAccess = Expression.Property(paramT, ForeignKeyMemberOptions.Member);
                }

                ((System.Collections.Specialized.INotifyCollectionChanged)(_originalQuery = ObjectStoreManager.DefaultObjectStore.GetQueryable <T>().Where(
                                                                               Expression.Lambda <Func <T, bool> >(
                                                                                   Expression.Equal(
                                                                                       propertyAccess,
                                                                                       Expression.Constant(_originalValue)), paramT)).Take(1))).CollectionChanged += CollectionChangedHandler;
            }

            _uncommittedValue = default(T1);
            _isUncommitted    = false;
            _isOriginalSetted = true;

            if (withUndo)
            {
                _isChanged    = false;
                _changedValue = default(T);
            }
            return(true);
        }