工作,我已經修改了原來的問題,因爲同樣的錯誤發生的區別在於執行,我現在已經添加Ninject的組合。DataAnnotations和FluentValidation不MVC 2項目
我創建了一個類
public class AlbumValidator : AbstractValidator<Album> {
public AlbumValidator() {
RuleFor(a => a.Title).NotEmpty();
}
}
我創建了一個ValidatorModule爲Ninject
internal class FluentValidatorModule : NinjectModule {
public override void Load() {
AssemblyScanner.FindValidatorsInAssemblyContaining<AlbumValidator>()
.ForEach(result => Bind(result.InterfaceType).To(result.ValidatorType).InSingletonScope());
}
}
這裏的驗證規則是我ValidatorFactory
public class NinjectValidatorFactory : ValidatorFactoryBase {
public override IValidator CreateInstance(Type validatorType) {
if (validatorType.GetGenericArguments()[0].Namespace.Contains("DynamicProxies")) {
validatorType = Type.GetType(string.Format("{0}.{1}[[{2}]], {3}",
validatorType.Namespace,
validatorType.Name,
validatorType.GetGenericArguments()[0].BaseType.AssemblyQualifiedName,
validatorType.Assembly.FullName));
}
return Container.Get(validatorType) as IValidator;
}
IKernel Container { get; set; }
public NinjectValidatorFactory(IKernel container) {
Container = container;
}
}
,並從相關部分我的全球
protected override void OnApplicationStarted() {
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
var factory = new NinjectValidatorFactory(Container);
ModelValidatorProviders.Providers.Add(
new FluentValidationModelValidatorProvider(factory));
DataAnnotationsModelValidatorProvider
.AddImplicitRequiredAttributeForValueTypes = false;
}
protected override IKernel CreateKernel() {
return Container;
}
IKernel Container {
get { return new StandardKernel(new FluentValidatorModule()); }
}
我加載創建新的相冊鏈接的樣本網站點擊,然後點擊創建按鈕離開標題空白,我再與()OnApplicationStarted錯誤保護覆蓋無效{ AreaRegistration.RegisterAllAreas()映入眼簾; RegisterRoutes(RouteTable.Routes);
var factory = new NinjectValidatorFactory(Container);
ModelValidatorProviders.Providers.Add(
new FluentValidationModelValidatorProvider(factory));
DataAnnotationsModelValidatorProvider
.AddImplicitRequiredAttributeForValueTypes = false;
}
protected override IKernel CreateKernel() {
return Container;
}
IKernel Container {
get { return new StandardKernel(
new Bootstrapper(),
new FluentValidatorModule()); }
}
我加載了創建表格,然後點擊創建離開標題空低,看哪一個錯誤
This property cannot be set to a null value.
它引用該生產線是實體框架自動生成的類中,我跟蹤
Namespace.Contains("DynamicProxies")
它返回false,這是因爲我告訴EF使用自定義命名空間SampleMusicStore.Web?
還是我缺少別的東西?
乾杯。
當你說它不工作,你的意思是驗證沒有發生,或者整個行動不起作用? – codeulike 2010-10-04 12:17:52