RT, 目前场景是 一个core 2.1 的web程序,使用autofac进行了service的注入(接口),并且在controller 中使用构造函数注入或者属性注入都可以正常work。此时写了个 baseWebController ,该基类内部需要用到一个 IServiceA 的一个funA()去获取一个 user对象,并保存在基类里,因为有很多controller 都会用到当前这个user对象, 那么问题来了,baseWebController 里边尝试使用属性注入 IServiceA 始终得不到注入的对象,如果改成构造函数注入,那么所有的controller 都要 额外写 构造函数(IServiceA a): base(a) 感觉这样的改动面是不是有点大了? 有什么合适的解决方案吗?
1 回答
慕雪6442864
TA贡献1812条经验 获得超5个赞
public interface IEngine {
T Resolve<T>() where T : class;
object Resolve(Type type);
IEnumerable<T> ResolveAll<T>();
object ResolveUnregistered(Type type);
} public class EngineContext
{ public static IEngine Create() =>
Singleton<IEngine>.Instance ?? (Singleton<IEngine>.Instance = new Engine()); public static void Replace(IEngine engine) => Singleton<IEngine>.Instance = engine; public static IEngine Current { get { if (Singleton<IEngine>.Instance == null) {
Create();
} return Singleton<IEngine>.Instance;
}
}
} public class Engine : IEngine {
private IServiceProvider _serviceProvider { get; set; } public virtual IServiceProvider ServiceProvider => _serviceProvider; protected IServiceProvider GetServiceProvider() { var accessor = ServiceProvider.GetService<IHttpContextAccessor>(); var context = accessor.HttpContext; return context != null ? context.RequestServices : ServiceProvider;
}
public T Resolve<T>() where T : class { return (T)GetServiceProvider().GetRequiredService(typeof(T));
} public object Resolve(Type type) { return GetServiceProvider().GetRequiredService(type);
} public IEnumerable<T> ResolveAll<T>() { return (IEnumerable<T>)GetServiceProvider().GetService(typeof(T));
} public object ResolveUnregistered(Type type) {
Exception innerException = null; foreach (var constructor in type.GetConstructors()) { try { var parameters = constructor.GetParameters().Select(parameter => { var service = Resolve(parameter.ParameterType); if (service == null) throw new Exception("Unknown dependency"); return service;
}); return Activator.CreateInstance(type, parameters.ToArray());
} catch (Exception ex) {
innerException = ex;
}
} throw new ArgumentException("No constructor was found that had all the dependencies satisfied.", innerException);
}
}
public class Singleton<T> : Singleton { private static T _instance; public static T Instance { get => _instance; set {
_instance = value;
AllSingletons[typeof(T)] = value;
}
}
} public class SingletonList<T> : Singleton<IList<T>> { static SingletonList() {
Singleton<IList<T>>.Instance = new List<T>();
} public new static IList<T> Instance => Singleton<IList<T>>.Instance;
} public class SingletonDictionary<TKey, TValue> : Singleton<IDictionary<TKey, TValue>> { static SingletonDictionary() {
Singleton<Dictionary<TKey, TValue>>.Instance = new Dictionary<TKey, TValue>();
} public new static IDictionary<TKey, TValue> Instance => Singleton<Dictionary<TKey, TValue>>.Instance;
} public class Singleton { static Singleton() {
AllSingletons = new ConcurrentDictionary<Type, object>();
} public static IDictionary<Type, object> AllSingletons { get; }
}在BaseController调用方法
var serviceA = EngineContext.Current.Resolve<IServiceA>();
这个方法提取自nopCommerce,可以在github找到
- 1 回答
- 0 关注
- 991 浏览
添加回答
举报
0/150
提交
取消
