##// END OF EJS Templates
changed urls in Implab.csproj
changed urls in Implab.csproj

File last commit:

r289:95896f882995 v3.0.14 v3
r296:58f49e47605b default
Show More
FactoryActivator.cs
77 lines | 3.0 KiB | text/x-csharp | CSharpLexer
using System;
using System.Collections.Generic;
using System.Reflection;
using Implab.Components;
using Unity;
using Unity.Injection;
using Unity.Lifetime;
namespace Implab.ServiceHost.Unity {
public class FactoryActivator : ITypeRegistration {
class FactoryInjector : ITypeMemberInjection {
public InjectionFactory Factory { get; set; }
public void Visit(TypeRegistrationBuilder builder) {
builder.AddInjectionMember(Factory);
}
}
public Type FactoryType { get; set; }
public string FactoryName { get; set; }
public Type RegistrationType { get; set; }
public LifetimeManager Lifetime { get; set; }
public IEnumerable<ITypeMemberInjection> MemberInjections {
get {
if (RegistrationType == null)
throw new Exception($"RegistrationType must be specified");
if (!typeof(IFactory<>).MakeGenericType(RegistrationType).IsAssignableFrom(FactoryType))
throw new Exception($"The factory {FactoryType} can't be used to create {RegistrationType} instances");
if (FactoryType.ContainsGenericParameters) {
yield return new FactoryInjector {
Factory = CreateDynamicInjectionFactory(FactoryName)
};
} else {
yield return new FactoryInjector {
Factory = (InjectionFactory)GetType()
.GetMethod(nameof(CreateInjectionFactory), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod(FactoryType, RegistrationType)
.Invoke(null, new [] { FactoryName })
};
}
}
}
public string Name { get; set; }
public Type GetRegistrationType(ContainerBuilder builder) {
return RegistrationType;
}
public LifetimeManager GetLifetime(ContainerBuilder builder) {
return Lifetime;
}
public Type GetImplementationType(ContainerBuilder builder) {
return null;
}
/// <summary>
/// Указывает зависимость, реализующую интерфейс <see cref="IFactory{TObj}"/>,
/// которая будет использоваться в качестве фабрики для создания объектов
/// </summary>
/// <param name="dependencyName"></param>
static InjectionFactory CreateInjectionFactory<TFac, TObj>(string dependencyName) where TFac : IFactory<TObj> {
return new InjectionFactory(c => c.Resolve<TFac>(dependencyName).Create());
}
static InjectionFactory CreateDynamicInjectionFactory(string dependencyName) {
return new InjectionFactory((c,t,name) => ((IFactory<object>)c.Resolve(t, dependencyName)).Create());
}
}
}