I am new to structure map. When I use property Injection for a Custom validation class the property is always null. I think i am missing some thing. Any help in this regards will be highly appreciated. I don't want to use System.Web.Mvc.DependencyResolver.Current.GetService(); to get the instance of logger in the AddressInfoValidator. we are using MVC 5, FluentValidation.Mvc(5.1.0.0), and Structure map(2.6.3.0) for injecting dependencies. I have a simple loging facade and a Myloger class:
public class LogingFacade : ILogingFacade
{
private IMyLoger loger;
LogingFacade(IMyLoger myLoger)
{
this.loger = myLoger;
}
public void SaveToLog()
{
this.loger.SendError();
}
}
public class MyLoger : IMyLoger
{
public void SendError()
{
//send to DB
}
}
The validator implimentation is as follows, I have used property injection because the class below will used as an Attribute:
public class AddressInfoValidator : AbstractValidator<AddressInfo>, IValidatorInterceptor
{
public AddressInfoValidator()
{
this.RuleFor(x => x.StreetAddress).NotEmpty().Length(1, 50);
this.RuleFor(x => x.UnitNumber).Length(0, 20);
this.RuleFor(x => x.City).NotEmpty().WithMessage(UIErrorMessages.MSG_UI_00001).Length(1, 50).WithMessage(UIErrorMessages.MSG_UI_00002);
this.RuleFor(x => x.State).NotEmpty();
this.RuleFor(x => x.ZipCode).NotEmpty().Length(1, 20).Matches("\\d{5}");
}
[SetterProperty]
public ILogingFacade Logger { get; set;}
public ValidationResult AfterMvcValidation(ControllerContext controllerContext, ValidationContext validationContext, ValidationResult result)
{
try
{
result.Errors.ToList().ForEach(r =>
{
this.Logger.SaveToLog(new ValidationError { PropertyName = r.PropertyName, AttemptedValue = r.AttemptedValue, ErrorMessage = r.ErrorMessage });
});
}
catch (System.AggregateException)
{
// TODO log the exception
}
return result;
}
public ValidationContext BeforeMvcValidation(ControllerContext controllerContext, ValidationContext validationContext)
{
return validationContext;
}
}
The address model is :
[Validator(typeof(AddressInfoValidator))]
public class AddressInfo
{
public string StreetAddress{ get; set;}
public string UnitNumber{ get; set;}
public string ZipCode{ get; set;}
public string City
public string State{ get; set;}
}
Structure map registry called from application on start in global.asax:
[CLSCompliant(false)]
public class ServiceUiWebRegistry : Registry
{
public ServiceUiWebRegistry()
{
this.For<IMyLoger>().Use<IMyLoger>();
this.For<ILogingFacade>().Use<LogingFacade>();
this.FillAllPropertiesOfType<ILogingFacade>();
}
}