抽象工厂和控制反转在运行时解决

Abstract Factory and Inversion of Control resolve at runtime

我有以下 class 和接口结构,我很难尝试让代码执行我需要的操作。

public interface IUserManager
{
    int Add(User user);
}

public class UserManagerA : IUserManager{}
public class UserManagerB : IUserManager{}

在此示例中,我使用 Ninject 作为 IoC 容器,但如果其他容器解决了问题,我愿意更改它:

这是我的 NinjectWebCommon.cs:

void RegisterServices(IKernel kernel)
{
    string userRole = CurrentUser.Role;//this gets the user logged in
    //This is the part I do not how to do
    //I wish I could just type this in:
    kernel.Bind<IUserManager>().To<UserManagerA>()
        .When(userRole == "RoleA"); // this doesn't work obviously
    kernel.Bind<IUserManager>().To<UserManagerB>()
        .When(userRole == "RoleB"); // same doesn't work
}

所有这些,以便在我的 (MVC) 控制器中我可以这样做:

public class UserController
{
    private readonly IUserManager _userManager;
    public UserController(IUserManager userManager)
    {
        _userManager = userManager;
    }
    public ActionResult Add(User user)
    {
        //this would call the correct manager
        //based on the userRole
        _userManager.Add(user);
    }
}

我一直在阅读关于抽象工厂的文章,但没有找到一篇解释如何将工厂与 IoC 容器集成并传递在 运行 时获得的参数来解析实现的文章。

创建一个 class 负责提供正确的 UserManager 并将其注入您的控制器:

public class UserManagerProvider : IUserManagerProvider
{
    private readonly IContext _context;

    public UserManagerProvider(IContext context)
    {
        _context = context;
    }

    public IUserManager Create(User currentUser)
    {
        if (currentUser.Role == "User A")
            return _context.Kernel.Get<UserManagerA>();

        if (currentUser.Role == "User B")
            return _context.Kernel.Get<UserManagerB>();

        // Or bind and resolve by name
        // _context.Kernel.Get<IUserManager>(currentUser.Role);
    }
}

在控制器中:

private readonly IUserManager _userManager;

public UserController(IUserManagerProvider userManagerProvider)
{
    _userManager = userManagerProvider.Create(CurrentUser);
}

此外,作为旁注,您可能应该有一个 CurrentUserProvider 负责获取当前用户。依赖静态方法会使单元测试变得困难,并且您实质上是在所有引用它的 classes 中隐藏了依赖项:

private readonly IUserManager _userManager;
private readonly User _currentUser;

public UserController(IUserManagerProvider userManagerProvider, ICurrentUserProvider currentUserProvider)
{
    _currentUser = currentUserProvider.GetUser();
    _userManager = userManagerProvider.Create(_currentUser);
}

如果 IUserManager 实现的数量不是很多(不太可能达到 100 个实现),您可以使用 Strategy Pattern 在组合过程中解析所有 UserManager 实例,然后选择在运行时使用的最佳实例。

首先,我们需要一种方法将 IUserManager 实现映射到角色。

public interface IUserManager
{
    int Add(User user);
    bool AppliesTo(string userRole);
}

public class UserManagerA : IUserManager
{
    // Add method omitted

    public bool AppliesTo(string userRole)
    {
        // Note that it is entirely possible to 
        // make this work with multiple roles and/or
        // multiple conditions.
        return (userRole == "RoleA");
    }
}

public class UserManagerB : IUserManager
{
    // Add method omitted

    public bool AppliesTo(string userRole)
    {
        return (userRole == "RoleB");
    }
}

然后我们需要一个策略 class 来根据 userRole 选择正确的实例。 IUserManager 个实例在组合应用程序时由 DI 容器提供。

public interface IUserManagerStrategy
{
    IUserManager GetManager(string userRole);
}

public class UserManagerStrategy
    : IUserManagerStrategy
{
    private readonly IUserManager[] userManagers;

    public UserManagerStrategy(IUserManager[] userManagers)
    {
        if (userManagers == null)
            throw new ArgumentNullException("userManagers");

        this.userManagers = userManagers;
    }

    public IUserManager GetManager(string userRole)
    {
        var manager = this.userManagers.FirstOrDefault(x => x.AppliesTo(userRole));
        if (manager == null && !string.IsNullOrEmpty(userRole))
        {
            // Note that you could optionally specify a default value
            // here instead of throwing an exception.
            throw new Exception(string.Format("User Manager for {0} not found", userRole));
        }

        return manager;
    }
}

用法

public class SomeService : ISomeService
{
    private readonly IUserManagerStrategy userManagerStrategy;

    public SomeService(IUserManagerStrategy userManagerStrategy)
    {
        if (userManagerStrategy == null)
            throw new ArgumentNullException("userManagerStrategy");
        this.userManagerStrategy = userManagerStrategy;
    }

    public void DoSomething()
    {
        string userRole = CurrentUser.Role;//this gets the user logged in

        // Get the correct UserManger according to the role
        IUserManager userManager = this.userManagerStrategy.GetManger(userRole);

        // Do something with userManger
    }
}

void RegisterServices(IKernel kernel)
{
    kernel.Bind<IUserManager>().To<UserManagerA>();
    kernel.Bind<IUserManager>().To<UserManagerB>();

    // Ninject will automatically supply both IUserManager instances here
    kernel.Bind<IUserManagerStrategy>().To<UserManagerStrategy>();

    kernel.Bind<ISomeService>().To<SomeService>();
}

此方法不需要您将容器注入应用程序。没有正在使用的服务位置。

另请注意,没有每次向应用程序添加新 UserManager 时都必须修改的 switch case 语句。何时使用 UserManager 的逻辑是 UserManager 实现的一部分,执行逻辑的顺序由 DI 配置决定。

此外,无论您使用哪个 DI 容器,这都有效。

您可以将它与 RagtimeWilly 的回答中的 CurrentUserProvider 结合起来,以一种干净的方式将用户角色放入使用它的服务中。

参考:Best way to use StructureMap to implement Strategy pattern