я с трудом интегрирующую autofac в моем applcation ..Autofac решительность в глубоком слое
Мое приложение интегрируется с другими приложениями, когда соединение с другим приложением, его поддерживается с помощью фитинга «Протокол» объекта
// This class is inherited by a few other classes
public abstract class Protocol
У меня есть слой, который запускается собственным потоком и обрабатывает запросы на соединение. Для каждого вида запроса создается другой вид протокола (другой объект протокола)
// 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, был бы очень признателен объяснение)
А вот мой ужасный класс:
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 <> который я слышал.
Спасибо!
Cyril спасибо! Не могли бы вы более подробно объяснить, как будет выглядеть этот IProtocolsFactory? Это звучит как идеальное решение для меня –
@ S.Peter Я отредактировал мое сообщение, чтобы включить образец 'IProtocolFactory' –
. Как я мог его использовать, чем без выдержки на IIndex <> –