Esempio n. 1
0
        /// <summary>
        /// Executes the save command.
        /// </summary>
        /// <param name="dto">The dto to save.</param>
        protected virtual void ExecuteSaveCommand(KeyedDataTransferObject dto)
        {
            _editingPart = dto;

            if (dto != null)
            {
                if (!dto.HasErrors ||
                    (dto.HasErrors && dto.DataErrorInfoCollection.Where(p => p.DataErrorInfoType == DataErrorInfoType.PropertyLevel).Count() == 0))
                {
                    var dtoType               = dto.GetType();
                    var openSaveRequestType   = typeof(SaveDtoRequest <>);
                    var closedSaveRequestType = openSaveRequestType.MakeGenericType(dtoType);
                    var saveRequest           = Activator.CreateInstance(closedSaveRequestType, dto);
                    var requestDispatcher     = _asyncRequestDispatcherFactory.CreateAsyncRequestDispatcher();
                    requestDispatcher.Add(saveRequest as Request);

                    _editingPart.IsLoading = true;

                    requestDispatcher.ProcessRequests(RequestCompleted, HandleRequestDispatcherException);
                }
                else
                {
                    var errors = dto.DataErrorInfoCollection.Where(p => p.ErrorLevel == ErrorLevel.Error).Select(p => p.ToString()).Aggregate((i, j) => i + Environment.NewLine + j);
                    _userDialogService.ShowDialog(string.Format("Please fix the following errors before trying to save:{0}{1}", Environment.NewLine, errors), "Errors", UserDialogServiceOptions.Ok);
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Maps the violations.
 /// </summary>
 /// <param name="dto">The dto that is associated with the rule violations.</param>
 /// <param name="ruleViolations">The rule violations.</param>
 public static void MapViolations(this AbstractDataTransferObject dto, IEnumerable <RuleViolation> ruleViolations)
 {
     foreach (var ruleViolation in ruleViolations)
     {
         dto.AddDataErrorInfo(new DataErrorInfo(ruleViolation.Message, ErrorLevel.Error, ruleViolation.PropertyNames));
     }
 }
Esempio n. 3
0
        private void AddListener(AbstractDataTransferObject dto, object pDto)
        {
            var dirtyInput =
                (
                    from evt in Observable.FromEventPattern <PropertyChangedEventArgs> (dto, "PropertyChanged")
                    where evt.EventArgs.PropertyName == PropertyUtil.ExtractPropertyName <AbstractDataTransferObject, bool> (p => p.IsDirty) ||
                    evt.EventArgs.PropertyName == PropertyUtil.ExtractPropertyName <AbstractDataTransferObject, bool> (p => p.HasErrors)
                    select(evt.Sender as AbstractDataTransferObject)
                );

            var dirtySubscriber = dirtyInput.ObserveOnDispatcher().Subscribe(
                (obj) =>
            {
                HasErrors = HasErrors || obj.HasErrors;
                IsDirty   = IsDirty || obj.IsDirty || HasErrors;

                var editingDto  = obj as EditableDataTransferObject;
                var pEditingDto = pDto as EditableDataTransferObject;
                if (editingDto != null)
                {
                    editingDto.EditStatus = editingDto.Key > 0 ? EditStatus.Update : EditStatus.Create;
                    if (pEditingDto != null && pEditingDto.Key > 0)
                    {
                        pEditingDto.EditStatus = EditStatus.Update;
                    }
                }
            }
                );

            _subscriptions.Add(dirtySubscriber);
        }
Esempio n. 4
0
        /// <summary>
        /// Executes the cancel command.
        /// </summary>
        /// <param name="dto">The dto to check.</param>
        protected virtual void ExecuteCancelCommand(KeyedDataTransferObject dto)
        {
            if (dto.Key > 0)
            {
                _editingPart = dto;

                var dtoType = dto.GetType();
                var openCancelRequestType   = typeof(GetDtoRequest <>);
                var closedCancelRequestType = openCancelRequestType.MakeGenericType(dtoType);
                var cancelRequest           = Activator.CreateInstance(closedCancelRequestType, dto.Key);
                var requestDispatcher       = _asyncRequestDispatcherFactory.CreateAsyncRequestDispatcher();
                requestDispatcher.Add(cancelRequest as Request);

                _editingPart.IsLoading = true;

                requestDispatcher.ProcessRequests(RequestCompleted, HandleRequestDispatcherException);
            }
            else
            {
                ExecuteCancelCommandWhenAdding(dto);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Maps the exception to <see cref="DataErrorInfo"/>.
        /// </summary>
        /// <param name="dto">
        /// The dto to which the <see cref="DataErrorInfo"/> belongs. 
        /// </param>
        /// <param name="exception">
        /// The exception. 
        /// </param>
        public static void MapExceptionToDataErrorInfo(
            AbstractDataTransferObject dto, 
            Exception exception )
        {
            DataErrorInfo error;
            if ( exception is ArgumentException )
            {
                string propertyName = ( exception as ArgumentException ).ParamName;

                var hasProperty = !string.IsNullOrEmpty ( propertyName ) && dto.GetType ().GetProperty ( propertyName ) != null;

                error = hasProperty
                            ? new DataErrorInfo ( exception.Message, ErrorLevel.Error, propertyName )
                            : new DataErrorInfo ( exception.Message, ErrorLevel.Error );
            }
            else
            {
                error = new DataErrorInfo ( exception.Message, ErrorLevel.Error );
            }

            dto.AddDataErrorInfo ( error );
        }
Esempio n. 6
0
        /// <summary>
        /// Maps the exception to <see cref="DataErrorInfo"/>.
        /// </summary>
        /// <param name="dto">
        /// The dto to which the <see cref="DataErrorInfo"/> belongs.
        /// </param>
        /// <param name="exception">
        /// The exception.
        /// </param>
        public static void MapExceptionToDataErrorInfo(
            AbstractDataTransferObject dto,
            Exception exception)
        {
            DataErrorInfo error;

            if (exception is ArgumentException)
            {
                string propertyName = (exception as ArgumentException).ParamName;

                var hasProperty = !string.IsNullOrEmpty(propertyName) && dto.GetType().GetProperty(propertyName) != null;

                error = hasProperty
                            ? new DataErrorInfo(exception.Message, ErrorLevel.Error, propertyName)
                            : new DataErrorInfo(exception.Message, ErrorLevel.Error);
            }
            else
            {
                error = new DataErrorInfo(exception.Message, ErrorLevel.Error);
            }

            dto.AddDataErrorInfo(error);
        }