/// <summary> /// Checks the validation of a single property /// </summary> /// <param name="item">The item.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="errors">The errors.</param> /// <param name="context">The context.</param> /// <returns></returns> public bool TryValidate(IRoleDataModel item, string propertyName, List <IModelError> errors, IModelContext context = null) { var rtn = TryValidateModel(item, Operation.View, errors, context); errors.RemoveAll(x => string.Compare(x.Property, propertyName, true) != 0); return(rtn); }
/// <summary> /// Create a new item with the properties (state) of an existing IRoleDataModel /// </summary> internal RoleEntityDataModel(IRoleDataModel model) { // copy state over Name = model.Name; Key = model.Key; IsAssignable = model.IsAssignable; }
/// <summary> /// Validates the model. /// </summary> /// <param name="item">The item.</param> /// <param name="operation">The operation.</param> /// <param name="errors">The errors.</param> /// <param name="context">The context.</param> /// <returns></returns> private bool TryValidateModel(IRoleDataModel item, Operation operation, List <IModelError> errors, IModelContext context = null) { if (item == null) { AddNullModelError(errors); return(false); } return(validator.IsValid(item, operation, errors)); }
/// <summary> /// Create a new item with the properties (state) of an existing IRoleDataModel /// </summary> public void Load(IRoleDataModel model) { if (model == null) { return; } // copy state from incomming model Id = model.Id; Name = model.Name; Key = model.Key; IsAssignable = model.IsAssignable; }
/// <summary> /// Represents a single Role instance as a select list item /// </summary> public static SelectListItem ToSelectListItem(this IRoleDataModel model, int id = -1) { if (model == null) { return(new SelectListItem()); } else { return(new SelectListItem { Text = string.Format("[{0}] {1}", model.Id, model.IsAssignable.ToString()), // change how the Role appear in drop downs here Value = model.Id.ToString(), Selected = (id == model.Id) }); } }
/// <summary> /// Tries the state of the item. /// </summary> /// <param name="model">The model.</param> /// <param name="trigger">The trigger.</param> /// <param name="errors">The errors.</param> /// <param name="context">The context.</param> /// <returns></returns> protected bool TryUpdateItemState(IRoleDataModel model, RoleTriggers trigger, List <IModelError> errors, IModelContext context = null) { var state = ExtractState(model); var stateMachine = GetStateMachine(model); if (stateMachine.CanFire(trigger)) { stateMachine.Fire(trigger); SetModelState(stateMachine.State, model); return(service.TrySave(model, errors, context)); } else { MakeErrorMessage(stateMachine, trigger, errors); return(false); } }
/// <summary> /// Tries to create a new item /// </summary> /// <param name="item">The item.</param> /// <param name="errors">The errors.</param> /// <param name="context">The context.</param> /// <returns></returns> /// <exception cref="InvalidOperationException">Unable to update IRoleDataModel"</exception> private bool TryCreate(IRoleDataModel item, List <IModelError> errors, IModelContext context = null) { if (TryValidateModel(item, Operation.Create, errors, context) == false) { return(false); } try { changeHandler.BeforeCreate(item, context); dal.Create(item, context); changeHandler.AfterCreate(item, context); } catch (Exception ex) { log.Exception(LogName, ex); throw new InvalidOperationException("Unable to create IRoleDataModel", ex); } return(true); }
/// <summary> /// Returns true if the model is valid /// </summary> public bool IsValid(IRoleDataModel model, Operation op, List <IModelError> errors) { var e = new List <IModelError>(); // check for required properties if (model.Name.IsPresent() == false) { e.Add(new ModelError { Property = "Name", ErrorMessage = "role_name_missing" }); } if (model.Key.IsPresent() == false) { e.Add(new ModelError { Property = "Key", ErrorMessage = "role_key_missing" }); } // check supplied properties are valid errors.CombineOrReplace(e); return(e.Any() == false); }
protected override void SetModelState(RoleStates state, IRoleDataModel model) { // todo : update model properties to reflect the state throw new NotImplementedException(); }
// Add new methods to represent other states // protected override RoleStates ExtractState(IRoleDataModel model) { // todo : extract a RoleStates to repersent the current model throw new NotImplementedException(); }
/// <summary> /// Tries the set the model to "complete" state. /// </summary> public bool TrySetComplete(IRoleDataModel model, List <IModelError> errors, IModelContext context = null) { return(TryUpdateItemState(model, RoleTriggers.Complete, errors, context)); }
/// <summary> /// Called when [update]. /// </summary> /// <param name="item">The item.</param> /// <param name="context">The context.</param> public virtual void AfterUpdate(IRoleDataModel item, IModelContext context) { }
/// <summary> /// Called when [customer update]. /// </summary> /// <param name="item">The item.</param> /// <param name="context">The context.</param> public virtual void BeforeUpdate(IRoleDataModel item, IModelContext context) { }
/// <summary> /// Called when [create]. /// </summary> /// <param name="item">The item.</param> /// <param name="context">The context.</param> public virtual void BeforeCreate(IRoleDataModel item, IModelContext context = null) { }
/// <summary> /// Extracts the state. /// </summary> abstract protected RoleStates ExtractState(IRoleDataModel model);
/// <summary> /// Represents a collection Role instance as select list items /// </summary> public static IEnumerable <SelectListItem> ToSelectListItem(this IEnumerable <IRoleDataModel> models, IRoleDataModel selected = null) { if (models == null) { return(Enumerable.Empty <SelectListItem>()); } else { var rtn = new List <SelectListItem>(); rtn.Add(new SelectListItem { Text = "Select .....", Value = "-1" }); rtn.AddRange(models.Select(x => x.ToSelectListItem(selected == null ? -1 : selected.Id)).OrderBy(x => x.Text)); return(rtn); } }
/// <summary> /// Gets the state machine. /// </summary> protected virtual StateMachine <RoleStates, RoleTriggers> GetStateMachine(IRoleDataModel model) { return(new StateMachine <RoleStates, RoleTriggers>(ExtractState(model))); }
/// <summary> /// Tries to save the item. /// </summary> /// <param name="item"></param> /// <param name="errors"></param> /// <param name="context"></param> /// <returns></returns> public bool TrySave(IRoleDataModel item, List <IModelError> errors, IModelContext context = null) { return(item.IsNew ? TryCreate(item, errors, context) : TryUpdate(item, errors, context)); }
/// <summary> /// Sets the state of the model. /// </summary> abstract protected void SetModelState(RoleStates state, IRoleDataModel model);