我们有一个ASP.NET项目,我们使用AutoFac到DI。 我们有一个包含所有数据库查询的服务层,我们需要在静态类中进行一些查询。静态类AutoFac DI
这是我们注册的依赖在Global.asax:
public class Dependencies
{
public static void RegisterDependencies()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();
builder.RegisterModule(new ServiceModule());
builder.RegisterModule(new EfModule());
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
public class ServiceModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(Assembly.Load("MyApp.Service")).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerLifetimeScope();
}
}
public class EfModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType(typeof(myDataContext)).As(typeof(IMyContext)).InstancePerLifetimeScope();
}
}
这是我们在控制器如何访问:
public class SomeController : Controller
{
private readonly IService1 _service1;
private readonly IService2 _service2;
public SomeController(IService1 service1, IService2 service2)
{
_service1 = service1;
_service2 = service2;
}
public ActionResult Index()
{
var service = _service1.GetAll();
...
return View(searchModel);
}
}
现在,我们需要从数据库中检索数据在一个静态类中,所以我们必须调用我们的服务层,但我们不知道该怎么做......我们已经看到了这一点,但我不知道它是否正确,但它的工作原理。
public static Test()
{
...
var service1 = DependencyResolver.Current.GetService<IService1>();
...
}
另外,它将如何在非静态类和静态类?
在此先感谢。
你不能(也不应该)注入静态类。想想当2个Web请求同时进入并尝试访问相同的上下文对象时会发生什么。相反,为什么不通过服务作为参数? – DavidG
好的,如果班级不是静态的呢?我怎样才能做到这一点?我想要的是,我可以在不知道里面发生了什么的情况下调用类中的方法,因为我需要从数据库中检索信息,所以我需要调用一个服务(或多个),但我不想担心这些班级以外的服务......因为我只想调用它并获取信息。 –
您的静态类在请求的生命周期中位于何处?如果它在'Controller'中的某个地方被调用,按照DavidG的说法:将该函数声明为'public static Test(IService1 service1)',并从'Controller'传入'service1'。 –