Exemplo n.º 1
0
        public static CreateResponse SetPropertiesAndValidate <TEventAttribute>(SapphireDbContext db,
                                                                                KeyValuePair <Type, string> property, object newValue,
                                                                                HttpInformation context, IServiceProvider serviceProvider)
            where TEventAttribute : ModelStoreEventAttributeBase
        {
            object newEntityObject = property.Key.SetFields(newValue);

            if (!ValidationHelper.ValidateModel(newEntityObject, serviceProvider,
                                                out Dictionary <string, List <string> > validationResults))
            {
                return(new CreateResponse()
                {
                    Value = newEntityObject,
                    ValidationResults = validationResults
                });
            }

            property.Key.ExecuteHookMethods <TEventAttribute>(ModelStoreEventAttributeBase.EventType.Before,
                                                              newEntityObject, context, serviceProvider);

            db.Add(newEntityObject);

            property.Key.ExecuteHookMethods <TEventAttribute>(ModelStoreEventAttributeBase.EventType.BeforeSave,
                                                              newEntityObject, context, serviceProvider);

            return(new CreateResponse()
            {
                Value = newEntityObject,
            });
        }
Exemplo n.º 2
0
        private ResponseBase SetPropertiesAndValidate(SapphireDbContext db, KeyValuePair <Type, string> property, object newValue, HttpInformation context,
                                                      CreateCommand command)
        {
            object newEntityObject = property.Key.SetFields(newValue, db);

            if (!ValidationHelper.ValidateModel(newEntityObject, serviceProvider, out Dictionary <string, List <string> > validationResults))
            {
                return(new CreateResponse()
                {
                    NewObject = newEntityObject,
                    ReferenceId = command.ReferenceId,
                    ValidationResults = validationResults
                });
            }

            property.Key.ExecuteHookMethods <CreateEventAttribute>(v => v.Before, newEntityObject, context, serviceProvider);

            db.Add(newEntityObject);

            property.Key.ExecuteHookMethods <CreateEventAttribute>(v => v.BeforeSave, newEntityObject, context, serviceProvider);

            db.SaveChanges();

            property.Key.ExecuteHookMethods <CreateEventAttribute>(v => v.After, newEntityObject, context, serviceProvider);

            return(new CreateResponse()
            {
                NewObject = newEntityObject,
                ReferenceId = command.ReferenceId
            });
        }