自定义会员资格提供程序中的 Unity 依赖项注入
我有 ASP.NET MVC3 项目,我想在其中使用自定义成员资格提供程序。我还想使用 Unity 来解决我的依赖注入。
这是来自 Global.asax 的代码:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
var container = new UnityContainer();
container.RegisterType<IAuthentification, Authentification>();
container.RegisterType<IRepository, Repository>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
这是来自我的会员提供程序的代码:
public class CustomMembershipProvider : MembershipProvider
{
[Dependency]
private IProveaRepository Repository { get; set; }
public override bool ValidateUser(string username, string password)
{
.....
}
问题是当我将断点放置到 ValidateUser 方法时,我看到 Repository 属性未初始化。但这种结构:
[Dependency]
private IProveaRepository Repository { get; set; }
例如,在控制器中工作得很好。
有谁知道为什么会这样以及该怎么办?
I have ASP.NET MVC3 project where I want to use custom membership provider. Also I want to use Unity for resolving my dependency injection.
this is code from Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
var container = new UnityContainer();
container.RegisterType<IAuthentification, Authentification>();
container.RegisterType<IRepository, Repository>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
this is code from my membership provider:
public class CustomMembershipProvider : MembershipProvider
{
[Dependency]
private IProveaRepository Repository { get; set; }
public override bool ValidateUser(string username, string password)
{
.....
}
Problem is when I put breakpoint to ValidateUser method I see that Repository property not initialized. But this construction:
[Dependency]
private IProveaRepository Repository { get; set; }
for example, works fine in controllers.
Does anybody know why it is so and what to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
过去几天我遇到了同样的问题。我最终得到了以下解决方案(类型和字段名称更改为与您的匹配)。
因此,即使 Unity 无法控制
CustomMembershipProvider
的构建,无参数构造函数也会让 Unity 参与(通过 MVC3DependencyResolver
)来提供正确的存储库实例。如果您要对
CustomMembershipProvider
进行单元测试,那么您可以直接使用 Unity 构建一个实例,它将使用第二个构造函数并避免调用DependencyResolver
。I had the same problem over the last couple of days. I ended up with the following solution (type and field names changed to match yours).
So even though Unity is not in control of the building of the
CustomMembershipProvider
, the parameterless constructor gets Unity involed (via the MVC3DependencyResolver
) to supply the correct repository instance.If you're unit testing the
CustomMembershipProvider
then you can just build an instance with Unity directly, which will use the second constructor and avoid the call toDependencyResolver
.Unity 无法将 IProveaRepository 实例注入您的自定义会员资格提供程序,因为:
如果您在代码中使用会员资格 priovider 类,您可以执行以下操作:
尝试将您的 customMembershipProvider 包装在抽象中,例如 IMembershipProvider 仅具有您使用的方法的签名。结果是这样的:
然后你可以统一注册它:
那么约束就是像这样传递控制器中的依赖项:
但是最好不要使用属性注入,而是像这样使用构造函数注入:
就是这样我理解并且会这么做。但也许有更好的方法,或者我忽略了一些有助于更轻松地实现它的 Unity API。
无论如何我希望它有帮助。
Unity cannot inject IProveaRepository instance into you custom membership provider because :
If you're using your membership priovider class in your code you could do the following :
Try to wrapp your customMembershipProvider in an abstraction for example IMembershipProvider that has only signature for methods that you use. The result is like that :
Then you could register it in unity :
Then the constraint is to pass the dependency in your controller like that :
But it would be event better to not user the property injection but the constructor injection like that :
It's the way I understand it and would do it. But maybe there is a better approach or I'm ignoring some of Unity API that would help to achieve it easier.
Anyway I hope it helps.
正如其他人所说,Unity 无法在提供程序中注入依赖项,因为它们是未知的
到容器,即使可能是提供程序的注册,您也没有通过容器构建提供程序的“工厂点”,有一个不违反良好设计原则的解决方案。 (这是因为,即使大多数人忽略这一点,使用 ServiceFactory 也太接近反模式了......)
但是,一个好的解决方案可能是将
[Dependency]
属性与UnityBuildUp
方法。因此,以您的示例为例,为了获得您想要执行的操作,请保留所有内容不变,然后将 BuildUp 调用放入提供程序构造函数中,
我希望它有所帮助。
While as others said Unity cannot inject dependencies in providers because they're not known
to the container and, even if could be a registration of a provider, you haven't a "factory point" where building the provider through the container, there's a solution which doesn't violate good design principles. (This because, even if most people ignore this, using a ServiceFactory is too close to an antipattern...)
But, a good solution could be the association of using the
[Dependency]
attribute in conjunction with the UnityBuildUp
method.So taking your example, to get what you're trying to do, leave all the things as they are, and put in the provider constructor the BuildUp call
I hope it helps.