2016-01-05 17:14:10 +08:00
|
|
|
|
// ***********************************************************************
|
2015-10-26 21:58:12 +08:00
|
|
|
|
// Assembly : OpenAuth.Mvc
|
|
|
|
|
// Author : yubaolee
|
|
|
|
|
// Created : 10-26-2015
|
|
|
|
|
//
|
|
|
|
|
// Last Modified By : yubaolee
|
|
|
|
|
// Last Modified On : 10-26-2015
|
|
|
|
|
// ***********************************************************************
|
|
|
|
|
// <copyright file="AutofacExt.cs" company="www.cnblogs.com/yubaolee">
|
|
|
|
|
// Copyright (c) www.cnblogs.com/yubaolee. All rights reserved.
|
|
|
|
|
// </copyright>
|
2016-01-05 10:03:35 +08:00
|
|
|
|
// <summary>IOC扩展</summary>
|
2015-10-26 21:58:12 +08:00
|
|
|
|
// ***********************************************************************
|
|
|
|
|
|
2015-11-19 21:49:39 +08:00
|
|
|
|
using System.Reflection;
|
2017-12-08 05:49:00 +08:00
|
|
|
|
using System.Web.Http;
|
2015-11-19 21:49:39 +08:00
|
|
|
|
using System.Web.Mvc;
|
2017-12-08 05:49:00 +08:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Integration.Mvc;
|
|
|
|
|
using Autofac.Integration.WebApi;
|
2015-12-02 16:28:01 +08:00
|
|
|
|
using OpenAuth.Repository;
|
2017-11-29 20:49:14 +08:00
|
|
|
|
using OpenAuth.Repository.Interface;
|
2017-12-08 05:49:00 +08:00
|
|
|
|
using IContainer = Autofac.IContainer;
|
2015-10-26 21:58:12 +08:00
|
|
|
|
|
2017-12-08 05:49:00 +08:00
|
|
|
|
namespace OpenAuth.App
|
2015-10-26 21:58:12 +08:00
|
|
|
|
{
|
2016-09-22 15:38:43 +08:00
|
|
|
|
public static class AutofacExt
|
2015-10-26 21:58:12 +08:00
|
|
|
|
{
|
2016-09-22 15:38:43 +08:00
|
|
|
|
private static IContainer _container;
|
|
|
|
|
|
2015-10-26 21:58:12 +08:00
|
|
|
|
public static void InitAutofac()
|
|
|
|
|
{
|
|
|
|
|
var builder = new ContainerBuilder();
|
|
|
|
|
|
2016-05-27 00:58:36 +08:00
|
|
|
|
//注册数据库基础操作和工作单元
|
2017-11-29 00:48:51 +08:00
|
|
|
|
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>)).PropertiesAutowired();
|
|
|
|
|
builder.RegisterType(typeof(UnitWork)).As(typeof(IUnitWork)).PropertiesAutowired();
|
2015-12-02 16:28:01 +08:00
|
|
|
|
|
2016-04-29 16:26:09 +08:00
|
|
|
|
//注册app层
|
2017-12-08 05:49:00 +08:00
|
|
|
|
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();
|
2015-10-26 21:58:12 +08:00
|
|
|
|
|
2017-03-24 15:35:52 +08:00
|
|
|
|
// 注册controller,使用属性注入
|
2017-12-08 05:49:00 +08:00
|
|
|
|
builder.RegisterControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();
|
|
|
|
|
|
|
|
|
|
//注册所有的ApiControllers
|
|
|
|
|
builder.RegisterApiControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();
|
|
|
|
|
|
|
|
|
|
builder.RegisterModelBinders(Assembly.GetCallingAssembly());
|
2015-10-26 21:58:12 +08:00
|
|
|
|
builder.RegisterModelBinderProvider();
|
|
|
|
|
|
|
|
|
|
// OPTIONAL: Register web abstractions like HttpContextBase.
|
2017-03-24 15:35:52 +08:00
|
|
|
|
//builder.RegisterModule<AutofacWebTypesModule>();
|
2015-10-26 21:58:12 +08:00
|
|
|
|
|
|
|
|
|
// OPTIONAL: Enable property injection in view pages.
|
|
|
|
|
builder.RegisterSource(new ViewRegistrationSource());
|
|
|
|
|
|
2017-03-24 15:35:52 +08:00
|
|
|
|
// 注册所有的Attribute
|
2015-10-26 21:58:12 +08:00
|
|
|
|
builder.RegisterFilterProvider();
|
|
|
|
|
|
|
|
|
|
// Set the dependency resolver to be Autofac.
|
2016-09-22 15:38:43 +08:00
|
|
|
|
_container = builder.Build();
|
2017-12-08 05:49:00 +08:00
|
|
|
|
|
|
|
|
|
//Set the MVC DependencyResolver
|
2016-09-22 15:38:43 +08:00
|
|
|
|
DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
|
2017-12-08 05:49:00 +08:00
|
|
|
|
|
|
|
|
|
//Set the WebApi DependencyResolver
|
|
|
|
|
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)_container);
|
2015-10-26 21:58:12 +08:00
|
|
|
|
}
|
2015-12-16 22:52:23 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-01-07 11:47:43 +08:00
|
|
|
|
/// 从容器中获取对象
|
2015-12-16 22:52:23 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public static T GetFromFac<T>()
|
|
|
|
|
{
|
2016-09-22 15:38:43 +08:00
|
|
|
|
return _container.Resolve<T>();
|
|
|
|
|
// return (T)DependencyResolver.Current.GetService(typeof(T));
|
2015-12-16 22:52:23 +08:00
|
|
|
|
}
|
2015-10-26 21:58:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|