dependency-injection

How does @Inject in Scala work

余生颓废 提交于 2019-12-14 04:17:14
问题 I'm wondering how does @Inject annotation in Play-Scala works. It obviously injects a dependency, but I'm curious how is it working. When I was using it on class extending controller and set routes generator to injectroutesgenerator it seems to autmagically create objects from those classes, but how do I use it in other context? I tried: @Inject val mailer: MailerClient = null But that doesn't seem to work. Are there any posibilities to @Inject things (that mailerClient, WS ets.) directly to

Is it “bad practice” to pass argument to guice module

谁都会走 提交于 2019-12-14 03:59:53
问题 Checking out Guice and I love it. I currently have problem where guice solved it by injecting all the required dependencies I need. But I wonder if I am using Guice in the wrong way. What I require though is define bindings depending on specific instance. And to achieve this I passed the instance in the module. For instance, consider the following (somewhat similar to my problem): public class CustomerModule extends AbstractModule { private Customer customer; public CustomerModule(Customer

Dependency Injection to resolve dependency with runtime data

坚强是说给别人听的谎言 提交于 2019-12-14 03:58:34
问题 I am using simple injector for my web api project. I have a service which requires a session token in order for it to instantiate. public class CustomerService { public CustomerService(Auth auth, IRepositoryFactory repositoryFactory) { // make post call to another web api for validation SomeWebApiCallToValidateAuth.vaildate(auth); } } So for this service, it requires an auth token and a repositoryFactory. I want it to be able to inject the auth parameter (which comes from the http web request

Unity Container - Lazy injection

混江龙づ霸主 提交于 2019-12-14 03:57:10
问题 Lets say i have a clas class Foo : FooBase { public Foo(Settings settings, IDbRepository db) : base(settings) { this.db = db; } ... } Basically FooBase receives settings via constructor and loads some config from configuration file. Then i have the class MySQLRepository which implements IDbRepository class MySQLRepository : IDbRepository { ... public MySQLRepository(IConfigurationRepository config) { conn = new MySQLConnection(config.GetConnectionString()); } ... } in Program.cs i have: Foo

Ninject Contextual Binding on MVC Request

狂风中的少年 提交于 2019-12-14 03:49:12
问题 I have an unusual situation injecting a service into an ASP.NET MVC Controller. The Controller provides a single action to render a side-bar menu on the page, and the service injected into the Controller is a factory to create the side-bar content. The action is decorated with the [ChildActionOnly] attribute: the side bar can only be rendered when rendering another action. The difficulty comes in that I want to inject different instances of the side-bar factory abstraction according to the

Need complete DI sample for WCF

落爺英雄遲暮 提交于 2019-12-14 03:44:11
问题 Does anyone have a complete and working DI sample for WCF? Every sample I find just gets me more confused. Does anyone know of a complete and working standalone simple sample that works with the built in stuff? Maybe once I get a handle of the built in stuff, I can move on to different DI frameworks such as StructureMap or Unity with WCF. My MVC project is currently using Unity for all its DI. 回答1: The code download for my book Dependency Injection in .NET contains a full, working example.

Guice: differences between Singleton.class and @Singleton

北慕城南 提交于 2019-12-14 03:40:24
问题 In Guice, what's the difference between: // Inside your AbstractModule subclass: @Override public void configure() { bind(Service.class).to(ServiceImpl.class).in(Singleton.class); } And: @Override public void configure() { bind(Service.class).to(ServiceImpl.class); } @Provides @Singleton public ServiceImpl providesService() { return new ServiceImpl(); } Are they both the same? When would you use one versus the other? Thanks in advance. 回答1: They are nearly identical. The @Singleton syntax is

StructureMap resolve dependency through injection instead of service location

大城市里の小女人 提交于 2019-12-14 03:39:42
问题 In my project I register many ISerializers implementations with the assembly scanner. FWIW this is the code that registers my ISerializers Scan(scanner => { scanner.AssemblyContainingType<ISerializer>(); scanner.AddAllTypesOf<ISerializer>().NameBy(type => type.Name); scanner.WithDefaultConventions(); }); Which then correctly registers ISerializer (...ISerializer) Scoped as: Transient JsonSerializer Configured Instance of ...JsonSerializer BsonSerializer Configured Instance of ..

Symfony 2 functional tests with mocked services

给你一囗甜甜゛ 提交于 2019-12-14 03:39:34
问题 I have a controller I'd like to create functional tests for. This controller makes HTTP requests to an external API via a MyApiClient class. I need to mock out this MyApiClient class, so I can test how my controller responds for given responses (e.g. what will it do if the MyApiClient class returns a 500 response). I have no problems creating a mocked version of the MyApiClient class via the standard PHPUnit mockbuilder: The problem I'm having is getting my controller to use this object for

Dependency injection into scala objects (not classes)

牧云@^-^@ 提交于 2019-12-14 03:26:57
问题 I have an import "import play.api.libs.ws.WSClient" which i want to use within my object Object X { ... } But this doesn't seem to be available inside my object. I see that dependency injection is only available for classes. How do i get this to work? 回答1: Injecting a dependency into an object is impossible. You have two options: Ugly and deprecated: Access the injector via the global application: val wsClient = Play.current.injector.instanceOf[WSClient] Way to go if your code needs to live