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
|
|
|
|
// ***********************************************************************
|
|
|
|
|
|
|
|
|
|
using Autofac;
|
2015-11-13 23:25:46 +08:00
|
|
|
|
using Autofac.Configuration;
|
2015-10-26 21:58:12 +08:00
|
|
|
|
using Autofac.Integration.Mvc;
|
|
|
|
|
using OpenAuth.App;
|
2015-11-19 21:49:39 +08:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Web.Mvc;
|
2015-12-02 16:28:01 +08:00
|
|
|
|
using OpenAuth.Domain.Interface;
|
2016-04-21 10:54:05 +08:00
|
|
|
|
using OpenAuth.Domain.Service;
|
2015-12-02 16:28:01 +08:00
|
|
|
|
using OpenAuth.Repository;
|
2015-10-26 21:58:12 +08:00
|
|
|
|
|
|
|
|
|
namespace OpenAuth.Mvc
|
|
|
|
|
{
|
2015-11-30 17:44:42 +08:00
|
|
|
|
internal static class AutofacExt
|
2015-10-26 21:58:12 +08:00
|
|
|
|
{
|
|
|
|
|
public static void InitAutofac()
|
|
|
|
|
{
|
|
|
|
|
var builder = new ContainerBuilder();
|
|
|
|
|
|
2016-05-27 00:58:36 +08:00
|
|
|
|
//注册数据库基础操作和工作单元
|
2015-12-02 16:28:01 +08:00
|
|
|
|
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>));
|
2016-04-29 16:26:09 +08:00
|
|
|
|
builder.RegisterType(typeof (UnitWork)).As(typeof (IUnitWork));
|
2015-12-02 16:28:01 +08:00
|
|
|
|
|
2016-05-27 00:58:36 +08:00
|
|
|
|
//注册WebConfig中的配置
|
2015-11-19 21:49:39 +08:00
|
|
|
|
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
|
2016-04-29 16:26:09 +08:00
|
|
|
|
|
|
|
|
|
//注册app层
|
2016-07-19 11:44:48 +08:00
|
|
|
|
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof (UserManagerApp)));
|
2016-04-29 16:26:09 +08:00
|
|
|
|
|
|
|
|
|
//注册领域服务
|
|
|
|
|
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(AuthoriseService)))
|
|
|
|
|
.Where(u =>u.Namespace== "OpenAuth.Domain.Service");
|
2015-12-02 16:28:01 +08:00
|
|
|
|
|
2016-04-17 23:16:58 +08:00
|
|
|
|
// Register your MVC controllers.
|
2015-11-13 21:33:53 +08:00
|
|
|
|
builder.RegisterControllers(typeof(MvcApplication).Assembly);
|
2015-10-26 21:58:12 +08:00
|
|
|
|
|
|
|
|
|
// OPTIONAL: Register model binders that require DI.
|
|
|
|
|
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
|
|
|
|
|
builder.RegisterModelBinderProvider();
|
|
|
|
|
|
|
|
|
|
// OPTIONAL: Register web abstractions like HttpContextBase.
|
|
|
|
|
builder.RegisterModule<AutofacWebTypesModule>();
|
|
|
|
|
|
|
|
|
|
// OPTIONAL: Enable property injection in view pages.
|
|
|
|
|
builder.RegisterSource(new ViewRegistrationSource());
|
|
|
|
|
|
|
|
|
|
// OPTIONAL: Enable property injection into action filters.
|
|
|
|
|
builder.RegisterFilterProvider();
|
|
|
|
|
|
|
|
|
|
// Set the dependency resolver to be Autofac.
|
|
|
|
|
var container = builder.Build();
|
|
|
|
|
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
|
|
|
|
|
}
|
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>()
|
|
|
|
|
{
|
|
|
|
|
return (T)DependencyResolver.Current.GetService(typeof(T));
|
|
|
|
|
}
|
2015-10-26 21:58:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|