dependency-injection

Spring @Autowired and @Value on property not working

大兔子大兔子 提交于 2020-01-13 09:33:08
问题 I would like to use @Value on a property but I always get 0 (on int). But on a constructor parameter it works. Example: @Component public class FtpServer { @Value("${ftp.port}") private int port; public FtpServer(@Value("${ftp.port}") int port) { System.out.println(port); // 21, loaded from the application.properties. System.out.println(this.port); // 0??? } } The object is spring managed, else the constructor parameter wouldn't work. Does anyone know what causes this weird behavior? 回答1:

Inject service into an AutoMapper destination class

萝らか妹 提交于 2020-01-13 08:40:07
问题 Say I have a source and destination class that is mapped using AutoMapper. The destination has a logger service injected into the constructor. However, I don't know how to get the service injected into the constructor through StructureMap? I've tried the following: Mapper.Initialize(m => { m.ConstructServicesUsing(ObjectFactory.GetInstance); }); which didn't prevent me having the exception on the mapping call, I guess because the service isn't being injected in properly. I also tried the

Dagger 2: how to change provided dependencies at runtime

回眸只為那壹抹淺笑 提交于 2020-01-13 07:49:33
问题 In order to learn Dagger 2 i decided to rewrite my application but I'm stuck with finding the proper solution for the following problem. For the purpose of this example let's assume we have an interface called Mode : public interface Mode { Object1 obj1(); //some other methods providing objects for app } and two implementations: NormalMode and DemoMode . Mode is stored in singleton so it could be accessed from anywhere within application. public enum ModeManager { INSTANCE,; private Mode mode

C# Dependency injection side effect (two step initialization anti-pattern)? [duplicate]

时光毁灭记忆、已成空白 提交于 2020-01-13 06:41:26
问题 This question already has answers here : Is there a pattern for initializing objects created via a DI container (5 answers) Closed 4 years ago . I'm working on a project in which my constructors contain - only - behavioral dependencies. i.e. I never pass values / state. Example: class ProductProcessor : IProductProcessor { public double SomeMethod(){ ... } } class PackageProcessor { private readonly IProductProcessor _productProcessor; private double _taxRate; public PackageProcessor

Learning Inversion of Control through Dependency Injection in MVC 3

一笑奈何 提交于 2020-01-13 06:34:10
问题 Can someone please provide me a simple example of DI and IoC ? I understand the first one but was unable to get an implementation of the second one. I'm working with MVC 3 and I really want to use the IDependencyResolver, but I can't understand how. Is ninject an option or is it required? Is there a tutorial like nerdDinner and I wasn't able to find it? Can somebody give me a link to study it? 回答1: In order to grok real world DI and its patterns and anti-patterns quickly, I recommend getting

Ninject Dependency Injection in MVC3 - Outside of a Controller

情到浓时终转凉″ 提交于 2020-01-13 05:18:06
问题 We are using Ninject in our MVC3 project to do dependency injection. I used NuGet to add package references to Ninject and Ninject.MVC3 packages. When I did this it created a NinjectMVC3 class in my App_Start folder: public static class NinjectMVC3 { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule)); DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule)

DDD: is it ok to inject a Service into an Entity

吃可爱长大的小学妹 提交于 2020-01-13 03:20:08
问题 I have a tree of Zone objects: class Zone { protected $parent; public function __construct(Zone $parent) { $this->parent = $parent; } } There are no children nor descendants property in the Zone, because I want to avoid the pain of managing these relationships in the domain model. Instead, a domain service maintains a closure table in the database, to map a zone to all its descendants, at any level. Now, I have a User which can be assigned one or more Zones: class User { protected $zones;

PHP - multiple different databases dependency injected class

耗尽温柔 提交于 2020-01-12 20:38:48
问题 I've spent the last several hours trying to find an answer to the "best", most logical, etc way to write a php database class to simultaneously connect to one postgresql db and one mysql db. Also, I'd like to adopt a Dependency Injection design but am new to that whole concept. So far I've come up with... class Database { public function PgSqlConnect() { /* Connect to database */ $host = 'localhost'; $dbname = '---'; $user = '---'; $pass = '---'; $timeout = 5; /* seconds */ try { $pgsql_dbh =

Installing a new middleware at runtime in ASP.Net Core

萝らか妹 提交于 2020-01-12 14:44:25
问题 When my application starts, I have a bunch of modules (module1, module2 …). For each of this module I have a bunch of controller actions : /myModuleController/module1/action1 /myModuleController/module1/action2 /myModuleController/module2/action1 /myModuleController/module2/action2 … As the user can log himself once per module, I deploy an authentication middleware per module, which is simply done this way : app.UseWhen((context) => context.Request.Path.StartsWithSegments(urlPath), appbuilder

Installing a new middleware at runtime in ASP.Net Core

若如初见. 提交于 2020-01-12 14:43:26
问题 When my application starts, I have a bunch of modules (module1, module2 …). For each of this module I have a bunch of controller actions : /myModuleController/module1/action1 /myModuleController/module1/action2 /myModuleController/module2/action1 /myModuleController/module2/action2 … As the user can log himself once per module, I deploy an authentication middleware per module, which is simply done this way : app.UseWhen((context) => context.Request.Path.StartsWithSegments(urlPath), appbuilder