Autofac在深层解析 [英] Autofac resolve in deep layer
问题描述
我很难在应用程序中集成autofac.
I'm having hard time integrating autofac in my applcation..
当通过合适的协议"对象维护与另一个应用程序的连接时,我的应用程序与其他应用程序集成
My application integrates with other applications, when a connection to another application is made its being maintained by a fitting "Protocol" object
// This class is inherited by a few other classes
public abstract class Protocol
我有一个由其自己的线程运行并处理连接请求的层.对于每种请求,都会创建不同种类的协议(一个不同的协议对象)
I have a layer that is run by it's own thread and handles connection requests. For each kind of request a different kind of protocol is created (a different protocol object)
// For example lets say every protocol (Inhertiance of Protocol abstract object)
// takes in ctor these 3 services + runtime configuration object:
public Protocol1(IFramingAgent, IFramingAlgorithm, IFramingParser, configObject configuration)
我的协议对象被密钥注册为协议.还, 每个服务都注册有密钥,因为每个协议都使用从同一接口继承的不同种类的协议.当然,所有这些都已注册为PerDependency(尽管我不太了解此LifeCycle与PerScope之间的区别,希望能得到一个解释)
My protocol objects are keyed registered as Protocol. Also, Each service is registered with key because each protocol uses a different kind that inherits from the same interface. Of course, all of these are registered as PerDependency (although I don't really understand the difference between this lifeCycle against PerScope, would really appreciate an explanation)
这是我的糟糕课:
public class ProtocolsLayer : Layer
{
private IFrameworkDependencyResolver _resolver;
private IConfigurationService _configService;
public ProtocolsLayer(IFrameworkDependencyResolver resolver, IConfigurationService configurationService)
{
_resolver = resolver;
_configService = configurationService;
}
void HandleConnection1()
{
// What I have at the moment (terrible):
// Resolve the fitting services (All keyed - key is received by the type, Resolve and ResolveWithParameters used here are my wrappers)
var agent = _resolver.Resolve<IFramingAgent>(typeof(Protocol1FramingAgent));
var algo = _resolver.Resolve<IFramingAlgorithm>(typeof(Protocol1FramingAlgorith));
var parser = _resolver.Resolve<IFramingParser>(typeof(Protocol1FramingParser));
// A parameter I get and pass to each protocol at runtime
var protocolConfig = _configService.GetConfig<Protocol1Configuration>();
// Finally resolve the protocol with it's parameters:
protocol = _resolver.ResolveWithParameters<IProtocol>(typeof(Protocol1), new List<object>{
agent, resolver, parser, protocolConfig
});
//...
// Theres gotta be a better way!!
}
void HandleConntection2()
{
// Same as in protocol1
}
void HandleConnection3()
{
// Same as in protocol1
}
}
请记住,我不希望引用autofac,这意味着我不能使用听到的IIndex<>.
Take in mind that I don't want references to autofac, meaning I can't use IIndex<> which I heard off.
谢谢!
推荐答案
您应该让依赖项注入框架管理实例化.
You should let the dependency injection framework manage instanciation.
您使用ResolveWithParameter
方法,其中这些参数已由依赖项注入框架解析.它不是必需的,您可以让框架为您找到依赖项:
You use a ResolveWithParameter
method where these parameters has been resolved by the dependency injection framework. It is not required and you can let the framework find the dependency for you :
如果Protocol1
需要命名参数,则可以在注册过程中指定它.
If Protocol1
needs a named parameter you can specify it during registration process.
builder.Register(c => c.Resolve<IConfigurationService>()
.GetConfig<Protocol1Configuration>())
.As<Protocol1Configuration>();
builder.RegisterType<Protocol1>()
.Named<IProtocol1>(nameof(Protocol1))
.WithParameter((pi, c) => pi.ParameterType == typeof(IFramingAgent),
(pi, c) => c.ResolveNamed<IFramingAgent>(nameof(Protocol1))
.WithParameter((pi, c) => pi.ParameterType == typeof(IFramingAlgorithm),
(pi, c) => c.ResolveNamed<IFramingAlgorithm>(nameof(Protocol1));
builder.RegisterType<FramingAgentProtocol1>()
.Named<IFramingAgent>(nameof(Protocol1));
builder.RegisterType<FramingAlgorithmProtocol1>()
.Named<IFramingAlgorithm>(nameof(Protocol1));
然后ProtocolsLayer
只需要依赖IIndex<String, Func<IProtocol>>
(或Func<Owned<Protocol1>>
)
public class ProtocolsLayer
{
public ProtocolsLayer(IIndex<String, Func<IProtocol>> index)
{
this._index = index;
}
private readonly IIndex<String, Func<IProtocol>> _index;
public void HandleConnection1()
{
IProtocol protocol = this._index[nameof(Protocol1)]();
}
}
如果不想为整个应用程序引入对IIndex<,>
的依赖性,则可以引入将在运行时中定义的IProtocolFactory
,并仅为注册项目创建实现.
If you don't want to introduce dependency on IIndex<,>
for your whole application you can introduce a IProtocolFactory
that will be defined in your runtime and create the implementation only for the registration project.
在您的运行时项目中:
public interface IProtocolFactory
{
IProtocol Create(String protocolName)
}
在您的注册项目中:
public class ProtocolFactory : IProtocolFactory
{
public ProtocolFactory(IIndex<String, IProtocol> index)
{
this._index = index;
}
private readonly IIndex<String, IProtocol> _index;
public IProtocol Create(String protocolName)
{
return this._index[typeof(TProtocol).Name];
}
}
然后,您的ProtocolsLayer
类将如下所示:
Then you ProtocolsLayer
class will look like this :
public class ProtocolsLayer
{
public ProtocolsLayer(IProtocolFactory protocolFactory)
{
this._protocolFactory = protocolFactory;
}
private readonly IProtocolFactory _protocolFactory;
public void HandleConnection1()
{
IProtocol protocol = this._protocolFactory.Create("Protocol1");
}
}
您还可以注册Func<String, IProtocol>
,该名称将命名为resolve IProtocol
You can also register a Func<String, IProtocol>
which will named resolve IProtocol
ProtocolsLayer
将如下所示:
public class ProtocolsLayer
{
public ProtocolsLayer(Func<String, IProtocol> protocolFactory)
{
this._protocolFactory = protocolFactory;
}
private readonly Func<String, IProtocol> _protocolFactory;
public void HandleConnection1()
{
IProtocol protocol = this._protocolFactory("Protocol1");
}
}
和类似的注册:
builder.Register(c => (String namedProtocol) => c.ResolveNamed<IProtocol>(namedProtocol)
.As<Func<String, IProtocol>>();
但是我不会推荐此解决方案,因为Func<String, IProtocol> protocolFactory
依赖项的意图尚不清楚.拥有IProtocolFactory
界面使依赖关系目标易于理解.
But I won't recommend this solution because the intent of Func<String, IProtocol> protocolFactory
dependency is not clear. Having a IProtocolFactory
interface makes dependency goal easy to understand.
这篇关于Autofac在深层解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!