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

Fluent+AutoFAC模式在ASP.NET MVC中的应用场景:

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

 

通过添加引用autofac,完成autofac的基本注册,等于完成autofac的asp.net MVC集成。Web.config中添加:

    <httpModules>
      <add name="ContainerDisposal" type="Autofac.Integration.Web.ContainerDisposalModule,Autofac.Integration.Web"/>
      <add name="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中register Person。

                             

Validation部分:

先添加引用:

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

 

 

 

           ModelValidatorProviders.Providers.Add(
                new FluentValidationModelValidatorProvider(new NopValidatorFactory()));
 
通过在Model类对象上使用Validator Attribute实现自动的验证
using FluentValidation.Attributes;
[Validator(typeof(NewsItemValidator))]
    public class NewsItemModel : BaseNopEntityModel
    {
        [NopResourceDisplayName("Admin.ContentManagement.News.NewsItems.Fields.Language")]
        public int LanguageId { get; set; }

 

在Admin的Model中以及front Web的Model中

 [Validator(typeof(NewsItemValidator))]
    public class NewsItemModel : BaseNopEntityModel
    {
        public NewsItemModel()
        {
            Tags = new List<string>();
            Comments = new List<NewsCommentModel>();
            AddNewComment = new AddNewsCommentModel();
        }

。。 。。。 。。。

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

http://code.google.com/p/autofac/wiki/AspNetIntegration

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值