【发布时间】:2015-05-27 20:10:14
【问题描述】:
我有一个使用实体框架的 MVC 应用程序。我正在使用存储库、工作单元和统一作为依赖注入。
我的问题是我有不同的身份验证类型,每种类型我想要一个不同的类,所以我决定使用策略模式
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);
}
}
【问题讨论】:
-
好的 - 你需要为你的问题提供一些额外的上下文。您能否向我们展示您如何通过 Unity 注册您的课程,向您的控制器展示您将
IAuthStrategy传递到其中,以及您收到的确切错误消息。 -
报错怎么办?只是说您遇到了错误,而没有详细说明实际错误是什么,这很难提供帮助。
-
这里出现错误:错误 1 类型“HRBL.AuthStrategy”不能用作泛型类型或方法“Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType
(Microsoft.Practices.Unity.IUnityContainer,参数 Microsoft.Practices.Unity.InjectionMember[])'。没有从“HRBL.AuthStrategy”到“HRBL.IAuthStrategy”的隐式引用转换。 C:\Farhan\Angular\SogetiEmployees\SogetiEmployees\App_Start\UnityConfig.cs 49 13 SogetiEmployees -
那是因为你要求unity注册
AuthStrategy为IAuthStrategy,而它没有实现接口。如果您想使用传递给控制器的AuthStrategy实例,然后要求它在运行时使用IAuthStrategy,您将需要另一个接口并注册它。 -
我知道,你能告诉我解决办法吗?
标签: asp.net-mvc entity-framework-6 repository-pattern strategy-pattern