【问题标题】:Factory Pattern with Identity 2.0 and Entity Framework带有 Identity 2.0 和实体框架的工厂模式
【发布时间】:2016-09-09 01:05:59
【问题描述】:

我们正在开发带有实体框架和身份 2.0 的全栈授权 WebApi。

它基于这里的 git repo

WebApi Full Stack Entity Framework Repository

在我们的服务层中,我们从上下文传递自定义接口,即

    private readonly Func<IGPFocusDataContext> _contextFactory;

    public PatientService(Func<IGPFocusDataContext> contextFactory)
    {
        this._contextFactory = contextFactory;
    }

我已经创建了一个类似的 UserService,此时我想以类似的方式注入 UserManager 和 RoleManager 接口。

谁能推荐最好的方法?

【问题讨论】:

    标签: c# entity-framework asp.net-web-api


    【解决方案1】:

    查看 repo,您​​可以将 UserManger 和 RoleManager 的实例/实现添加到 UnityConfig.cs 中的 Unity 容器中,例如:

    container.RegisterType<UserManager, UserManager>();
    

    或者

    container.RegisterType<IUserManager, UserManager>();
    

    然后在你的用户服务中

    private readonly IUserManger _userManger;
    private readonly IRoleManger _roleManger;
    
    public UserService(IUserManger userManger, IRoleManager roleManger)
    {
        this._userManager = userManger;
        this._roleManger = roleManger;
    }
    

    当您在控制器中添加类似于 PatientService 中的 PatientService 的服务时,虽然我不熟悉 Unity,但我希望 Unity 将它们联系在一起。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多