AutoFac+MVC+WebApi源码----我踩过的坑

2021-02-10 15:15

阅读:285

发现网上关于AutoFac的Demo源码比较少,综合MVC和WepApi的更少。所以贴出源码

WebApi项目(MVC4不需要引用,历史遗留问题,人懒没删)

技术分享图片

技术分享图片

 

建项目

新建类库IAutoFacDal(接口),AutoFacDal,IAutoFacLogic(接口),AutoFacLogic,AutoFacDto(参数),AutoFacEntity(数据库实体)

技术分享图片

 

新建WebApi

技术分享图片

AutoFacEntity

技术分享图片

User源码

技术分享图片技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace AutoFacEntity.DBModel
 8 {
 9     public class User
10     {
11         /// 
12         /// 姓名
13         /// 
14         public string Name { get; set; }
15         /// 
16         /// 年龄
17         /// 
18         public int Age { get; set; }
19         /// 
20         /// 性别
21         /// 
22         public string Sex { get; set; }
23     }
24 }
User

 

AutoFacDto

技术分享图片

UserAddInputDto源码

技术分享图片技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace AutoFacDto.Input
 8 {
 9     public class UserAddInputDto
10     {
11         /// 
12         /// 姓名
13         /// 
14         public string Name { get; set; }
15         /// 
16         /// 年龄
17         /// 
18         public int Age { get; set; }
19         /// 
20         /// 性别
21         /// 
22         public string Sex { get; set; }
23     }
24 }
UserAddInputDto

UserModel源码

技术分享图片技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace AutoFacDto.Model
 8 {
 9     public class UserModel
10     {
11         /// 
12         /// 姓名
13         /// 
14         public string Name { get; set; }
15         /// 
16         /// 年龄
17         /// 
18         public int Age { get; set; }
19         /// 
20         /// 性别
21         /// 
22         public string Sex { get; set; }
23     }
24 }
UserModel

UserAddOutputDto源码

技术分享图片技术分享图片
 1 using AutoFacDto.Model;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace AutoFacDto.Output
 9 {
10     public class UserAddOutputDto
11     {
12         public UserModel Info { get; set; }
13     }
14 }
UserAddOutputDto

 

IAutoFacDal 

IUserDal

技术分享图片

IUserDal源码

技术分享图片技术分享图片
 1  1 using AutoFacDto.Input;
 2  2 using AutoFacDto.Model;
 3  3 using AutoFacDto.Output;
 4  4 using System;
 5  5 using System.Collections.Generic;
 6  6 using System.Linq;
 7  7 using System.Text;
 8  8 using System.Threading.Tasks;
 9  9 
10 10 namespace IAutoFacDal.User
11 11 {
12 12     public interface IUserDal
13 13     {
14 14         UserAddOutputDto UserAdd(UserAddInputDto input);
15 15     }
16 16 }
View Code

AutoFacDal

技术分享图片

UserDal源码(另一个类无视)

技术分享图片技术分享图片
 1 using IAutoFacDal.User;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using AutoFacDto.Input;
 8 using AutoFacDto.Output;
 9 using AutoFacDto.Model;
10 
11 namespace AutoFacDal.User
12 {
13     public class UserDal : IUserDal
14     {
15         public UserAddOutputDto UserAdd(UserAddInputDto input)
16         {
17             UserAddOutputDto output = new UserAddOutputDto { Info=new UserModel()};
18             output.Info.Age = input.Age;
19             output.Info.Name = input.Name+"--默认调用";
20             output.Info.Sex = input.Sex;
21             return output;
22         }
23     }
24 }
UserDal

IAutoFacLogic

技术分享图片

IUserLogic源码

技术分享图片技术分享图片
using AutoFacDto.Input;
using AutoFacDto.Output;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IAutoFacLogic.User
{
    public interface IUserLogic
    {
        UserAddOutputDto UserAdd(UserAddInputDto input);
    }
}
IUserLogic

 

 

 AutoFacLogic

技术分享图片

 

UserLogic源码(UserInfoLogic是我测试替换数据源的,可以无视)

 1 using IAutoFacLogic.User;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using AutoFacDto.Input;
 8 using AutoFacDto.Output;
 9 using IAutoFacDal.User;
10 
11 namespace AutoFacLogic.User
12 {
13     public class UserLogic : IUserLogic
14     {
15         public IUserDal iServerUerDal;
16 
17         public UserLogic(IUserDal param)
18         {
19             iServerUerDal = param;
20         }
21         public UserAddOutputDto UserAdd(UserAddInputDto input)
22         {
23             return iServerUerDal.UserAdd(input);
24         }
25     }
26 }

 AutoFacWebApi

WebApi项目默认是有代码并且能实现的

我只给出关键代码,如果需要Demo源码联系QQ85528445备注:博客园Autofac(Home里面有报错的话,把报错的删掉就能运行了)

技术分享图片技术分享图片
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using AutoFacDal.User;
using AutoFacLogic.User;
using IAutoFacDal.User;
using IAutoFacLogic.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace AutoFacWebApi
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);

            //注册全局过滤器
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);            
            AutoFacStart();
        }
        #region 实现依赖注入
        /// 
        /// 使用AutoFac实现依赖注入,
        /// 
        public void AutoFacStart()
        {
            var builder = new ContainerBuilder();
            SetupResolveRules(builder);  //注入

            builder.RegisterControllers(Assembly.GetExecutingAssembly());  //注入Controller

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());//注入APi
            builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);//注入APi
            builder.RegisterWebApiModelBinderProvider();//注入APi

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));//注入Controller

            var webApiResolver = new AutofacWebApiDependencyResolver(container);    //注入APi
            GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;  //注入APi
        }
        /// 
        /// 注入规则
        /// 
        /// 
        protected void SetupResolveRules(ContainerBuilder builder)
        {
            //UI项目只用引用service和repository的接口,不用引用实现的dll。
            //如需加载实现的程序集,将dll拷贝到bin目录下即可,不用引用dll
            var IServicesBLL = Assembly.Load("IAutoFacLogic");
            var ServicesBLL = Assembly.Load("AutoFacLogic");

            var IServicesDAL = Assembly.Load("IAutoFacDal");
            var ServicesDAL = Assembly.Load("AutoFacDal");

            //实现服务接口和服务实现的依赖 业务逻辑层
            builder.RegisterAssemblyTypes(IServicesBLL, ServicesBLL).AsImplementedInterfaces();

            //实现服务接口和服务实现的依赖 数据层
            builder.RegisterAssemblyTypes(IServicesDAL, ServicesDAL).AsImplementedInterfaces();
            //特定条件的过滤注册-只注册结尾是User的
            //builder.RegisterAssemblyTypes(IServices, Services).Where(t => t.Name.EndsWith("User")).AsImplementedInterfaces();
            //使用新的覆盖旧的注册DLL-使用UserNew代替UserOld
            builder.RegisterType().As();
            builder.RegisterType().As();
            builder.RegisterType().As();
        }
        #endregion
    }
}
Global
技术分享图片技术分享图片
using AutoFacDto.Input;
using IAutoFacLogic.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AutoFacWebApi.Controllers
{
    public class HomeController : Controller
    {
        private IUserLogic iServerUserLogic;
        private IUserInfoLogic iServerUserInfoLogic;

        /// 
        /// 配置构造函数注入
        /// 
        public HomeController(IUserLogic param,IUserInfoLogic param2)
        {
            iServerUserLogic = param;
            iServerUserInfoLogic = param2;
        }
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";
         
            UserAddInputDto input = new UserAddInputDto();
            input.Name = "Home Page";
            //动态调用Bll项目中的类方法
            //ViewBag.Title = iServerUserLogic.UserAdd(input).Info.Name;
            ViewBag.Title = iServerUserInfoLogic.UserAdd(input).Info.Name;
            return View();
        }
    }
}
HomeController

 


评论


亲,登录后才可以留言!