我有一个我正在调用EndPointConfigurator的类,我想在其中注入一些东西,特别是一个工作单元,并在该类中使用它。
public class EndPointConfigurator { private static UnitOfWork _unitOfWork; public EndPointConfigurator(IUnitOfWork uow) { _unitOfWork = (UnitOfWork)uow; } //use unit of work }我这样注册我的控制器:
builder.RegisterAssemblyTypes(getExecutingAssembly) .Where(t => t.Name.EndsWith("Controller", StringComparison.InvariantCulture));我想知道注册这个类的最佳方法是什么,以便它可以像控制器那样注入一些东西。 控制器没有接口,我不想或不需要这个类的接口,因为我不需要在任何地方注入它。
I have a class that I'm calling EndPointConfigurator and I would like to inject some things, specifically a Unit of Work, into it and use it in that class.
public class EndPointConfigurator { private static UnitOfWork _unitOfWork; public EndPointConfigurator(IUnitOfWork uow) { _unitOfWork = (UnitOfWork)uow; } //use unit of work }I register my controllers as such:
builder.RegisterAssemblyTypes(getExecutingAssembly) .Where(t => t.Name.EndsWith("Controller", StringComparison.InvariantCulture));I was wondering what is the best way to register this class so that it can get something injected into it like controllers do. Controllers don't have interfaces and I don't want or need an interface for this class because I won't be needing to inject it anywhere.
最满意答案
您可以将类型注册为自己:
builder.RegisterType<EndPointConfigurator>().As<EndpointConfigurator>();要么
builder.RegisterType<EndPointConfigurator>().AsSelf();或者,如果您未指定任何服务, Autofac将自动注册为自己
builder.RegisterType<EndPointConfigurator>();我更喜欢第一种解决方案,这样我们明确告诉容器我们想要做什么。
You can register the type as itself :
builder.RegisterType<EndPointConfigurator>().As<EndpointConfigurator>();or
builder.RegisterType<EndPointConfigurator>().AsSelf();or if you don't specify any service, Autofac will automatically register as itself
builder.RegisterType<EndPointConfigurator>();I prefer the first solution, this way we explicity tell the container what we want to do.
更多推荐
发布评论