| 本文用Unity 进行DLL注入. 逻辑不懂.网上看了好多关系unity的文章..慢慢领悟了. 1.先在nuget 上下载?unity 我用的版本是5.11.7 2.首先来一个特性类.DefaultAttribute 继承Attribute 代码: ? ?public class DefaultAttribute : Attribute? ? {
 ? ? ? ? public DefaultAttribute(Type registerAs)
 ? ? ? ? {
 ? ? ? ? ? ? this.RegisterAs = registerAs;
 ? ? ? ? }
 ? ? ? ? public Type RegisterAs { get; private set; }
 ? ? ? ? /// <summary>? ? ? ? /// 进行注入
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="container"></param>
 ? ? ? ? /// <param name="type"></param>
 ? ? ? ? public void OnVisit(IUnityContainer container, Type type)
 ? ? ? ? {
 ? ? ? ? ? ? if (type.IsAbstract)
 ? ? ? ? ? ? ? ? throw new ArgumentException(string.Format("虚拟类型{0}不能注册为服务实例。", type));
 ? ? ? ? ? ? container.RegisterType(RegisterAs, type, new ContainerControlledLifetimeManager());? ? ? ? }
 ? ? }
 3.注入项目DLL, public class SingletonUnity? ? {
 ? ? ? ? public static void CreateAutomaticInit()
 ? ? ? ? {
 ? ? ? ? ? ? #region 自动化 注入
 ? ? ? ? ? ? string path = AppDomain.CurrentDomain.BaseDirectory + "bin\\OSS.FromeWork.MES.Service.dll";
 ? ? ? ? ? ? Assembly assembly = Assembly.LoadFile(path);?
 ? ? ? ? ? ? IList<Type> types = assembly.GetTypes();
 ? ? ? ? ? ? //定义集合,存储在接口上标注DefaultAttribute的Type? ? ? ? ? ? IList<KeyValuePair<DefaultAttribute, Type>> items = new List<KeyValuePair<DefaultAttribute, Type>>();
 ? ? ? ? ? ? foreach (Type type in types)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? foreach (DefaultAttribute attr in type.GetCustomAttributes(typeof(DefaultAttribute), true))
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? items.Add(new KeyValuePair<DefaultAttribute, Type>(attr, type));
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? }
 ? ? ? ? ? ? try? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? foreach (KeyValuePair<DefaultAttribute, Type> kvp in items)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? //进行注入
 ? ? ? ? ? ? ? ? ? ? (kvp.Key).OnVisit(UnityRoot.Container, kvp.Value);
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? }
 ? ? ? ? ? ? catch (Exception ex)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? Console.WriteLine("自动注入出错");
 ? ? ? ? ? ? }
 ? ? ? ? ? ? #endregion
 ? ? ? ? }
 ? ? }
 ? ? public class UnityRoot
 ? ? {
 ? ? ? ? public static IUnityContainer Container = new UnityContainer();
 ? ? }
 4.添加仓储模式. ? ? public interface IBaseRepository<TEntity> where TEntity : class, new()? ? {
 ? ? ? ? #region Add
 ? ? ? ? /// <summary>
 ? ? ? ? /// 增加单条数据
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="model">实体对象</param>
 ? ? ? ? /// <returns>操作是否成功</returns>
 ? ? ? ? Task<bool> Add(TEntity model);
 ? ? ? ? /// <summary>
 ? ? ? ? /// 增加单条数据
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="model">实体对象</param>
 ? ? ? ? /// <returns>返回自增长ID</returns>
 ? ? ? ? Task<int> AddReturnIdentity(TEntity model);
 ? ? ? ? #endregion
 ? ? }
 ? ? [Default(typeof(IBaseRepository<>))]? ? public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, new()
 ? ? {
 ? ? ? ? #region Add
 ? ? ? ? /// <summary>
 ? ? ? ? /// 新增数据
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="model">实体数据</param>
 ? ? ? ? /// <returns>返回:bool 类型 true or false</returns>
 ? ? ? ? public virtual async Task<bool> Add(TEntity model)
 ? ? ? ? {
 ? ? ? ? ? ? var dbcontext = SqlBase.Client;
 ? ? ? ? ? ? var i = await Task.Run(() => dbcontext.Insertable<TEntity>(model).ExecuteCommandAsync());
 ? ? ? ? ? ? return i > 0;
 ? ? ? ? }
 ? ? ? ? /// <summary>
 ? ? ? ? /// 添加单条数据,并返回 自增列
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="model">实体数据</param>
 ? ? ? ? /// <returns>返回:bool 类型 true or false</returns>
 ? ? ? ? public virtual async Task<int> AddReturnIdentity(TEntity model)
 ? ? ? ? {
 ? ? ? ? ? ? var dbcontext = SqlBase.Client;
 ? ? ? ? ? ? return await Task.Run(() => dbcontext.Insertable<TEntity>(model).ExecuteReturnIdentityAsync());
 ? ? ? ? }
 ? ? ? ? #endregion
 ? ? }
 6.把仓储注入到实现的类目当中 public interface IPVDWorksheet? ? {
 ? ? ? ? /// <summary>
 ? ? ? ? /// 新增数据
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="model"></param>
 ? ? ? ? /// <returns></returns>
 ? ? ? ? Task<bool> Add(M_PVDWorksheet model);
 ? ? ? ? /// <summary>
 ? ? ? ? /// 新增数据,返回自增长ID
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="m"></param>
 ? ? ? ? /// <returns></returns>
 ? ? ? ? Task<int> AddReturnIdentity(M_PVDWorksheet m);
 ? ? }
 ?[Default(typeof(IPVDWorksheet))]? ? public class PVDWorksheet : IPVDWorksheet
 ? ? {
 ? ? ? ? public IBaseRepository<M_PVDWorksheet> DAO;
 ? ? ? ? /// <summary>
 ? ? ? ? /// 钩爪函数注入..默认调用参数最多的
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="_dal"></param>
 ? ? ? ? public PVDWorksheet(IBaseRepository<M_PVDWorksheet> _dal)
 ? ? ? ? {
 ? ? ? ? ? ? DAO = _dal;
 ? ? ? ? }
 ? ? ? ? /// <summary>
 ? ? ? ? /// 新增数据
 ? ? ? ? /// </summary>
 ? ? ? ? /// <param name="m">实体</param>
 ? ? ? ? /// <returns>可以在这里做AOP 切面编程</returns>
 ? ? ? ? public async Task<bool> Add(M_PVDWorksheet m)
 ? ? ? ? {
 ? ? ? ? ? ? return await DAO.Add(m);
 ? ? ? ? }
 ? ? ? ? public async Task<int> AddReturnIdentity(M_PVDWorksheet m)
 ? ? ? ? {
 ? ? ? ? ? ? return await DAO.AddReturnIdentity(m);
 ? ? ? ? }
 ? ? } 7.调用 private static IPVDWorksheet Dao? ? ? ? {
 ? ? ? ? ? ? get
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? return UnityRoot.Container.Resolve<IPVDWorksheet>();
 ? ? ? ? ? ? }
 ? ? ? ? }
 8.在项目启动的时候.调用 ?SingletonUnity.CreateAutomaticInit(); |