inversion-of-control

StructureMap: How to register the same instance for all its interfaces

梦想与她 提交于 2019-12-23 08:58:52
问题 StructureMap newbie question. public class SomeClass: IInterface1, IInterface2 { } I would like the following test to pass: Assert.AreSameInstance( container.GetInstance<IInterface1>(), container.GetInstance<IInterface2>()); How would I do an explicit registration of this? I know in Castle Windsor I would do something like kernel.Register(Component.For(typeof(IInterface1), typeof(IInterface2)) .ImplementedBy(typeof(SomeClass)); But I don't see any equivalent API 回答1: ObjectFactory.Initialize

Testing if a Type has been registered in Unity

喜你入骨 提交于 2019-12-23 08:03:25
问题 Can I test if a type has been registered in a Unity container without calling for a Resolve and trapping the exception? 回答1: Unity 2.0 will have an IsRegistered method that you can use to find out if a type has been registered in the container. The Beta1 of Unity 2.0 is available on Codeplex as of Feb 10th. See the release notes and download it here; http://unity.codeplex.com/wikipage?title=Unity2%20Beta1 UPDATE: Downloaded and tested Unity 2.0 beta 1 on Feb 27th 2010, and it's by far

How to register a generic interface using TinyIOC

给你一囗甜甜゛ 提交于 2019-12-23 07:49:52
问题 Suppose I have a generic interface and a generic implementation. How do I register all usages? Specifically, I have the following (reduced for simplicity): public interface IRepository<T> where T : TableEntity { T GetById(string partitionKey, string rowKey); void Insert(T entity); void Update(T entity); void Update(string partitionKey, string rowKey, Action<T> updateAction); void Delete(T entity); IQueryable<T> Table { get; } } public class AzureRepository<T> : IRepository<T> where T :

Can you register an existing instance of a type in the Windsor Container?

岁酱吖の 提交于 2019-12-23 07:05:56
问题 In the Windsor IOC container is it possible to register a type that I've already got an instance for, instead of having the container create it? 回答1: There is a AddComponentInstance method on the Container's Kernel property. From the Unit Tests: [Test] public void AddComponentInstance() { CustomerImpl customer = new CustomerImpl(); kernel.AddComponentInstance("key", typeof(ICustomer), customer); Assert.IsTrue(kernel.HasComponent("key")); CustomerImpl customer2 = kernel["key"] as CustomerImpl;

Dependency injection in thread that create objects

拟墨画扇 提交于 2019-12-23 03:52:38
问题 If I have a thread that is started from the main application. In this thread events are generated. Everytime an event is generated an object is created. In that object a dependancy is needed so I want to inject that object. How can I pass this dependancy to the created object? Do I need to pass the depandancy downwards (and let the class that works in the thread know of the dependancy) or is there a nicer way to do this? I'm using Unity btw. 回答1: The safest way is to let each thread build a

Tutorial About IoC + NHibernate + MVC

余生颓废 提交于 2019-12-23 02:53:12
问题 I was following up Jason Dentler's series of posts on how to use NHibernate + Ioc (Ninject) with a real world ASP.NET MVC N-Tier application. Sadly he dropped the series. Does anyone has any good suggestions of articles that cover a similar scenario? I would be very interesting in learning such things. 回答1: Bob Cravens TruckTracker turorials utalise MVC, NHibernate, Ninject and MySQL. I have found them to be an invaluable resource. 回答2: I don't know about tutorials but I think that you should

Castle Windsor: How to prevent circular references in factory-created objects were the created objects refers back to the factory

旧城冷巷雨未停 提交于 2019-12-23 02:24:30
问题 I am using windsor castle as my IoC container, and has run in to a bit of a problem. This is best explained in code, so I´ll give it a try. I have a factory class, that should provide me with implementations of a certain interface: public interface IObjectCreatorFactory { IObjectCreator GetObjectCreator(Type objectType); } public interface IObjectCreator { T CreateObject<T>(IDataRow data); bool SupportsType(Type type); } Implementation of the factory class could look like this, though I am

Configuring Ninject to return a list of results

余生颓废 提交于 2019-12-23 02:01:38
问题 I'm using Ninject to resolve dependencies and it's working swimmingly until now. In this example, I actually need a list of objects initialized based on data stored in my App.config. However, Ninject keeps returning an empty list. The snippet below is an example of what I tried. I've included the constructors for the class hierarchy for some context. public ServiceSchedulerBuilder(IList<ITextExportService> textExportServices) { _textExportService = textExportServices; } public

Configuring Ninject to return a list of results

眉间皱痕 提交于 2019-12-23 02:01:28
问题 I'm using Ninject to resolve dependencies and it's working swimmingly until now. In this example, I actually need a list of objects initialized based on data stored in my App.config. However, Ninject keeps returning an empty list. The snippet below is an example of what I tried. I've included the constructors for the class hierarchy for some context. public ServiceSchedulerBuilder(IList<ITextExportService> textExportServices) { _textExportService = textExportServices; } public

configuration settings and IoC

假装没事ソ 提交于 2019-12-22 14:49:06
问题 I use IoC (DI) approach and usually have parameters, which are being read from configuration settings (i.e. connection strings, static values etc) by the lowest layer (DB layer etc). What is the best way to do it? Read directly in this the lowest layer, i.e.: string sendGridApiKey = ConfigurationManager.AppSettings["SendGridApiKey"]; It works, but need to add also this key to config file of unit test project. Also, assembly depends on configuration file Read it in the highest layer (i.e. web