dependency-injection

Use DbContext in ASP .Net Singleton Injected Class

时光怂恿深爱的人放手 提交于 2020-01-08 21:02:21
问题 I need to access my database in a Singleton class instantiated in my Startup class. It seems that injecting it directly results in a DbContext that is disposed. I get the following error: Cannot access a disposed object. Object name: 'MyDbContext'. My question is twofold: Why doesn't this work and how can I access my database in a singleton class instance? Here is my ConfigureServices method in my Startup class: public void ConfigureServices(IServiceCollection services) { // code removed for

Use DbContext in ASP .Net Singleton Injected Class

蹲街弑〆低调 提交于 2020-01-08 21:01:46
问题 I need to access my database in a Singleton class instantiated in my Startup class. It seems that injecting it directly results in a DbContext that is disposed. I get the following error: Cannot access a disposed object. Object name: 'MyDbContext'. My question is twofold: Why doesn't this work and how can I access my database in a singleton class instance? Here is my ConfigureServices method in my Startup class: public void ConfigureServices(IServiceCollection services) { // code removed for

Global DI container in WPF

一笑奈何 提交于 2020-01-07 06:38:12
问题 I find that in WPF it is often not possible to use a DI container in the composite root only. I set up all dependencies in the root but I find the need to instantiate view models that simply cannot be instantiated in the root. Take custom controls for example that live in tabs or get spawned in new windows. Their view models need to be instantiated by the view itself when it gets active. Many of my view models need a dialog provider which I inject as an interface into the VM constructor. I am

What's the point of dependency injection if you still have to pass in an argument?

痞子三分冷 提交于 2020-01-07 06:26:21
问题 I'm having a bit of trouble understanding the basic idea of dependency injection. (I'm using Play 2.5 with the play-slick module) Say I have a class Users that needs a database connection. package models @Singleton class Users @Inject() (dbConfigProvider: DatabaseConfigProvider) { private val db = dbConfigProvider.get[JdbcProfile].db private val users = TableQuery[UserTable] private val setupAction = DBIO.seq(users.schema.create) private val setupFuture: Future[Unit] = db.run(setupAction) def

How do I inject a buffered reader into a class with a file reader as its parameter using Spring boot?

早过忘川 提交于 2020-01-07 03:25:09
问题 I have this spring boot application in which I have the below line inside a method. BufferedReader reader = new BufferedReader(new FileReader("somePath")); How can I inject this into my code so I can mock it for my unit tests? Using guice I could use a provider. But how can I achieve this using spring boot? Any help would be much appreciated. 回答1: If you want to mock the class which have the below line inside a method. BufferedReader reader = new BufferedReader(new FileReader("somePath"));

Initialization of bean failed; ConversionNotSupportedException

南楼画角 提交于 2020-01-06 21:58:28
问题 I have a higher hierarchy context, which on server startup (tomcat) gets the following bean: <bean id="org.sakaiproject.rubrics.api.rubric.RubricsService" class="org.sakaiproject.rubrics.impl.RubricsServiceImpl" init-method="init" singleton="true"> <property name="rubricsLogic" ref="org.sakaiproject.rubrics.logic.RURubricLogic" /> <property name="externalLogic" ref="org.sakaiproject.rubrics.api.rubric.ExternalLogic" /> </bean> That bean's class ('RubricsServiceImpl'), implements an API

What is the correct way to structure this generic object creation

白昼怎懂夜的黑 提交于 2020-01-06 19:45:35
问题 Ignoring the fact that this uses the Aurelius Framework, this question is more about how I need to re-tweak the code to make the generic constructor injection work for both type: < string > and < TCustomConnection > Also ignore the fact that the child objects are in the same unit, I would generally put them in their own, but this just makes it easier to post in a question. I'm trying use the Factory Method pattern to determine what type of connection it should be making at runtime depending

Spring dependency injection not completing in time

故事扮演 提交于 2020-01-06 19:26:39
问题 All, I'm setting up a Spring IOC container using dependency injection, etc. I'm running into a very bizarre behavior where it seems that some beans in a collection get fully initialized before use while others don't. The weird part is that all of these beans share the same superclass and the failed dependency injection is happening in one of the superclass dependencies. Here is the example: @Component class Bar { void doSomething() { // do something } } class Foo { @Autowired Bar bar }

How can I add a Dependency that can be used as type parameter 'TImpl' for Castle Windsor?

僤鯓⒐⒋嵵緔 提交于 2020-01-06 15:32:51
问题 I was getting this runtime exception with a particular URL: " Missing dependency. Component NRBQ.Web.Controllers.DeliveryController has a dependency on SeaStore.Data.Legacy.Interfaces.INRBQDeliveryRepository, which could not be resolved. Make sure the dependency is correctly registered in the container as a service, or provided as inline argument." ExceptionType: "Castle.MicroKernel.Resolvers.DependencyResolverException " ...so I added this code (based on existing code that works) to the IOC

Dagger 1.x @Inject throws an IllegalArgumentException

早过忘川 提交于 2020-01-06 14:13:25
问题 I want to replace our component registry (with dexfile class loader magic) with an dependency injection framework for Android. The first try is dagger. When trying I get the following error: 11-06 13:05:41.040 16269-16269/com.daggertoolkitexample E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{daggertoolkitexample/com.dagger.MyActivity}: java.lang.IllegalArgumentException: No inject registered for members/com.dagger.MyActivity. You