inversion-of-control

How to set up IoC when a key class needs Session (or other context-specific variable)

那年仲夏 提交于 2020-01-28 05:18:17
问题 I am trying to figure out how to use IoC in situations where the dependent classes can change based on some variable in the application (in this case, Session state). For example, each of our clients have a different database, so the connection to the database needs to be built on a value stored in their Session (particularly since some users could have multiple databases if they own multiple businesses, and would switch between databases). Here is a generic example of how we'd currently set

How to set up IoC when a key class needs Session (or other context-specific variable)

痞子三分冷 提交于 2020-01-28 05:17:06
问题 I am trying to figure out how to use IoC in situations where the dependent classes can change based on some variable in the application (in this case, Session state). For example, each of our clients have a different database, so the connection to the database needs to be built on a value stored in their Session (particularly since some users could have multiple databases if they own multiple businesses, and would switch between databases). Here is a generic example of how we'd currently set

Why not use an IoC container to resolve dependencies for entities/business objects?

旧时模样 提交于 2020-01-26 05:15:21
问题 I understand the concept behind DI, but I'm just learning what different IoC containers can do. It seems that most people advocate using IoC containers to wire up stateless services, but what about using them for stateful objects like entities? Whether it's right or wrong, I normally stuff my entities with behavior, even if that behavior requires an outside class. Example: public class Order : IOrder { private string _ShipAddress; private IShipQuoter _ShipQuoter; public Order(IOrderData

How to use @Autowired in not Spring's stereotype classes

我怕爱的太早我们不能终老 提交于 2020-01-24 19:29:05
问题 I would like to use that repository in this class, but when I put a stereotype like @Component, I get an error from the IDE: Could not autowire. No beans of 'Authentication' type found. public class CustomMethodSecurityExpressionRoot extends SecurityExpressionRoot implements MethodSecurityExpressionOperations { @Autowired private FlatRepository flatRepository; public CustomMethodSecurityExpressionRoot(Authentication authentication) { super(authentication); } } 回答1: You cannot @Autowire inside

What is wrong in my way of explainning DI and IoC?

江枫思渺然 提交于 2020-01-24 10:00:48
问题 Yesterday during an interview I was asked what DI and IoC in spring were. My reply was: when a class(A) extends abstract class(B) or implements interface(B) or create a object of class(B) of any class in it, then A is said said to be dependent on B . Injecting this dependency, i.e. injecting the object in costructor or in setter method is called DI and in this process control over creating object goes to the "outside world" like XML configuration, this inversion of control is IoC. DI is not

How to pass constructor parameter while using spring auto wiring?

拟墨画扇 提交于 2020-01-24 09:58:29
问题 Our Project is using spring DI/IoC, so i am using autowiring to inject beans. The program needs to pass parameters to an object during it's instantiation. And the parameters are know at run time (not at compile time). How to achive this while using autowiring. Sample code is as below. Interface - IMessage package com.example.demo.services; public interface IMessage { String message(String name); } Implementations - SayHelloService package com.example.demo.services; import org.springframework

Where is the composition root in a Windows Phone app?

↘锁芯ラ 提交于 2020-01-24 07:45:45
问题 I'm new to WP 7. For purposes of dependency injection, I want to adhere to practices acquired doing WinForms apps. I therefore want to build my app graph at the composition root. What part of a regular WP 7 app source code can be viewed as the composition root? 回答1: The pattern I prefer, as recommended by the Patterns & Practices team, is: Add a ViewModelLocator class to your resources App.xaml with an x:Key="ViewModelLocator" , and add to that class a property for each ViewModel type and

Autofac Dependency Injection in Azure Function

荒凉一梦 提交于 2020-01-23 06:20:12
问题 I am trying to implement DI using Autofac IOC in Azure function. I need to build the container, but not sure where to put the code to build the container 回答1: I think for now you would need to do something ugly like: public static string MyAwesomeFunction(string message) { if (MyService == null) { var instantiator = Initialize(); MyService = instantiator.Resolve<IService>(); } return MyService.Hello(message); } private static IService MyService = null; private static IContainer Initialize() {

Why should I not make my services singletons (ioc)?

依然范特西╮ 提交于 2020-01-23 05:39:05
问题 Important: Do note that I do not mean singletons as in having a private constructor and a static instance variable (or as someone suggested a static class), but singletons as returning the same instance from the inversion of control container during the application lifetime. Many containers use a short life time per default. Either a new instance per dependency (or per request) or an instance per scope (such as a HTTP request). I'm wondering why containers promote short lived objects instead

Unity IoC and MVC 3 Beta - Passing IRepository to Controller Constructor

给你一囗甜甜゛ 提交于 2020-01-23 03:06:08
问题 Did something change in MVC 3? I have tried all the examples on the Internet for setting up Unity as my IoC Container, but I keep getting an error saying that Unity cannot resolve my UserController. Here is my constructor on my UserController: public UserController(IUserService userService) { _userService = userService; } I have the IUserService registered, that is not the problem. I keep getting errors, no matter what example I try. Does anyone have a good tutorial, or code, that works with