Generic Type in constructor

You can’t make constructors generic, but you can use a generic static method instead: public static Constructor CreateInstance<T>(int blah, IGenericType<T> instance) and then do whatever you need to after the constructor, if required. Another alternative in some cases might be to introduce a non-generic interface which the generic interface extends. EDIT: As per the comments… … Read more

how to use MVVMLight SimpleIoc? [closed]

SimpleIoc crib sheet: 1) You register all your interfaces and objects in the ViewModelLocator class ViewModelLocator { static ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); if (ViewModelBase.IsInDesignModeStatic) { SimpleIoc.Default.Register<IDataService, Design.DesignDataService>(); } else { SimpleIoc.Default.Register<IDataService, DataService>(); } SimpleIoc.Default.Register<MainViewModel>(); SimpleIoc.Default.Register<SecondViewModel>(); } public MainViewModel Main { get { return ServiceLocator.Current.GetInstance<MainViewModel>(); } } } 2) Every object is a singleton by … Read more

How to resolve IOptions instance inside ConfigureServices?

If you need to resolve service using the service provider manually you can use this AddSingleton/AddScoped/AddTransient overload: // Works for AddScoped and AddTransient as well services.AddSingleton<IBarService>(sp => { var fooService = sp.GetRequiredService<IFooService>(); return new BarService(fooService); } If you really want to, you can build an intermediate service provider using the BuildServiceProvider() method on the IServiceCollection: … Read more

Inversion of Control < Dependency Injection [closed]

If you accept Fowler’s definition, Inversion of Control is a much broader term than DI that covers all framework usage where you plug into a framework, but the framework is still in control. For example, in .NET, frameworks such as ASP.NET or Windows Presentation Foundation are ultimately in control, but provide various events and Seams … Read more

Inversion of Control vs Dependency Injection

The Inversion-of-Control (IoC) pattern, is about providing any kind of callback (which “implements” and/or controls reaction), instead of acting ourselves directly (in other words, inversion and/or redirecting control to the external handler/controller). For example, rather than having the application call the implementations provided by a library (also known as toolkit), a framework calls the implementations provided by the application. The Dependency-Injection (DI) pattern is a … Read more

Dependency Injection Unity – Conditional Resolving

A simple way to solve this is with the strategy pattern. Note that you can add or remove login providers without changing the design – you simply need to change the DI configuration. Interfaces public interface IAuthenticate{ bool Login(string user, string pass); bool AppliesTo(string providerName); } public interface IAuthenticateStrategy { bool Login(string providerName, string user, … Read more