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中添加:
例如在DependencyRegistrar.cs中
在Global.asax中registerPerson。
Validation部分:
先添加引用:
由于使用了Ioc,我们看到在Controller中也可以使用带有参数的方式了:
在Admin的Model中以及frontWeb的Model中
。。。。。。。。
更多详细资料,请参考http://code.google.com/p/autofac/
http://code.google.com/p/autofac/wiki/AspNetIntegration
话说应该先介绍一些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();
}
。。。。。。。。
更多详细资料,请参考
相关文章推荐
- Nop-Fluent+AutoFAC模式在ASP.NET MVC中的应用场景<四>
- Nop-ASP.NET MVC Area概念的应用Admin后台项目<三>
- Nop-ASP.NET MVC Area概念的应用Admin后台项目<三>
- Asp.Net MVC<四>:路由器
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- ASP.NET MVC 3: Razor’s @: and <text> syntax
- ASP.NET MVC (Razor)开发 <<周报与绩效考核系统>> 总结与分享
- ASP.NET MVC 3: Razor’s @: and <text> syntax
- Note on <Pro ASP.NET MVC 4> - 01
- asp.net mvc <tr>变&lt;tr/&gt; @Html.Raw 作用
- <转>ASP.NET MVC HtmlHelper用法大全
- New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)
- 如何实现IEnumerable<T>和IEnumerator接口,一个ASP.NET MVC日志模型的实现
- <ASP.NET4 从入门到精通>学习笔记3
- .net 3.5 sp1 的新加程序集System.Web.Abstractions中的装饰者模式,及其在Asp.net Mvc中的应用
- ASP.NET MVC中Autofac实现的自动注入模式
- asp.net MVC&JQuery 应用(树型表格 )
- System.Web.Abstractions中的装饰者模式,及其在Asp.net Mvc中的应用
- ASP.NET 中的 ViewState <转>
- <asp.net> 实现返回功能