dependency-injection

Dependency Injection on Authorization Policy

久未见 提交于 2020-06-15 21:36:09
问题 I want to create a claim based authorization for my ASP.NET Core app: public void ConfigureServices(IServiceCollection services) { services.AddAuthorization(options => { options.AddPolicy("Founders", policy => policy.RequireClaim("EmployeeNumber", "1", "2", "3", "4", "5")); }); } The problem is that I have a non trivial method to resolve the employee numbers (1 to 5) and I want to use a DI service: public interface IEmployeeProvider { string[] GetAuthorizedEmployeeIds(); } I would like to

How should I extend Injectable from another Injectable with many Injections in angular2?

爱⌒轻易说出口 提交于 2020-06-14 04:09:31
问题 Is it possible to do something like this? (cause I tried, and haven't succeed): @Injectable() class A { constructor(private http: Http){ // <-- Injection in root class } foo(){ this.http.get()... }; } @Injectable() class B extends A{ bar() { this.foo(); } } 回答1: Kind of - you have to make a super call to the constructor of your base class. Just pass down the needed dependencies: @Injectable() class A { constructor(private http: Http){ // <-- Injection in root class } foo(){ this.http.get()...

Dependency injection (ninject) using strings, anti-pattern?

柔情痞子 提交于 2020-06-13 19:05:13
问题 I have some code that is using ninject to inject dependencies, these dependencies are actual strings. Is this an anti-pattern to inject strings rather than creating a new object for example. I.e. I wanted to inject Username and Password, would it actually be better to create a small class called credentials with 2 properies of Usernamd and Password and inject this ? Injecting strings into constructors can be done via kernel.Bind<IUser>().To<User>() .WithConstructorArgument(@"username",

MVC5 Web API and Dependency Injection

天涯浪子 提交于 2020-06-12 07:15:11
问题 Trying to do some DI on Web API 2 without third-party tools. So, from some examples I've got custom dependency resolver (why there's no integrated one? Strange, even Microsoft.Extensions.DependencyInjection provides nothing): public class DependencyResolver : IDependencyResolver { protected IServiceProvider _serviceProvider; public DependencyResolver(IServiceProvider serviceProvider) { this._serviceProvider = serviceProvider; } public IDependencyScope BeginScope() { return this; } public void

MVC5 Web API and Dependency Injection

倾然丶 夕夏残阳落幕 提交于 2020-06-12 07:14:48
问题 Trying to do some DI on Web API 2 without third-party tools. So, from some examples I've got custom dependency resolver (why there's no integrated one? Strange, even Microsoft.Extensions.DependencyInjection provides nothing): public class DependencyResolver : IDependencyResolver { protected IServiceProvider _serviceProvider; public DependencyResolver(IServiceProvider serviceProvider) { this._serviceProvider = serviceProvider; } public IDependencyScope BeginScope() { return this; } public void

Angular 5 Injector - How to inject string

狂风中的少年 提交于 2020-06-12 06:47:52
问题 I am trying to inject strings into my angular component. The code below works fine but it gives a deprecation warning: get is deprecated: from v4.0.0 use Type or InjectionToken @Component({ selector: 'app-label', templateUrl: './label.component.html', styleUrls: ['./label.component.scss'] }) export class LabelComponent { public name: string; public caption: string; constructor( private injector: Injector ) { this.name = this.injector.get('name'); this.caption = this.injector.get('caption'); }

How to mock service with symfony 4 in functional tests?

…衆ロ難τιáo~ 提交于 2020-06-10 14:54:54
问题 I have an commandbus handler, which injects some service: class SomeHandler { private $service; public function __construct(SomeService $service) { $this->service = $service; } public test(CommandTest $command) { $this->service->doSomeStuff(); } } SomeService has method doSomeStuff with external calls, which I want not to use during testing. class SomeService { private $someBindedVariable; public function __construct($someBindedVariable) { $this->someBindedVariable = $someBindedVariable; }

Use dependency injection in static class

丶灬走出姿态 提交于 2020-06-08 04:52:07
问题 I need to use Dependency Injection in a static class. the method in the static class needs the value of an injected dependency. The following code sample demonstrates my problem: public static class XHelper { public static TResponse Execute(string metodo, TRequest request) { // How do I retrieve the IConfiguracion dependency here? IConfiguracion x = ...; // The dependency gives access to the value I need string y = x.apiUrl; return xxx; } } 回答1: You basically have two options: Change the

How to avoid Google guice injector getInstance() repetative calls due to null?

♀尐吖头ヾ 提交于 2020-06-01 05:14:29
问题 I have recently started using Google guice DI framework and am quite new to it. I am facing below problem - @inject returns null always and have to call injector to inject various references. This is my AbstractModule class public class AppModule extends AbstractModule { private static final Injector injector = Guice.createInjector(new AppModule()); private final Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions( new DropwizardMetricsOptions().setEnabled(true) )); private final

How to avoid Google guice injector getInstance() repetative calls due to null?

那年仲夏 提交于 2020-06-01 05:14:26
问题 I have recently started using Google guice DI framework and am quite new to it. I am facing below problem - @inject returns null always and have to call injector to inject various references. This is my AbstractModule class public class AppModule extends AbstractModule { private static final Injector injector = Guice.createInjector(new AppModule()); private final Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions( new DropwizardMetricsOptions().setEnabled(true) )); private final