依赖注入框架Autofac的简单使用,以及在 ASP.NET MVC 中的使用
2012-04-06 17:31 音乐让我说 阅读(640) 评论(0) 编辑 收藏 举报Autofac 基础
转载自:http://www.cnblogs.com/liping13599168/archive/2011/07/16/2108209.html
Autofac是一款IOC框架,比较于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很轻量级性能上也是很高的。于是,今天抽空研究了下它。下载地址:http://code.google.com/p/autofac/downloads/list
代码:
using System;
using Autofac;
using Autofac.Configuration;
namespace DearBruce.AutofacDemo.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
#region Demo1 - 硬编码注册
//var builder = new ContainerBuilder();
//builder.RegisterType<DatabaseManager>();
//builder.RegisterType<SqlDatabase>().As<IDatabase>();
//using (var container = builder.Build())
//{
// var manager = container.Resolve<DatabaseManager>();
// manager.Search("SELECT * FORM USER");
//}
#endregion
#region Demo2 - 读取配置文件
//var builder = new ContainerBuilder();
//builder.RegisterType<DatabaseManager>();
//builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
//using (var container = builder.Build())
//{
// var manager = container.Resolve<DatabaseManager>();
// manager.Search("SELECT * FORM USER");
//}
#endregion
#region Demo2 - 通过 Register 方法进行注册
var builder = new ContainerBuilder();
//builder.RegisterType<DatabaseManager>();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
builder.Register(c => new DatabaseManager(c.Resolve<IDatabase>()));
using (var container = builder.Build())
{
var manager = container.Resolve<DatabaseManager>();
manager.Search("SELECT * FORM USER");
}
#endregion
}
}
/// <summary>
/// Database operate interface
/// </summary>
public interface IDatabase
{
string Name { get; }
void Select(string commandText);
void Insert(string commandText);
void Update(string commandText);
void Delete(string commandText);
}
public class SqlDatabase : IDatabase
{
public string Name
{
get { return "sqlserver"; }
}
public void Select(string commandText)
{
Console.WriteLine(string.Format("'{0}' is a query sql in {1}!", commandText, Name));
}
public void Insert(string commandText)
{
Console.WriteLine(string.Format("'{0}' is a insert sql in {1}!", commandText, Name));
}
public void Update(string commandText)
{
Console.WriteLine(string.Format("'{0}' is a update sql in {1}!", commandText, Name));
}
public void Delete(string commandText)
{
Console.WriteLine(string.Format("'{0}' is a delete sql in {1}!", commandText, Name));
}
}
public class OracleDatabase : IDatabase
{
public string Name
{
get { return "oracle"; }
}
public void Select(string commandText)
{
Console.WriteLine(string.Format("'{0}' is a query sql in {1}!", commandText, Name));
}
public void Insert(string commandText)
{
Console.WriteLine(string.Format("'{0}' is a insert sql in {1}!", commandText, Name));
}
public void Update(string commandText)
{
Console.WriteLine(string.Format("'{0}' is a update sql in {1}!", commandText, Name));
}
public void Delete(string commandText)
{
Console.WriteLine(string.Format("'{0}' is a delete sql in {1}!", commandText, Name));
}
}
public class DatabaseManager
{
IDatabase _database;
public DatabaseManager(IDatabase database)
{
_database = database;
}
public void Search(string commandText)
{
_database.Select(commandText);
}
public void Add(string commandText)
{
_database.Insert(commandText);
}
public void Save(string commandText)
{
_database.Update(commandText);
}
public void Remove(string commandText)
{
_database.Delete(commandText);
}
}
}
Demo 下载: https://files.cnblogs.com/Music/AutofacDemo.rar
Autofac 在 ASP.NET MVC 下的注册
请参考下面的代码,来自于 https://github.com/MarlabsInc/SocialGoal/
using System.Web.Mvc; using Autofac; using Autofac.Integration.Mvc; using System.Reflection; using SocialGoal.Data.Repository; using SocialGoal.Data.Infrastructure; using SocialGoal.Service; using SocialGoal.Mappings; using SocialGoal.Web.Core.Authentication; using Microsoft.AspNet.Identity.EntityFramework; using SocialGoal.Model.Models; using SocialGoal.Data.Models; using Microsoft.AspNet.Identity; namespace SocialGoal { public static class Bootstrapper { public static void Run() { SetAutofacContainer(); //Configure AutoMapper AutoMapperConfiguration.Configure(); } private static void SetAutofacContainer() { var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest(); builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest(); builder.RegisterAssemblyTypes(typeof(FocusRepository).Assembly) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces().InstancePerHttpRequest(); builder.RegisterAssemblyTypes(typeof(GoalService).Assembly) .Where(t => t.Name.EndsWith("Service")) .AsImplementedInterfaces().InstancePerHttpRequest(); builder.RegisterAssemblyTypes(typeof(DefaultFormsAuthentication).Assembly) .Where(t => t.Name.EndsWith("Authentication")) .AsImplementedInterfaces().InstancePerHttpRequest(); builder.Register(c => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>( new SocialGoalEntities()))) .As<UserManager<ApplicationUser>>().InstancePerHttpRequest(); builder.RegisterFilterProvider(); IContainer container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); } } }
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。