dependency-injection

How does Guice Populate Annotated Fields

久未见 提交于 2019-12-23 11:54:36
问题 For the sake of my own education, I wanted to build a simple Dependency Injection framework that functions similar to the way Google's Guice does. So that when a class is loaded, it pre-populates annotated fields with data from a factory class. I am using Reflections to scan all my factory classes at compile time and save those classes in a static list so that when it comes time to load my classes, I have a reference to my factories that I can then scan methods and return the appropriate data

Symfony 3 - Outsourcing Controller Code into Service Layer

社会主义新天地 提交于 2019-12-23 11:09:38
问题 I am very new in Symfony 3 and I want to avoid the business logic in my controllers. What I have done so far is this: <?php namespace RestBundle\Controller; use RestBundle\Entity\Attribute; use RestBundle\Entity\DistributorProduct; use RestBundle\Entity\AttributeValue; use RestBundle\Entity\ProductToImage; use Symfony\Component\HttpFoundation\Request; use RestBundle\Entity\Product; use FOS\RestBundle\Controller\FOSRestController; /** * Product controller. * */ class ProductController extends

@Inject and @PostConstruct not working in singleton pattern

你离开我真会死。 提交于 2019-12-23 10:18:13
问题 I have a class as below: public class UserAuthenticator { private static UserAuthenticator authenticator = @Inject private UserRepository userRepository; @PostConstruct public void init() { List<User> allUsers = userRepository.findAll(); for (User user : allUsers) { users.put(user.getEmail(), user.getPassword()); serviceKeys.put(user.getServiceKey(), user.getEmail()); } } public static UserAuthenticator getInstance() { if (authenticator == null) { authenticator = new UserAuthenticator(); }

Replace Spring.Net IoC with another Container (e.g. Ninject)

蹲街弑〆低调 提交于 2019-12-23 10:10:23
问题 I'm curious to know if it's possible to replace Spring.Net's built-in IoC container with Ninject. We use Ninject on my team for IoC in our other projects so I would like to continue using that container if possible. Is this possible? Has anyone written a Ninject-Spring.Net Adapter?? Edit I like many parts of the Spring.Net package (the data access, transactions, etc.) but I don't really like the dependency injection container. I would like to replace that with Ninject Thanks 回答1: Jeffrey, can

Dependency Injection across assemblies & namespaces

杀马特。学长 韩版系。学妹 提交于 2019-12-23 10:10:17
问题 I am working on a DI problem that I think I understand the cause of, but I need some suggestions to work around. I have built a stand alone assembly that talks to Sql (call this assembly a), and another assembly that contains business logic (call this assembly b). I created an interface for the db class in the b assembly. Since the interface isn't part of the db assembly, I don't need any references to the db project, and I can load a reference to the db assembly or a stub if I want to run

Can Windsor cooperate with another IoC container?

江枫思渺然 提交于 2019-12-23 09:36:26
问题 In the core of our application, we use Castle Windsor to manage our dependencies. We'll be loading plugins from third parties which may be using their own IoC containers. We'd like them to be able to receive dependencies from the core, e.g. through constructor injection of the core's services, but also receive dependencies from their own IoC container. It seems like if they supplied a service provider interface, Windsor could use it to resolve unknown dependencies, ignoring the results (since

container.RegisterWebApiControllers(GlobalConfiguration.Configuration) causes InvalidOperationException

微笑、不失礼 提交于 2019-12-23 09:04:02
问题 In my integration tests I'm using the same SimpleInjector.Container which I construct in the Web API project I'm testing. But this line in composition root class: container.RegisterWebApiControllers(GlobalConfiguration.Configuration); causes an exception: System.TypeInitializationException : The type initializer for 'MyProject.Api.Test.Integration.HttpClientFactory' threw an exception. ---- System.InvalidOperationException : This method cannot be called during the application's pre-start

WELD-001408 Unsatisfied dependencies when injecting EntityManager

半城伤御伤魂 提交于 2019-12-23 08:53:19
问题 I have @Stateless bean which implements two interfaces (remote and local). I have also added @LocalBean anotation for accessing bean with a no-interface view. @Stateless @LocalBean public class WeatherDataBean implements WeatherDataBeanRemote, WeatherDataBeanLocal { @Inject private EntityManager entityManager; public WeatherDataBean () { } // ....attributes, getter & setter methods .... } I use @Inject for this reason taken from this example of JBoss AS7 quickstart: We use the "resource

WELD-001408 Unsatisfied dependencies when injecting EntityManager

孤者浪人 提交于 2019-12-23 08:52:20
问题 I have @Stateless bean which implements two interfaces (remote and local). I have also added @LocalBean anotation for accessing bean with a no-interface view. @Stateless @LocalBean public class WeatherDataBean implements WeatherDataBeanRemote, WeatherDataBeanLocal { @Inject private EntityManager entityManager; public WeatherDataBean () { } // ....attributes, getter & setter methods .... } I use @Inject for this reason taken from this example of JBoss AS7 quickstart: We use the "resource

How do I pass a dependency to a Serilog Enricher?

与世无争的帅哥 提交于 2019-12-23 08:50:03
问题 I'm using Serilog in my application for logging. When I'm configuring the logger, I have code like this: var log = new LoggerConfiguration() .Enrich.With<MySerilogEnricher>() .ReadAppSettings() .CreateLogger(); I want to inject some dependencies into my MySerilogEnricher class, but when I try, I get this compiler error: error CS0310: 'SerilogEnricher' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TEnricher' in the generic type or method