您的位置:首页 > 编程语言 > ASP

Nop-Fluent+AutoFAC模式在ASP.NET MVC中的应用场景<四>

2012-02-23 15:14 357 查看
Fluent+AutoFAC模式在ASP.NETMVC中的应用场景:

话说应该先介绍一些IoC或者DependencyInjection之类的东西,但我确实是才学这些不多久,虽然接触ASP.NET许久。

通过添加引用autofac,完成autofac的基本注册,等于完成autofac的asp.netMVC集成。Web.config中添加:
<httpModules>
<addname="ContainerDisposal"type="Autofac.Integration.Web.ContainerDisposalModule,Autofac.Integration.Web"/>
<addname="PropertyInjection"type="Autofac.Integration.Web.Forms.PropertyInjectionModule,Autofac.Integration.Web"/>
</httpModules>


例如在DependencyRegistrar.cs中
builder.RegisterType<GeoCountryLookup>().As<IGeoCountryLookup>().InstancePerHttpRequest();
builder.RegisterType<CountryService>().As<ICountryService>().InstancePerHttpRequest();


在Global.asax中registerPerson。

Validation部分:
先添加引用:

由于使用了Ioc,我们看到在Controller中也可以使用带有参数的方式了:

ModelValidatorProviders.Providers.Add(
newFluentValidationModelValidatorProvider(newNopValidatorFactory()));


通过在Model类对象上使用ValidatorAttribute实现自动的验证

usingFluentValidation.Attributes;

[Validator(typeof(NewsItemValidator))]
publicclassNewsItemModel:BaseNopEntityModel
{
[NopResourceDisplayName("Admin.ContentManagement.News.NewsItems.Fields.Language")]
publicintLanguageId{get;set;}


在Admin的Model中以及frontWeb的Model中

[Validator(typeof(NewsItemValidator))]
publicclassNewsItemModel:BaseNopEntityModel
{
publicNewsItemModel()
{
Tags=newList<string>();
Comments=newList<NewsCommentModel>();
AddNewComment=newAddNewsCommentModel();
}

。。。。。。。。

更多详细资料,请参考http://code.google.com/p/autofac/

http://code.google.com/p/autofac/wiki/AspNetIntegration
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: