1.在寫邏輯層前,需要弄好反轉控制,因框架沒有寫接口,所以Ninject只負責返回當前實例,有點類似共享設計模式。
public sealed class IOCHelper
{
private static IKernel kernel = new StandardKernel();
/// <summary>
/// 获取注入实例
/// </summary>
/// <typeparam name="T">接口名</typeparam>
/// <param name="name">实例配置名</param>
/// <returns>T的实现实例</returns>
public static T GetInstance<T>(string name = "") where T : class
{
try
{
if (string.IsNullOrEmpty(name))
return kernel.Get<T>();
return kernel.Get<T>(name);
}
catch (Exception ex)
{
throw new Exception("获取注入实例错误,请确认配置是否正确", ex);
}
}
}