2016-11-20 13:34:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.Default
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* !! IoC Container 9000 !!
|
|
|
|
|
*
|
|
|
|
|
* ...you probably want to replace this one as soon as possible.
|
|
|
|
|
*
|
|
|
|
|
* A Simple Injector implementation is provided in the Tapeti.SimpleInjector package.
|
|
|
|
|
*/
|
|
|
|
|
public class DefaultDependencyResolver : IDependencyInjector
|
|
|
|
|
{
|
|
|
|
|
private readonly Lazy<DefaultControllerFactory> controllerFactory;
|
|
|
|
|
private readonly Lazy<DefaultRoutingKeyStrategy> routingKeyStrategy = new Lazy<DefaultRoutingKeyStrategy>();
|
|
|
|
|
private readonly Lazy<DefaultMessageSerializer> messageSerializer = new Lazy<DefaultMessageSerializer>();
|
2016-11-21 19:54:29 +00:00
|
|
|
|
private readonly Lazy<ILogger> logger;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DefaultDependencyResolver(Func<IPublisher> publisherFactory)
|
|
|
|
|
{
|
|
|
|
|
controllerFactory = new Lazy<DefaultControllerFactory>(() => new DefaultControllerFactory(publisherFactory));
|
2016-11-21 19:54:29 +00:00
|
|
|
|
|
|
|
|
|
logger = new Lazy<ILogger>(() =>
|
|
|
|
|
{
|
|
|
|
|
// http://stackoverflow.com/questions/6408588/how-to-tell-if-there-is-a-console
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// ReSharper disable once UnusedVariable
|
|
|
|
|
var dummy = Console.WindowHeight;
|
|
|
|
|
|
|
|
|
|
return new ConsoleLogger();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return new DevNullLogger();
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public T Resolve<T>() where T : class
|
|
|
|
|
{
|
|
|
|
|
if (typeof(T) == typeof(IControllerFactory))
|
|
|
|
|
return (T)(controllerFactory.Value as IControllerFactory);
|
|
|
|
|
|
|
|
|
|
if (typeof(T) == typeof(IRoutingKeyStrategy))
|
|
|
|
|
return (T)(routingKeyStrategy.Value as IRoutingKeyStrategy);
|
|
|
|
|
|
|
|
|
|
if (typeof(T) == typeof(IMessageSerializer))
|
|
|
|
|
return (T)(messageSerializer.Value as IMessageSerializer);
|
|
|
|
|
|
2016-11-21 19:54:29 +00:00
|
|
|
|
if (typeof(T) == typeof(ILogger))
|
|
|
|
|
return (T)logger.Value;
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void RegisterController(Type type)
|
|
|
|
|
{
|
|
|
|
|
controllerFactory.Value.RegisterController(type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|