OpenAuth.Net/OpenAuth.App/AutofacExt.cs

80 lines
3.0 KiB
C#
Raw Normal View History

// ***********************************************************************
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();
//注册数据库基础操作和工作单元
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
//注册app层
2017-12-08 05:49:00 +08:00
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();
2015-10-26 21:58:12 +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.
//builder.RegisterModule<AutofacWebTypesModule>();
2015-10-26 21:58:12 +08:00
// OPTIONAL: Enable property injection in view pages.
builder.RegisterSource(new ViewRegistrationSource());
// 注册所有的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
}
/// <summary>
/// 从容器中获取对象
/// </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-10-26 21:58:12 +08:00
}
}