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 關注
- 992 瀏覽
添加回答
舉報
0/150
提交
取消
