我想用mvc3应用程序实现autofac存储库。请让我知道如何逐步实施。
答案 0 :(得分:1)
这是我在这种情况下会做的事情。
1)我会让NuGet
下载并解析AutoFac
的所有依赖项。从包管理器控制台,执行
PM> Install-Package Autofac
这应该会自动引用项目中的Autofac
和Autofac.Integration.Mvc
。
2)使用以下代码在应用程序根目录中创建Bootstrapper.cs
public static class Bootstrapper
{
public static void Run()
{
SetAutofacContainer();
}
private static void SetAutofacContainer()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
// Register all respository classes
builder.RegisterAssemblyTypes(typeof(ReportRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces().InstancePerHttpRequest();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
3)从Global.asax.cs调用Bootstrapper's Run
方法
protected void Application_Start()
{
Bootstrapper.Run();
}
答案 1 :(得分:0)
1) Download autofac dll from
https://code.google.com/p/autofac/downloads/list?can=1&q=
For example :
https://code.google.com/p/autofac/downloads/detail?name=Autofac-3.0.0.0-NET40.zip&can=1&q=
2) Add the reference of dll.
3) Register SQL class in global.asax file.
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<DataManagerMasterSQL>().AsImplementedInterfaces().InstancePerLifetimeScope();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));