向简单注入器注册类型时的回调操作



我需要能够为特定类型注册一个回调操作,当该类型在容器中注册时将被调用。这是与专用框架集成的一部分。

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)
{
}
public void CallbackWhenRegistered(Type type, Action action)
{
}
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();
}
}
}

更新

在查看了 MvvmCross 源代码后,我注意到框架只关心一些服务,它们是:

  • IMvxValueConverterRegistry
  • IMvxTargetBindingFactoryRegistry
  • IMvxTypeCache<View>
  • IMvxNamespaceListViewTypeResolver
  • IMvxValueCombinerRegistry

除非您覆盖这些抽象,否则无需实现CallbackWhenRegistered

最新更新