相当于 Unity Func<T,TResult> 注册的简单注入器
Simple injector equivalent of Unity Func<T,TResult> registration
在 Unity 中,下面一行解决了我的问题。
container.RegisterType<Func<Type, Object>>(new InjectionFactory(x => new Func<Type, Object>((y) => x.Resolve(y))));
我可以使用 Simple Injector 实现吗?
或者有更好的方法来注册我的工厂?
public class QueryFactory : IQueryFactory
{
private readonly Func<Type, object> _resolveCallback;
public QueryFactory(Func<Type, object> resolveCallback)
{
_resolveCallback = resolveCallback;
}
public T ResolveQuery<T>()
where T : class, IQuery
{
return _resolveCallback(typeof(T)) as T;
}
}
好的,我解决了
container.Register<Func<Type, Object>>(() => { return (x) => container.GetInstance(x); });
在 Unity 中,下面一行解决了我的问题。
container.RegisterType<Func<Type, Object>>(new InjectionFactory(x => new Func<Type, Object>((y) => x.Resolve(y))));
我可以使用 Simple Injector 实现吗?
或者有更好的方法来注册我的工厂?
public class QueryFactory : IQueryFactory
{
private readonly Func<Type, object> _resolveCallback;
public QueryFactory(Func<Type, object> resolveCallback)
{
_resolveCallback = resolveCallback;
}
public T ResolveQuery<T>()
where T : class, IQuery
{
return _resolveCallback(typeof(T)) as T;
}
}
好的,我解决了
container.Register<Func<Type, Object>>(() => { return (x) => container.GetInstance(x); });