ioc-container

Still need help understanding why Ninject might be better than manual DI

旧街凉风 提交于 2019-12-08 17:00:38
问题 This is an extension to the question Why do I need an IoC container as opposed to straightforward DI code? I've been learning Ninject and came up with the following example, the example goes through the manual way of doing DI and the Ninject way of doing DI: class Program { static void Main(string[] args) { NinjectWay(); ManualWay(); Console.ReadKey(); } private static void ManualWay() { Console.WriteLine("ManualWay***********************"); IWeapon sword = new Sword(); Samurai samurai = new

Dependency injection for extension classes?

血红的双手。 提交于 2019-12-08 16:42:02
问题 I'm using Microsoft Unity as my IoC container. I have a number of extension classes which adds useful methods to my business objects This is the sort of code I use today: public static class BusinessObjectExtensions { public static bool CanDoStuff(this BusinessObject obj) { var repository = BusinessHost.Resolver.Resolve<IRepository>(); var args = new EArgument { Name = obj.Name }; return repository.AMethod(obj.UserName, args); } } Is there a better way to manage dependency injection for

Autofac: Resolving variant types with both in and out type arguments

隐身守侯 提交于 2019-12-08 16:10:22
问题 This question is a follow up of my previous question: Autofac: Hiding multiple contravariant implementations behind one composite. I'm trying to find the boundries of what we can do with Autofac's covariance and contravariance support. I noticed that Autofac's ContravariantRegistrationSource only supports generic interfaces with a single generic parameter that is marked with the in keyword. This seems to limit the usefulness of this feature, and I'm wondering if Autofac has other ways in

Registering 'half-closed' generic component

China☆狼群 提交于 2019-12-08 15:11:41
问题 I have two interfaces: public interface IQuery<TResult> { } public interface IQueryHandler<in TQuery, out TResult> where TQuery : IQuery<TResult> { TResult Handle(TQuery q); } An example of a closed implementation of IQueryHandler: public class EventBookingsHandler : IQueryHandler<EventBookings, IEnumerable<EventBooking>> { private readonly DbContext _context; public EventBookingsHandler(DbContext context) { _context = context; } public IEnumerable<EventBooking> Handle(EventBookings q) {

IOC across visual studio projects?

人盡茶涼 提交于 2019-12-08 14:38:24
I'm trying to retro-fit (bad idea I know, but better late than never) IOC and DI into an existing solution. The code base is made of up about 30 projects, each which have classes in them which have little or no visibility to the outside world. Being relatively new to the IOC thing I'm trying to use best practise when reworking the code, and it seems best not to pass the IOC container around or make it static, hence I'm trying to do everything via constructor injection. However, and here comes my question, I am having to make an awful lot of the classes public across projects (i.e. physical

Issue with Autofac 2 and MVC2 using HttpRequestScoped

泪湿孤枕 提交于 2019-12-08 13:24:05
问题 I'm running into an issue with Autofac2 and MVC2. The problem is that I am trying to resolve a series of dependencies where the root dependency is HttpRequestScoped. When I try to resolve my UnitOfWork (which is Disposable), Autofac fails because the internal disposer is trying to add the UnitOfWork object to an internal disposal list which is null. Maybe I'm registering my dependencies with the wrong lifetimes, but I've tried many different combinations with no luck. The only requirement I

Windsor Ioc container: How to register that certain constructors take different implementation of an interface

风格不统一 提交于 2019-12-08 10:03:09
问题 I have lots of classes that take an IMyService as a constructor argument. e.g. ClassA(IMyservice myservice) // this should take a Concrete1 for IMyService ClassB(IMyservice myservice) // this should take a Concrete2 for IMyService How do I do my registration so that ClassB gets a Concrete2 and ClassA gets a Concrete1? Plus, is there a way to make one the default and only specify the instances that deviate from the default? (As the majority will take a Concrete1 and only a small number will

Spring and Multithreading

£可爱£侵袭症+ 提交于 2019-12-08 09:27:09
问题 I need to start a variable number of threads which in turn each start a varying number of threads (i.e. i threads where the Ith thread needs to start Ki threads) in a spring application. assuming each of the "I threads" contains an inner class which is autowired how will I generate those instances? So I have an A bean which needs to somehow generate I instances of a bean which needs to be spring managed to satisfy its dependencies. I've written a short sample code of what I think is the base

Dependency Injection and .NET Attributes

旧时模样 提交于 2019-12-08 05:02:52
问题 I have a couple of method attributes which do some logging. Our logging code sits behind an interface (ILog) and I'd like it if the attributes were only dependent upon that interface as opposed to an implementation. This isn't really about testability or dependency inversion so much as it is about keeping the coupling of components clean. An example would be where we have a web (Mvc) specific attribute as follows: HandleExceptionAttribute : FilterAttribute, IExceptionFilter { public void

Chaining layers with IoC, setting lower callback to upper and avoid circular reference

核能气质少年 提交于 2019-12-08 04:30:35
问题 I have a scenario where I need a lower layer to be controlled by an upper layer much like a puppet master pulling on strings. The lower layer also will call back to the upper layer as some internal events are generated from time to time. I am using SimpleInjector, I inject the ILower in to the Upper constructor. I cannot inject the Upper in to the Lower as it would cause a circular reference. Instead I have a register callback function to link the two layers. However, I have to scatter my