-
-
Save jmarnold/1113223 to your computer and use it in GitHub Desktop.
ValidationBehavior<T>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IModelTypeCoordinator | |
{ | |
IValidationModel FindGetFor(Type postType); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IValidationModel | |
{ | |
ValidationResult ValidationResult { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ModelTypeCoordinator : IModelTypeCoordinator | |
{ | |
private readonly IChainResolver _resolver; | |
public ModelTypeCoordinator(IChainResolver resolver) | |
{ | |
_resolver = resolver; | |
} | |
public IValidationModel FindGetFor(Type postType) | |
{ | |
var postChain = chain.FindUniqueByInputType(typeof(T)); | |
var getChain = chain.Find(achain.FirstCall().HandlerType, achain.FirstCall().HandlerType.GetMethod("Get")); | |
return (IValidationModel) Activator.CreateInstance(getChain.InputType()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidationBehavior<T> : BasicBehavior where T : class | |
{ | |
IFubuRequest request; | |
IPartialFactory _factory; | |
IModelTypeCoordinator _coordinator; | |
public ValidationBehavior(IFubuRequest request, IPartialFactory factory, IModelTypeCoordinator coordinator) | |
: base(PartialBehavior.Executes) | |
{ | |
this.request = request; | |
this._factory = factory; | |
this._coordinator = coordinator; | |
} | |
protected override DoNext performInvoke() | |
{ | |
var model = request.Get<T>(); | |
// validate and return DoNext.Continue if successful | |
// e.g., var result = _validationProvider.Validate(model); | |
var target = _coordinator.FindGetFor(model.GetType()); | |
// set ValidationResult property (e.g., target.ValidationResult = result;) | |
_factory | |
.BuildPartial(targetType.GetType()) | |
.InvokePartial(); | |
return DoNext.Stop; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment