简体   繁体   English

ASP.NET Web Api 2中每个请求的条件依赖性解析

[英]Conditional dependency resolving per request in ASP.NET Web Api 2

I'm currently working on a Rest API using asp.net Web Api (5.0.0-rc1). 我目前正在使用asp.net Web Api(5.0.0-rc1)开发Rest API。 By now we don't use any DI container, so it's Poor Man's DI. 现在我们不使用任何DI容器,所以它是穷人的DI。 But the plan is to use one or another soon. 但计划是尽快使用其中一种。

In order to resolve our dependencies we use our own implementation of IHttpControllerActivator similar to how it is described in Mark Seemann's blog . 为了解决我们的依赖关系,我们使用自己的IHttpControllerActivator实现,类似于Mark Seemann博客中的描述。

The problem 问题

All our controllers use services in order to perform their actions. 我们所有的控制器都使用服务来执行他们的操作。 We're also trying to reuse controllers for different roles. 我们还尝试将控制器重用于不同的角色。 Fe a controller that lists all users has the HttpGet attribute 列出所有用户的控制器具有HttpGet属性

[HttpGet("api/role/{role}/users")]

So depending on the role in the uri (could be fe admin or department ) we want to resolve the controller's dependencies differently. 因此,根据uri中的角色(可能是fe admindepartment ),我们希望以不同方式解析控制器的依赖关系。 This could mean, that we want to wrap a service with a decorator or we need to inject a some kind of strategy`into the service depending on the role. 这可能意味着,我们想要用装饰器包装服务,或者我们需要根据角色在服务中注入某种策略。

Note: The conditional resolving of a dependency could be deep down in the dependency graph. 注意:依赖关系的条件解析可能在依赖关系图中很深。

The question 这个问题

Since I'm quite new to dependency injection I'm really unsure what's the standard way to resolve conditional dependencies. 由于我对依赖注入很陌生,所以我真的不确定解决条件依赖关系的标准方法是什么。 Is dependency injection meant to be conditional on a per request base or is the dependency graph rather fixed and we should use factories in this case? 依赖注入是指以每个请求为基础还是依赖图是相当固定的,我们应该在这种情况下使用工厂? Fe a controller gets ISomeServiceFactory instead of a ISomeService and the factory itself gets the role injected. 控制器获取ISomeServiceFactory而不是ISomeService ,工厂本身会获得注入的角色。

I also took a look at Castle Windsor's Typed Factory Facility , but I'm not sure if that would solve our problem. 我还看了一下Castle Windsor的Typed Factory Facility ,但我不确定这是否能解决我们的问题。

There are various ways you translate a run-time value to one among several available dependencies (and entire sub-graphs). 有多种方法可以将运行时值转换为几个可用依赖项(以及整个子图)中的一个。 Instead of relying on factories, I rather prefer selecting among available dependencies. 我宁愿选择可用的依赖项,而不是依赖工厂。

So far, I've identified at least three different ways of doing this: 到目前为止,我已经确定了至少三种不同的方法:

Of these, I personally prefer Partial Type Name , because it's the most loosely coupled. 其中,我个人更喜欢Partial Type Name ,因为它是最松散耦合的。

All these selection strategies rely on all of the strategy instances being available before selection. 所有这些选择策略都依赖于选择之前可用的所有策略实例。 Often, that's not a problem, because if they're thread-safe, a single instance of each can be reused across all HTTP requests. 通常,这不是问题,因为如果它们是线程安全的,则可以在所有HTTP请求中重用每个实例。

However, even if one or more of the services aren't thread-safe, there are ways to deal with potential performance issues - most notably using the Virtual Proxy pattern. 但是,即使一个或多个服务不是线程安全的,也有办法处理潜在的性能问题 - 最明显的是使用虚拟代理模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM