dependency-injection

How to @Inject into existing object hierarchy using Guice?

天大地大妈咪最大 提交于 2019-12-20 09:59:25
问题 I have an existing object hierarchy where some objects have fields that need to be injected. Also there are some other objects that are constructed using Google Guice and need to be injected with references to some objects from previously described object hierarchy. How do I do such kind of injection with Guice? The problem is that objects from existing hierarchy were not constructed using Guice, and therefore are not subject to inject process by default. There is, of course injector

How to extend already defined lists and maps in Spring application context?

▼魔方 西西 提交于 2019-12-20 09:57:29
问题 Imagine a staged application context with different phases. We start with an early phase to define the necessary infrastructure. The xml application contexts are loaded sequentially. The reason to split up these files is an extension/plugin mechanism. Stage 01-default-configuration.xml We prepare and declare the map with id exampleMapping to enhance them later with data. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util

Should Unity be configured in code or configuration file?

走远了吗. 提交于 2019-12-20 09:37:39
问题 Microsoft's Unity dependency injection framework can be configured either through code or through the applications configuration file (app.config). Code example: IUnityContainer container = new UnityContainer() .RegisterType<IInterface, ConcreteImplementation>(); Configuration example: <unity> <containers> <container> <types> <type type="IInterface, MyAssembly" mapTo="ConcreteImplementation, MyAssembly" /> What are the advantages/disadvantages to each approach? I can think of the obvious

Is it possible to inject EJB implementation and not its interface using CDI?

元气小坏坏 提交于 2019-12-20 09:37:32
问题 My configuration is: Wildfly 8.2.0, Weld Is it possible to inject in bean and not in its interface in CDI ? @Stateless class Bean implements IBean { ... } interface IBean { ... } @SessionScoped class Scoped { @Inject Bean bean; //Fail @Inject IBean iBean; //OK } EDIT : More Info in my previous question: Stateless EJB implements interface injection failed 回答1: Yes you can, but as EJB inject the business view the only business view you are exposing is the @Local view which is the default when

Scala: reconciling type classes with dependency injection

元气小坏坏 提交于 2019-12-20 09:01:37
问题 There seems to be a lot of enthusiasm among Scala bloggers lately for the type classes pattern, in which a simple class has functionality added to it by an additional class conforming to some trait or pattern. As a vastly oversimplified example, the simple class: case class Wotsit (value: Int) can be adapted to the Foo trait: trait Foo[T] { def write (t: T): Unit } with the help of this type class: implicit object WotsitIsFoo extends Foo[Wotsit] { def write (wotsit: Wotsit) = println(wotsit

Using Dagger for dependency injection on constructors

夙愿已清 提交于 2019-12-20 08:59:26
问题 So, I'm currently redesigning an Android app of mine to use Dagger. My app is large and complicated, and I recently came across the following scenario: Object A requires a special DebugLogger instance which is a perfect candidate for injection. Instead of passing around the logger I can just inject it through A's constructor. This looks something like this: class A { private DebugLogger logger; @Inject public A(DebugLogger logger) { this.logger = logger; } // Additional methods of A follow,

Which pattern to use for logging? Dependency Injection or Service Locator?

拥有回忆 提交于 2019-12-20 08:56:33
问题 Consider this scenario. I have some business logic that now and then will be required to write to a log. interface ILogger { void Log(string stuff); } interface IDependency { string GetInfo(); } class MyBusinessObject { private IDependency _dependency; public MyBusinessObject(IDependency dependency) { _dependency = dependency; } public string DoSomething(string input) { // Process input var info = _dependency.GetInfo(); var intermediateResult = PerformInterestingStuff(input, info); if

Injecting multiple implementations with Dependency injection

安稳与你 提交于 2019-12-20 08:55:39
问题 I'm currently working on a ASP.NET Core Project and want to use the built-in Dependency Injection (DI) functionality. Well, I started with an interface: ICar { string Drive(); } and want to implement the ICar interface multiple times like public class BMW : ICar { public string Drive(){...}; } public class Jaguar : ICar { public string Drive(){...}; } and add the following in the Startup class public void ConfigureServices(IServiceCollection services) { // Add framework services. services

Has anyone used ServiceLoader together with Guice?

放肆的年华 提交于 2019-12-20 08:51:19
问题 I keep wanting to try this on a larger scale with our app + build system, but higher priorities keep pushing it to the back burner. It seems like a nice way to load Guice modules and avoids the common complaint about "hard coded configuration". Individual configuration properties rarely change on their own, but you will almost always have a set of profiles, usually for different environments (Debug, Production, etc). ServiceLoader lets you pull a list of all implementations defined as a

Using Reader Monad for Dependency Injection

杀马特。学长 韩版系。学妹 提交于 2019-12-20 08:40:45
问题 I recently saw the talks Dead-Simple Dependency Injection and Dependency Injection Without the Gymnastics about DI with Monads and was impressed. I tried to apply it on a simple problem, but failed as soon as it got non-trivial. I really would like to see a running version of dependency injection where a class that depends on more than one value that has to be injected a class that depends on a class that depends on something to be injected as in the following example trait FlyBehaviour { def