ioc-container

I'm confused about interface abstractions when using IoC

让人想犯罪 __ 提交于 2019-12-17 16:28:12
问题 I've recently been trying to learn IoC, and have a couple questions based on the following code: public class WarriorModule : NinjectModule { public override void Load() { Bind<IWeapon>().To<Sword>(); Bind<Samurai>().ToSelf(); } } I'm having trouble grasping the new concept of interfaces. Before I would create an interface such as IRunnable , implementing the function void Run() . With IoC, I'm now viewing an interface as something that only maps to a single concrete class. Assuming that, how

Castle Windsor passing constructor parameters

妖精的绣舞 提交于 2019-12-17 15:58:09
问题 I have an IAddress class with a few properties. I then have a concrete type that implements this interface. This concrete type has a couple of different constructors I could use. How can I pass parameter values to one of these constructors at run-time? I cannot use the config file as I will be reusing this concrete type multiple times and each time the parameter values will be different. IWindsorContainer container = new WindsorContainer(new XmlInterpreter()); IAddress address = container

Strategy Pattern and Dependency Injection using Unity

对着背影说爱祢 提交于 2019-12-17 15:36:25
问题 I am finally getting my feet wet with Dependency Injection (long overdue); I got started playing with Unity and run into an issue with the strategy pattern. I can use the container to return to me specific implementations of a strategy based on a name, but what I don't see is how I am supposed to get the right strategy in the context. Let's illustrate on a simple example: the context is a car, which has an IEngine (the strategy), with 2 implementations, FastEngine and SlowEngine. The code

Arguments against Inversion of Control containers

谁说胖子不能爱 提交于 2019-12-17 10:26:25
问题 Seems like everyone is moving towards IoC containers. I've tried to "grok" it for a while, and as much as I don't want to be the one driver to go the wrong way on the highway, it still doesn't pass the test of common sense to me. Let me explain, and please correct/enlighten me if my arguments are flawed: My understanding: IoC containers are supposed to make your life easier when combining different components. This is done through either a) constructor injection, b) setter injection and c)

Ninject + Bind generic repository

孤街浪徒 提交于 2019-12-17 09:37:49
问题 I'm trying to Bind a generic IRepository<> interface to my generic Repository<> - however it always return null? I have tried various things like: Bind(typeof(IRepository<CustomerModel>)).To(typeof(Repository<CustomerModel>)); Bind(typeof(IRepository<>)).To(typeof(Repository<>)); However if I pass in a non-generic interface and class then it works like a dream? 回答1: Bind(typeof(IRepository<>)).To(typeof(Repository<>)); This is the correct syntax for binding an open generic. If you are

MVC, EF - DataContext singleton instance Per-Web-Request in Unity

余生长醉 提交于 2019-12-17 02:21:06
问题 I have a MVC 3 web application, where I am using the Entity Framework for the data access. Furthermore, I have made a simple use of the repository pattern, where e.g. all Product related stuff is handled in the "ProductRepository" and all User related stuff is handled in the "UserRepository". Thus, I am using the UNITY container, to make a singleton instance of the DataContext, which I inject into each of the repositories. A quick search on Google, and everyone recommends you to NOT use a

Unit Testing the IoC container itself

这一生的挚爱 提交于 2019-12-14 02:18:49
问题 I don't think this was asked before, although it's really hard to search for a term like unit test ioc container and not find a question about how to implement IoC in order to perform unit tests. I would like to have unit tests against the IoC container itself basically because sometimes I have issues with the container (like you could with any other part of an application), and it's pretty troublesome to test the resolution of dependencies merely debugging. If I could introduce unit tests

Do DI Containers need to instantiate the object themselves in order to use constructor injection?

梦想与她 提交于 2019-12-14 02:18:08
问题 Or instead of creating the object themselves, do they somehow intercept or hook into object creation (for example, a Controller instantiated by the MVC framework) and pass in whatever dependencies they're required to? I realize they need to do something when the object is created in order to use constructor injection, but I am unclear as to whether the containers need to do the creating, or if they somehow intercept the object's creation. Whatever the answer is, do all DI containers do it

Convert Castle Windsor xml config to C# code

点点圈 提交于 2019-12-13 19:52:21
问题 I want to convert something like this: <components> <component id=""service1"" service=""WindsorTests.IService, MyAssembly"" type=""WindsorTests.Service1, MyAssembly""/> <component id=""service2"" service=""WindsorTests.IService, MyAssembly"" type=""WindsorTests.Service2, MyAssembly""/> <component id=""consumer"" type=""WindsorTests.Consumer, MyAssembly""> <parameters> <services> <dictionary> <entry key=""one"">${service1}</entry> <entry key=""two"">${service2}</entry> </dictionary> <

Should I use Unity Config file or Code to register types and instances?

不问归期 提交于 2019-12-13 19:02:02
问题 Finally started to configure an IoC Container! I'm using Unity and have configured it to have my objects registered using the config file: e.g. <container> <register type="ILogger" mapTo="Logger"> <lifetime type="singleton"/> </register> <register type="IPdfWriter" mapTo="PdfWriter"> <lifetime type="perthread" /> <constructor /> </register> </container> I've reached a point where I doubt this is a good approach to register types. For example a class of mine is dependent on the ICacheManager