Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation: How to inject A Model State wrapper with Ninject?
    text
    copied!<p>I was looking at this tutorial <a href="http://asp-umb.neudesic.com/mvc/tutorials/validating-with-a-service-layer--cs" rel="noreferrer">http://asp-umb.neudesic.com/mvc/tutorials/validating-with-a-service-layer--cs</a> on how to wrap my validation data around a wrapper.</p> <p>I would like to use dependency inject though. I am using ninject 2.0</p> <pre><code>namespace MvcApplication1.Models { public interface IValidationDictionary { void AddError(string key, string errorMessage); bool IsValid { get; } } } </code></pre> <p>// wrapper</p> <pre><code>using System.Web.Mvc; namespace MvcApplication1.Models { public class ModelStateWrapper : IValidationDictionary { private ModelStateDictionary _modelState; public ModelStateWrapper(ModelStateDictionary modelState) { _modelState = modelState; } #region IValidationDictionary Members public void AddError(string key, string errorMessage) { _modelState.AddModelError(key, errorMessage); } public bool IsValid { get { return _modelState.IsValid; } } #endregion } } </code></pre> <p>// controller</p> <pre><code>private IProductService _service; public ProductController() { _service = new ProductService(new ModelStateWrapper(this.ModelState), new ProductRepository()); } </code></pre> <p>// service layer</p> <pre><code>private IValidationDictionary _validatonDictionary; private IProductRepository _repository; public ProductService(IValidationDictionary validationDictionary, IProductRepository repository) { _validatonDictionary = validationDictionary; _repository = repository; } public ProductController(IProductService service) { _service = service; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload