我需要能够为特定类型注册一个回调操作,当在容器中注册该类型时将调用该回调操作。这是与专门框架集成的一部分。
void CallbackWhenRegistered<T>(Action action);
我正在寻找一种最佳方法,因为我不喜欢创建用于包装所有Register方法的适配器。
是否有任何内置功能可以在容器中注册特定类型时帮助触发提供的操作?
这里是一个(未经测试的)适配器,可能会让您入门:
public sealed class SimpleInjectorMvxIoCProvider : IMvxIoCProvider, IDisposable
{
private readonly Container container;
private readonly IServiceProvider provider;
private readonly Scope scope;
public SimpleInjectorMvxIoCProvider(Container container)
{
container.Options.DefaultScopedLifestyle = ScopedLifestyle.Flowing;
this.container = container;
this.provider = container;
}
private SimpleInjectorMvxIoCProvider(Container container, Scope scope)
{
this.scope = scope;
this.container = container;
this.provider = container;
}
public void CallbackWhenRegistered<T>(Action action)
{
throw new NotImplementedException();
}
public void CallbackWhenRegistered(Type type, Action action)
{
throw new NotImplementedException();
}
public bool CanResolve<T>() where T : class =>
this.container.GetRegistration<T>(throwOnFailure: false) != null;
public bool CanResolve(Type type) =>
this.container.GetRegistration(type, throwOnFailure: false) != null;
public T Create<T>() where T : class => this.container.GetInstance<T>();
public object Create(Type type) => this.container.GetInstance(type);
public IMvxIoCProvider CreateChildContainer() =>
new SimpleInjectorMvxIoCProvider(this.container, new Scope(this.container));
public T GetSingleton<T>() where T : class => this.Create<T>();
public object GetSingleton(Type type) => this.Create(type);
public T IoCConstruct<T>() where T : class
{
throw new NotImplementedException();
}
public T IoCConstruct<T>(IDictionary<string, object> arguments) where T : class
{
throw new NotImplementedException();
}
public T IoCConstruct<T>(object arguments) where T : class
{
throw new NotImplementedException();
}
public T IoCConstruct<T>(params object[] arguments) where T : class
{
throw new NotImplementedException();
}
public object IoCConstruct(Type type)
{
throw new NotImplementedException();
}
public object IoCConstruct(Type type, IDictionary<string, object> arguments)
{
throw new NotImplementedException();
}
public object IoCConstruct(Type type, object arguments)
{
throw new NotImplementedException();
}
public object IoCConstruct(Type type, params object[] arguments)
{
throw new NotImplementedException();
}
public void RegisterSingleton<TInterface>(TInterface theObject) where TInterface : class =>
this.container.RegisterInstance(theObject);
public void RegisterSingleton(Type tInterface, object theObject) =>
this.container.RegisterInstance(tInterface, theObject);
public void RegisterSingleton<TInterface>(Func<TInterface> theConstructor) where TInterface : class =>
this.container.RegisterSingleton(theConstructor);
public void RegisterSingleton(Type tInterface, Func<object> theConstructor) =>
this.container.RegisterSingleton(tInterface, theConstructor);
public void RegisterType<TInterface>(Func<TInterface> constructor) where TInterface : class =>
this.container.Register(constructor);
public void RegisterType(Type t, Func<object> constructor) =>
this.container.Register(t, constructor);
public void RegisterType(Type tFrom, Type tTo) => this.container.Register(tFrom, tTo);
public T Resolve<T>() where T : class => this.container.GetInstance<T>();
public object Resolve(Type type) => this.container.GetInstance(type);
public bool TryResolve<T>(out T resolved) where T : class
{
resolved = this.provider.GetService(typeof(T)) as T;
return resolved != null;
}
public bool TryResolve(Type type, out object resolved)
{
resolved = this.provider.GetService(type);
return resolved != null;
}
public void RegisterType<TFrom, TTo>()
where TFrom : class
where TTo : class, TFrom
{
this.container.Register<TFrom, TTo>();
}
public void Dispose()
{
if (this.scope != null)
{
this.scope.Dispose();
}
else
{
this.container.Dispose();
}
}
}