mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-19 18:22:11 +08:00
统一注入代码
This commit is contained in:
80
OpenAuth.App/AutofacExt.cs
Normal file
80
OpenAuth.App/AutofacExt.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
// ***********************************************************************
|
||||
// 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>
|
||||
// <summary>IOC扩展</summary>
|
||||
// ***********************************************************************
|
||||
|
||||
using System.Reflection;
|
||||
using System.Web.Http;
|
||||
using System.Web.Mvc;
|
||||
using Autofac;
|
||||
using Autofac.Integration.Mvc;
|
||||
using Autofac.Integration.WebApi;
|
||||
using OpenAuth.Repository;
|
||||
using OpenAuth.Repository.Interface;
|
||||
using IContainer = Autofac.IContainer;
|
||||
|
||||
namespace OpenAuth.App
|
||||
{
|
||||
public static class AutofacExt
|
||||
{
|
||||
private static IContainer _container;
|
||||
|
||||
public static void InitAutofac()
|
||||
{
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
//注册数据库基础操作和工作单元
|
||||
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>)).PropertiesAutowired();
|
||||
builder.RegisterType(typeof(UnitWork)).As(typeof(IUnitWork)).PropertiesAutowired();
|
||||
|
||||
//注册app层
|
||||
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();
|
||||
|
||||
// 注册controller,使用属性注入
|
||||
builder.RegisterControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();
|
||||
|
||||
//注册所有的ApiControllers
|
||||
builder.RegisterApiControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();
|
||||
|
||||
builder.RegisterModelBinders(Assembly.GetCallingAssembly());
|
||||
builder.RegisterModelBinderProvider();
|
||||
|
||||
// OPTIONAL: Register web abstractions like HttpContextBase.
|
||||
//builder.RegisterModule<AutofacWebTypesModule>();
|
||||
|
||||
// OPTIONAL: Enable property injection in view pages.
|
||||
builder.RegisterSource(new ViewRegistrationSource());
|
||||
|
||||
// 注册所有的Attribute
|
||||
builder.RegisterFilterProvider();
|
||||
|
||||
// Set the dependency resolver to be Autofac.
|
||||
_container = builder.Build();
|
||||
|
||||
//Set the MVC DependencyResolver
|
||||
DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
|
||||
|
||||
//Set the WebApi DependencyResolver
|
||||
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)_container);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从容器中获取对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static T GetFromFac<T>()
|
||||
{
|
||||
return _container.Resolve<T>();
|
||||
// return (T)DependencyResolver.Current.GetService(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user