dependency-injection

Inverse of @DependsOn annotation

不打扰是莪最后的温柔 提交于 2019-12-12 14:12:46
问题 Spring (and presumably other DI containers, but I'm using Spring) recognises the @DependsOn annotation. You use this to identify any other beans that must be initiated BEFORE this bean, eg @Component @DependsOn({"initiatedFirst", "initiatedSecond"}) public class InitiatedThird { //... Is there an analogous annotation that means that the supplied beans must be initiated AFTER the annotated bean? For example @Component @DependencyOf({"initiatedSecond", "initiatedThird"}) public class

Ninject Binding by Convention not working with generics types

纵饮孤独 提交于 2019-12-12 14:11:52
问题 I am using .NET 4.5, Ninject 3 with the binding by convention lib as follow: kernel.Bind(x => x .FromAssembliesMatching("assembly.dll") .SelectAllClasses().InheritedFrom(typeof(ICommandHandler<>)) .BindAllInterfaces()); And this is binding properly for: public class MyCommandHandler : ICommandHandler<MyCommand> But doesn't bind: public class MyGenericCommandHandler<T> : ICommandHandler<MyGenericCommand<T>> However, the previous binding works if I add individual bindings for specific

Adding user class to all presenters

眉间皱痕 提交于 2019-12-12 14:11:49
问题 I have a hazy understanding of GIN, but have it working for injecting presenters, etc. I'm trying to inject a self-made "User" class into all my presenters in order to get the currently logged-in user. I've added @Inject to the constructor on my User class, and added User to my GIN module ... but apart from that, I'm totally lost. Do I bind it to my app presenter (tried that, but I get an error since User doesn't extend my AppPresenter)? As a singleton? Is this even the right way to get pass

How can Guice's @RequestScoped be done with Play 2.6

六眼飞鱼酱① 提交于 2019-12-12 14:04:23
问题 I need the authenticated User, to access REST services. My idea is to have an IdentityService that is RequestScoped and can be injected by all REST service clients. The setup is like: Controller -> BusinessLogic -> RESTServiceClient So to passing the User down would involve lots of code. As Guice's @RequestScoped is not supported (https://groups.google.com/forum/#!msg/play-framework/MEOAhdOJz5Q/-cukqS-ZBAAJ) How can this be done with Play? The Problem is also described here (without a

What to pass via constructor and what to pass via interface?

半世苍凉 提交于 2019-12-12 13:26:04
问题 This is a question about dependency injection. When constructing a service object, we pass in collaborators via the constructor in the construction phase. The service object would implement an interface, and that would be called during the run-phase. Sometimes is is difficult to know if a particular object should be passed via the constructor or be part of the interface implemented by the service class? Are there any rules about choosing one option over the other? The question is most

Instantiating an object in a worker thread with Dependency Injection

扶醉桌前 提交于 2019-12-12 13:14:54
问题 My objective is to run a never ending process in a parallel thread. The problem is, I cannot just instantiate my worker service in the new Thread, because I am using DI in my application. Based on my research here on SO, I have noticed many people are suggesting that Abstract Factories need to be injected into the thread to dynamically instantiate a thread-safe object in a parallel thread. 1, 2 /// <summary> /// Responsible for starting parallel long running worker threads /// </summary>

Springboot: How can I perform an integration test with real dependencies?

会有一股神秘感。 提交于 2019-12-12 13:02:09
问题 I'm starting to learn both Java and Spring boot now, and I'm having some problems with dependency injection in integration tests. I have a class under src/main/java/com/rfd/domain/services called TransactionService, which is marked as @Service and which has another dependencies, one of them a repository created by Spring boot. When I launch the application, it is launched correctly so I assume the dependencies are being resolved correctly. This is the summarized class: package com.rfd.domain

Spring injection bind toInstance

故事扮演 提交于 2019-12-12 12:42:01
问题 Is there a way to bind an injected object to a specific instance using Spring DI similar to Google Guice's bind(MyClass.class).toInstance(myclassobject); 回答1: If the constructor or member variable is annotated with @Autowired , Spring will try to find a bean that matches the type of the Object. You can get similar functionality to the annotation using @Qualifier , for example: bind(MyClass.class).annotatedWith(Names.named("main")).toInstance(myclassobject); would become in Spring: @Autowired

DI and repository pattern

为君一笑 提交于 2019-12-12 12:26:17
问题 Currently, my code is similar to this (shortened just to make a point): DAL Repository Interface public interface IRepository<TEntity, in TKey> { IList<TEntity> GetAll(); TEntity Get(TKey id); TEntity Add(TEntity item); TEntity Update(TEntity item); bool Remove(TKey id); } Base EF repository public class BaseEFRepository<TEntity, TKey> : IRepository<TEntity, TKey> where TEntity: class, IEntity<TKey> where TKey: struct { protected readonly DbContext _dbContext; public BaseRepository() {

Dependency inversion principle and composition

可紊 提交于 2019-12-12 12:11:29
问题 I am reading about SOLID principles and I stopped here on " Dependency inversion principle" which means the objects should passed already instantiated to anther object, which means composition cannot be applied with Dependency inversion principle am right? or there is something I miss? UPDATE ************************************************** suppose you have a class and this class has an attribute which is reference to anther object, we have 2 solution(for me): Create the object outside the