.net core 【二】
一、构造引擎:
1.首先Core层创建一个接口实例:
using System; using System.Collections.Generic; using System.Text; namespace General.Core { //系统的引擎接口 public interface IEngine { /// <summary> /// 构建一个实例 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> T Resolve<T>() where T : class; } }
2. web层写一个继承实例的方法
using System; using System.Collections.Generic; using System.Text; namespace General.Core { //系统的引擎接口 public interface IEngine { /// <summary> /// 构建一个实例 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> T Resolve<T>() where T : class; } }
3. 在core层添加该方法
using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime; using System.Text; namespace General.Core { public class EnginContext { private static IEngine _engine; /// <summary> /// 静态方法的形式去构造出一个对象 /// </summary> /// <param name="engine"></param> /// <returns></returns> /// 该方法就是保证每次只有一个 [MethodImpl(MethodImplOptions.Synchronized)] public static IEngine Initialize(IEngine engine) { if (_engine == null) _engine = engine; return _engine; } /// <summary> /// 当前引擎 /// </summary> public static IEngine Current { get { return _engine; } } } }
4. 添加一个业务层操作对象
// 添加对象 EnginContext.Initialize(new GeneralEngine(services.BuildServiceProvider()));