3

Lind.DDD.Repositories.EF层介绍~实现DbContext的动态注入

 2 years ago
source link: https://www.cnblogs.com/lori/p/15838647.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Lind.DDD.Repositories.EF层介绍~实现DbContext的动态注入

很久很久之前,我写了Lind.DDD文章,有兴趣可以阅读《Lind.DDD敏捷领域驱动框架》,今天主要说一下,如何动态注入DbContext,而不是new一个DbContext子类。

  • 配置文件添加上下文注册
  • 在ef仓储中添加对IOC构造方法注入的支持
  /// <summary>
  /// 通过构造方法进行DbContext上下文的注入
  /// </summary>
  [InjectionConstructor]
  public EFRepository(DbContext db)
  {
      //Db = new WEBDataEntities();
      Db = db;
      if (this.DataPageSize == 0)
      {
          int dataPageSize;
          int.TryParse(System.Configuration.ConfigurationManager.AppSettings["BulkDataPageSize"] ?? "10000", out dataPageSize);
          this.DataPageSize = dataPageSize;
      }
  }
  • 直接在业务层使用对应的仓储
public class UserService
{
        IExtensionRepository<User_Data> userDataRepository;
        IExtensionRepository<User_Code> userCodeRepository;
        IExtensionRepository<User_Set> userSetRepository;
        IExtensionRepository<User_Logs> userLogsRepository;

        public UserService()
        {
            userDataRepository = UnityHelper.Instance.GetService<IExtensionRepository<User_Data>>();
            userCodeRepository = UnityHelper.Instance.GetService<IExtensionRepository<User_Code>>();
            userSetRepository = UnityHelper.Instance.GetService<IExtensionRepository<User_Set>>();
            userLogsRepository = UnityHelper.Instance.GetService<IExtensionRepository<User_Logs>>();
        }
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK