我发现了一些与我发布的问题类似的问题,但我并没有从他们那里得到我真正需要的东西.
我仍在努力使用Microsoft Unity DI来实现我的CustomMembershipProvider.
我仍在努力使用Microsoft Unity DI来实现我的CustomMembershipProvider.
定制会员:
public class CustomMembershipProviderService : MembershipProvider { private readonly IUserService _userService; public CustomMembershipProviderService(IUserService userService) { this._userService = userService; } public override string ApplicationName { ...
用户服务:
public class UserService : IUserService { private readonly IUserRepository _repository; private readonly IUnitOfWork _unitOfWork; public UserService(IUserRepository repository,IUnitOfWork unitOfWork) { this._repository = repository; this._unitOfWork = unitOfWork; } ...
的AccountController:
public class AccountController : Controller { // next line is what I don't feel too sure about what to do? // shouldn't my controller constructor use an Interface as its property? private readonly CustomMembershipProviderService _customMembershipProviderService; public AccountController(CustomMembershipProviderService customMembershipProviderService) { this._customMembershipProviderService = customMembershipProviderService; } ...
如何为MembershipProvider类创建接口?
我试过了:
public interface ICustomMembershipProvider : MembershipProvider
但我觉得这不行,所以我卡住了,不知道如何使用我的存储库,UoW,服务和Unity DI实现MembershipProvider
解决方法
回答第二个问题
MembershipProvider和RoleProvider的问题在于它们不是由容器构建的,而是由框架构建的. AFAIK设置活动成员资格提供程序的唯一方法是web.config文件.
因此,如果您想通过容器使用容器来解析存储库,服务等依赖关系,那么您必须使用良好的旧(我开玩笑)服务定位器模式.
public class CustomMebershipProvider : ICustomMebershipProvider { public bool ValidateUser(string user,string pwd) { var svc = Global.Container.Resolve<IMyService>(); /* ... */ } }
对第一个问题
您仍然可以使用Constructor Injection在控制器中注入活动成员资格提供程序.
首先,使用您需要的所有方法创建一个界面.这里的问题是MembershipProvider是一个具体的类,所以你不能从它继承接口.只需创建新界面,从MembershipProvider复制所需功能的方法签名:
public interface ICustomMembershipProvider { bool MyMethod(); /* From MembershipProvider */ bool ValidateUser(string username,string password); /* ... */ }
然后让你的提供者实现它并注入:
Container .RegisterType<ICustomMembershipProvider>( new InjectionFactory(c => (ICustomMembershipProvider) Membership.Provider))