inversion-of-control

Confusion with StructureMap 4.6 Transient Lifecycle

牧云@^-^@ 提交于 2020-01-15 03:52:04
问题 I am using StructureMap 4.6 as my IoC Container. I am a bit confused about its lifecycles. As I have read in its documentation, Transient will create a single instance of the object per container. Supported Lifecycles I am checking this scenario by creating a simple Console Application project. My code is as below: Program.cs class Program { private static IContainer _Container; static void Main(string[] args) { _Container = Container.For<ConsoleRegistry>(); var serv1 = _Container.GetInstance

Injecting a dependency into a custom ModelBinder

做~自己de王妃 提交于 2020-01-14 13:44:08
问题 I have an ASP.net MVC project in the works at the moment, and I'm wondering if the following could be possible: I have a custom ModelBinder class that has a reference to a service (essentially a fetcher) as a dependency. I want the dependency to be injected using an IoC container (currently Ninject) but there seems to be nowhere in the method chain that I can plug in something that says load the model binder from my IoC container. My first thought is to have a generic object binder that then

Injecting a dependency into a custom ModelBinder

扶醉桌前 提交于 2020-01-14 13:44:06
问题 I have an ASP.net MVC project in the works at the moment, and I'm wondering if the following could be possible: I have a custom ModelBinder class that has a reference to a service (essentially a fetcher) as a dependency. I want the dependency to be injected using an IoC container (currently Ninject) but there seems to be nowhere in the method chain that I can plug in something that says load the model binder from my IoC container. My first thought is to have a generic object binder that then

What is the best strategy for Dependency Injection of User Input?

别来无恙 提交于 2020-01-14 10:32:32
问题 I've used a fair amount of dependency injection, but I'd like to get input on how to handle information from the user at runtime. I have a class that connects to a com port. I allow the user to select the com port number. Right now, I have that com port parameter as a constructor argument. The reasoning being that the class cannot function without that information, and it's implementation specific (a mock version of this class wouldn't need a com port). The alternative is to have a "Start"

ASP.Net MVC5 and StructureMap4 - Simplified Approach

只谈情不闲聊 提交于 2020-01-14 02:44:06
问题 While integrating StructureMap.MVC5 to an ASP.Net MVC5 web application, realized that it uses 3.1 version of SM and not 4+. Then tried taking the files included in this Nuget and changing it for SM4, but a lot of code was there and several incompatible calls across SM3.1 and SM4. With that, I ended up writing a simple IoC as below. Looking for advise on its shortfalls and what inefficiencies this have compared to the Nuget version linked here. Define Default Registry public class

Spring.Net IoC and Magic Strings

旧时模样 提交于 2020-01-13 19:48:47
问题 I've been thinking about the IApplicationContext.GetObject(string name) method and it seems to rely fairly heavily on magic strings to get objects from the application context. With other containers like StructureMap you can use generics to specify the configuration and request objects but is there a better way than using strings with the Spring.Net IoC container to request objects from the ApplicationContext? 回答1: You could have a wrapper that you call, taking a generic type parameter.

Dependency Injection Gotchas

杀马特。学长 韩版系。学妹 提交于 2020-01-13 19:24:49
问题 Does anyone have a list of link(s) on the www for a good list of DI gotchas? I have been trying to inject controls using DI, in an asp.net webforms app and found that on recursive build up that ViewState is lost. Also would be helpful a list of articles where the developer needs to be aware to gotchas before taking teh big step in implementing IoC/DI in the app. 回答1: DI itself has no gotchas. Or more precisely, it would be like enumerating general programming gotchas, like passing a null

MEF error, was circular dependency and is now something else

故事扮演 提交于 2020-01-13 12:06:54
问题 I've got a circular dependency that recently came about because of a change in my application architecture. The application relies on a plugin manager that loads plugins via MEF. Everything up until worked fine, because it looked something like this: // model.cs [Export("Model")] public class Model { public PluginManager PM { get; set; } [ImportingConstructor] public Model( [Import] PluginManager plugin_manager) { PM = plugin_manager; } } // pluginmanager.cs [Export(typeof(PluginManager))]

Using Ninject IOC to replace a factory

情到浓时终转凉″ 提交于 2020-01-13 07:42:06
问题 I've got a factory method inside a parser. Essentially as I load a token I look up the handler for that token, or drop through to the default handler. I've implemented this as a switch and as a Dictionary<string,Type> but both approaches require me to store the mapping somewhere else than the handler class. We are using Ninject for IOC and so I've realized I can also do it using kernel.Get<ITokenHandler>(tokenName); but that doesn't save me storing the information on what token the handler

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