Asp.Net MVC 和策略模式

Asp.Net MVC and Strategy pattern

我有一个使用 Entity Framework 的 MVC 应用程序。我正在使用存储库、工作单元和统一作为依赖注入。

我遇到的问题是我有不同的身份验证类型,每种类型我想要不同的class,所以我决定使用策略模式

    public interface IAuthStrategy
    {
        OperationResponse<AuthenticationMechanismDTO> GetAuthenticationMechanism(string userName);
    }

    public class AuthStrategy 
    {
        readonly IAuthStrategy _authStrategy;

        public AuthStrategy(IAuthStrategy authStrategy)
        {
            this._authStrategy = authStrategy;
        }

        public OperationResponse<AuthenticationMechanismDTO> GetAuthenticationMechanism(string userName)
        {
            return _authStrategy.GetAuthenticationMechanism(userName);

        }

    }



    public class UserNamePasswordMechanism : IAuthStrategy
    {

        private IInstitutionRepository _institutionRepository;

        public UserNamePasswordMechanism(IInstitutionRepository institutionRepository)
        {
            this._institutionRepository = institutionRepository;
        }

        public OperationResponse<AuthenticationMechanismDTO> GetAuthenticationMechanism(string userName)
        {

            throw new NotImplementedException();
        }
    }

我的问题是我将 IAuthStrategy 注入控制器,它给了我一个错误,因为我没有实现 IAuthStrategy,而是将它传递给 AuthStrategy 构造函数,正如您在我的代码中看到的那样。

我该如何解决这个错误?

这是我的控制器

 public class EmployeeController : ApiController
        {

            private IAuthStrategy _auth;

            public EmployeeController(IAuthStrategy auth)
            {
                this._employeeBL = employeeBL;
                this._auth = auth;

            }}
    }

这是我在其中注册我的类型的统一配置

public class UnityConfig
    {
        #region Unity Container
        private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
        {
            var container = new UnityContainer();
            RegisterTypes(container);
            return container;
        });

        /// <summary>
        /// Gets the configured Unity container.
        /// </summary>
        public static IUnityContainer GetConfiguredContainer()
        {
            return container.Value;
        }
        #endregion

        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>


   public static void RegisterTypes(IUnityContainer container)
    {
        // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
        // container.LoadConfiguration();

        // TODO: Register your types here

        container.RegisterType<IInstitutionRepository, InstitutionRepository>();

        container.RegisterType<IAuthStrategy, AuthStrategy>();

        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

    }
}

您的单位注册和 类 看起来有点不对劲。

据我所知,这是你真正想做的。

设置将在运行时确定应使用哪个 IAuthStrategy 的工厂:

public interface IAuthStrategyFactory
{
    IAuthStrategy GetAuthStrategy();
}

public class AuthStrategyFactory : IAuthStrategyFactory
{
    readonly IAuthStrategy _authStrategy;

    public AuthStrategy(...)
    {
        //determine the concrete implementation of IAuthStrategy that you need
        //This might be injected as well by passing 
        //in an IAuthStrategy and registering the correct one via unity  at startup.
        _authStrategy = SomeCallToDetermineWhichOne(); 
    }

    public IAuthStrategy GetAuthStrategy() 
    {
        return _authStrategy;
    }
}

这是您现有的 AuthStrategy:

public interface IAuthStrategy
{
    OperationResponse<AuthenticationMechanismDTO> GetAuthenticationMechanism(string userName);
}

public class UserNamePasswordMechanism : IAuthStrategy
{

    private IInstitutionRepository _institutionRepository;

    public UserNamePasswordMechanism(IInstitutionRepository institutionRepository)
    {
        this._institutionRepository = institutionRepository;
    }

    public OperationResponse<AuthenticationMechanismDTO> GetAuthenticationMechanism(string userName)
    {

        throw new NotImplementedException();
    }
}

统一注册工厂:

container.RegisterType<IAuthStrategyFactory, AuthStrategyFactory>();

在你的控制器中:

public class EmployeeController : ApiController
{
    private IAuthStrategy _auth;

    public EmployeeController(IAuthStrategyFactory authFactory)
    {
        this._employeeBL = employeeBL;
        this._auth = authFactory.GetAuthStrategy();
    }
}

实际上我错过了在 AuthStrategyFactory 上实现的 IAuthStrategyFactory,一旦我实现并在工作的统一容器中注册。

谢谢