dependency-injection

Why there's no configuration file at all for dependency injection with Google Guice?

删除回忆录丶 提交于 2020-01-01 08:24:12
问题 I am checking out Google Guice as DI framework but I am a bit puzzled: why there's no configuration file at all? I found a partial explanation on this question but it is still not clear how I would be able to set my component roles (or any other thing I need to use a switch) without a config file. Any help appreciated! 回答1: The configuration is in the code instead of config files, which is a valid decision for many scenarios. Yes, it means that you have to rebuild (possibly just the modules)

What is difference between resource injection and dependency injection (CDI) in Java?

被刻印的时光 ゝ 提交于 2020-01-01 07:31:08
问题 I have been learning Java EE for while and found Java EE provides two types of injection mechanisms Resource Injection Dependency Injection Please guide me to understand the difference between Resource Injection & Dependency Injection. 回答1: Java EE provides injection mechanisms that enable our objects to obtain the references to resources and other dependencies without having to instantiate them directly (explicitly with ‘new’ keyword). We simply declare the needed resources & other

Unity Container - Dependency Injection of Dynamic DbContext into Service

帅比萌擦擦* 提交于 2020-01-01 07:12:32
问题 I'm building a .Net Web API which uses a Service+Repository pattern w/ Entity Framework. Each controller's CRUD actions relay data retrieved by calls to a Service. I have a SomeContext that extends DbContext: public class SomeContext : DbContext { public SomeContext(string connString) : base(connString) { } // DbSets ... } The Service is initialized w/ a constructor that accepts an ISomeContext: public class Service : IService { public Service(ISomeContext ctx) : base(ctx) { _alpha = new

Service lifetimes transient vs scoped vs singleton [closed]

蓝咒 提交于 2020-01-01 06:26:43
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I found an article on https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1 and it explains a asp.net core DI and service lifetime. Article mentions following lifetimes: transient scoped singleton I am trying to find a real world example or at least a better explanation

How do I obtain a new stateful session bean in a servlet thread?

我的梦境 提交于 2020-01-01 05:35:07
问题 I'm experimenting with EJB3 I would like to inject a stateful session bean into a servlet, so that each user that hits the servlet would obtain a new bean. Obviously, I can't let the bean be an instance variable for the servlet, as that will be shared. And apparantly injecting local variables isn't allowed. I can use the new operator to create a bean, but that doesn't seem the right approach. Is there a right way to do this? It seems like what I'm trying to do is fairly straightforward, after

Dependency Injection - When to use property injection

依然范特西╮ 提交于 2020-01-01 05:27:07
问题 I've a class which have a constructor like this: private string _someString; private ObjectA _objectA; private ObjectB _objectB; private Dictionary<Enum, long?> _dictionaryA; private Dictionary<Tuple<Enum,long?>, long?> _dictionaryB; public SomeDiClass(string someString) { _someString = someString; _objectA = new ObjectA(); _objectB = new ObjectB(); _dictionaryA = new Dictionary<Enum, long?>(); _dictionaryB = new Dictionary<Tuple<Enum, long?>, long?>(); } I want to get the dependency creation

Dropwizard and Guice: injecting Environment

狂风中的少年 提交于 2020-01-01 05:04:28
问题 I am currently building a Dropwizard + Guice + Jersey-based application where the database access is being handled by JDBI for the time being. What I am trying to achieve is to have your typical enterprise architecture, where Resources access Service classes accessing a DAO class that in turn accesses the database. It would be nice to get all this wired up in a proper DI way, although I guess I can build my object graph in the run() method of the application if all else fails. So, I'm running

Dropwizard and Guice: injecting Environment

大城市里の小女人 提交于 2020-01-01 05:04:13
问题 I am currently building a Dropwizard + Guice + Jersey-based application where the database access is being handled by JDBI for the time being. What I am trying to achieve is to have your typical enterprise architecture, where Resources access Service classes accessing a DAO class that in turn accesses the database. It would be nice to get all this wired up in a proper DI way, although I guess I can build my object graph in the run() method of the application if all else fails. So, I'm running

Injecting Service in Middleware in ASP.NET Core

做~自己de王妃 提交于 2020-01-01 04:51:08
问题 I want to inject a service based on the HTTP header value. So I have 2 classes - DbDataProvider and InMemDataProvider, both are implemented from IDataProvider. Whenever an API call is made, a header is passed by the client which determines whether DbDataProvider is required or InMemDataProvider is required. How do I achieve that? So in short I need to inject service in the ServiceCollection in one of the middlewares. Is that possible? The problem is that in the ConfigureService method in

Remove a service in ASP.Net Core Dependency Injection [duplicate]

本秂侑毒 提交于 2020-01-01 04:30:08
问题 This question already has answers here : How to remove a default service from the .net core IoC container? (2 answers) Closed 2 years ago . In an Asp.Net MVC Core (early versions, versions 1.0 or 1.1), dependency injection bindings are configured as follow in the Startup.cs class : public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddScoped<IMyService, MyService>(); // ... } } In my applications, I usually have a base Startup class, where generic