inversion-of-control

How to turn this Service Locator pattern into true Dependency Injection pattern?

╄→гoц情女王★ 提交于 2019-12-10 09:23:48
问题 I asked a more general question a minute ago: How to organize DI Framework usage in an application?, and the feedback I got was that I was using a Service Locator Pattern rather than true DI as is pointed out by Martin Fowler here: http://martinfowler.com/articles/injection.html Actually, I read that article just the other day, but apparently haven't quite grasped it. So let's say I have the following code: interface ICardReader { string GetInfo(); void SetDebugMode(bool value); void

Ninject conditional binding

↘锁芯ラ 提交于 2019-12-10 07:59:50
问题 I'm playing around with Ninject for a simple test-bed project at home, just to see what I can do with it. As a starting point I'm building a console runner for some service, which accepts a variety of arguments and based on what it gets in, uses the same methods provided for a fluent interface to configure a model to run. As an example, suppose I have a verbosity switch, /o . /o can be passed as /o:quiet , /o:normal , or /o:verbose . The various options are self-explanatory. To satisfy this

IOC issue: Too many Abstract Factories for Runtime data dependent classes

拜拜、爱过 提交于 2019-12-10 04:07:01
问题 I have recently started to use DI in one of my projects. For runtime dependent classes, I created corresponding Abstract factories. After following this pattern I end up having too many abstract factories, almost one for each of my class. Is it common to end up having too many abstract factories when using IOC ? Scenario: Suppose I get an "Entity" object from a database. There are 10 different use cases a user could perform on this entity object. For each of the usecase I have a different

Castle Windsor problem

主宰稳场 提交于 2019-12-09 19:12:05
问题 I have a problem with castle core, i'm trying to inject two different database connection to specific repositories. public class Repository1 { public Repository1(System.Data.Common.DbConnection conn) { } } public class Repository2 { public Repository2(System.Data.Common.DbConnection conn) { } } Now for example im would like to inject Mysql connection to Repository1 and Oracle connection to repository2. 回答1: Something like this: container.Register(Component .For<DbConnection>() .ImplementedBy

I need more Ninject practical examples

无人久伴 提交于 2019-12-09 15:59:12
问题 In the past, I used swiftsuspenders that is an actionscript 3 IoC controller. Basically the first version of switfsuspender had something similar to the Ninject kernel that was called injector. If I wanted to create an application injector (with let's say the most relevant mappings to be used throughout the application), I had to inject the injector itself in the application classes. I am wondering now what is the practice to use kernel.get<> among several classes in the application. Should I

Why is CastleWindsor's BeginScope out of scope?

若如初见. 提交于 2019-12-09 15:32:15
问题 I'm trying to add Castle Windsor to my Web API project, and am following this post, but am getting a compile-time error on this line of code: this._scope = container.BeginScope(); ...as " 'Castle.Windsor.IWindsorContainer' does not contain a definition for 'BeginScope' and no extension method 'BeginScope' accepting a first argument of type 'Castle.Windsor.IWindsorContainer' could be found (are you missing a using directive or an assembly reference?) " Here is the entire code so that it can be

How to inject dependency property using Ioc Unity

百般思念 提交于 2019-12-09 15:06:50
问题 I have the following classes: public interface IServiceA { string MethodA1(); } public interface IServiceB { string MethodB1(); } public class ServiceA : IServiceA { public IServiceB serviceB; public string MethodA1() { return "MethodA1() " +serviceB.MethodB1(); } } public class ServiceB : IServiceB { public string MethodB1() { return "MethodB1() "; } } I use Unity for IoC, my registration looks like this: container.RegisterType<IServiceA, ServiceA>(); container.RegisterType<IServiceB,

Is this a typical use case for IOC?

≯℡__Kan透↙ 提交于 2019-12-09 14:20:09
问题 My current application allows users to define custom web forms through a set of admin screens. it's essentially an EAV type application. As such, I can't hard code HTML or ASP.NET markup to render a given page. Instead, the UI requests an instance of a Form object from the service layer, which in turn constructs one using a several RDMBS tables. Form contains the kind of classes you would expect to see in such a context: Form => IEnumerable<FormSections> => IEnumerable<FormFields> Here's what

The type String cannot be constructed

限于喜欢 提交于 2019-12-09 14:09:14
问题 I'm using Web.api and Unity and I am getting the following error when trying to open the default "help" area: [InvalidOperationException: The type String cannot be constructed. You must configure the container to supply this value.] Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstructor) +280 Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext

Unittesting IoC registration?

主宰稳场 提交于 2019-12-09 11:59:39
问题 Should you unittest the code that registers components into your IoC container? If so, how? 回答1: In spring you can have a unit test that simply loads the application-context without asserting anything. It's actually a fairly useful test in conjunction with automatic build, since spring complains about a lot of problems when loading the full context. 回答2: It somehow feels wrong to me to have the IoC container running in my test-projects. I also noticed that most of the bugs that are caused by