44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace FlowScope.Container
|
|
{
|
|
public static class GeneratedFactories
|
|
{
|
|
private static readonly Dictionary<Type, Func<Container, object>> Factories = new Dictionary<Type, Func<Container, object>>();
|
|
|
|
public static void Register<TImplementation>(Func<Container, TImplementation> factory)
|
|
{
|
|
if (factory == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(factory));
|
|
}
|
|
|
|
Factories[typeof(TImplementation)] = c => factory(c);
|
|
}
|
|
|
|
public static bool TryGetFactory(Type implementationType, out Func<Container, object> factory)
|
|
{
|
|
if (implementationType == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(implementationType));
|
|
}
|
|
|
|
return Factories.TryGetValue(implementationType, out factory);
|
|
}
|
|
|
|
public static void RegisterAssembly(Container container)
|
|
{
|
|
if (container == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(container));
|
|
}
|
|
|
|
foreach (var pair in Factories)
|
|
{
|
|
container.RegisterFactory(pair.Key, pair.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|