Category: C#

The simplest form of configurable Dependency Injection

public class MyClass { public static Func<string, string> GetConfigValue = s => ConfigurationManager.AppSettings[s]; //… Normal use: string connectionString = MyClass.GetConfigValue(“myConfigValue”); Custom (e.g. Unit Testing) use: MyClass.GetConfigValue = s => s == “myConfigValue” ? “Hi”, “string.Empty”; The simplest form of configurable Dependency Injection Read more →

How to make a custom dependent file in Visual Studio.

From this: To this: Open the .proj file Find the XML that defines these files <Compile Include=”Models\Context.cs” /> <Compile Include=”Models\Client.cs” /> <Compile Include=”Models\Image.cs” /> <Compile Include=”Models\Office.cs” /> <Compile Include=”Models\Policy.cs” /> and add DependentUpon tags <Compile Include=”Models\Context.cs” /> <Compile Include=”Models\Client.cs”> <DependentUpon>Context.cs</DependentUpon> </Compile> <Compile Include=”Models\Image.cs”> <DependentUpon>Context.cs</DependentUpon> </Compile> <Compile Include=”Models\Office.cs”> <DependentUpon>Context.cs</DependentUpon> </Compile> <Compile Include=”Models\Policy.cs”> <DependentUpon>Context.cs</DependentUpon> </Compile> Read more →

Basic MSMQ example

ClassLibrary project public class Command1 { public int id { get; set; } public string name { get; set; } } static string queue = @”.private$Command1Queue”; Client project static void Main(string[] args) { MessageQueue msMq = new MessageQueue(queue); while (true) { var key = Console.ReadKey(); switch (key.Key) { case ConsoleKey.Escape: return; default: string name = “SomeName”; for (int i =… Read more →

SimpleInjector IEnumerable<ISomeService<T>> With A Mixture Of Open & Closed Generics – Further investigation

Please note that as of Release 2.3 of SimpleInjector the features discussed in this and related articles are available out of the box with the new registration method RegisterAllOpenGeneric This post is a follow on from previous posts here and here. I’ve altered the test to return IEnumerable<ISomeService<T>> where T is an interface instead of a class. public interface IClass… Read more →

SimpleInjector IEnumerable<ISomeService<T>> with a mixture of open & closed generics – one possible solution

Please note that as of Release 2.3 of SimpleInjector the features discussed in this and related articles are available out of the box with the new registration method RegisterAllOpenGeneric private static Container BootstrapSI() { Container container = new Container(); List<Type> types = new List<Type>(); container.RegisterManyForOpenGeneric(typeof(IObserve<>), AccessibilityOption.PublicTypesOnly, (service, implTypes) => { //ignore implTypes as it only contains closed generic types var… Read more →

SimpleInjector IEnumerable<ISomeService<T>> with a mixture of open & closed generics – the problem

Please note that as of Release 2.3 of SimpleInjector the features discussed in this and related articles are available out of the box with the new registration method RegisterAllOpenGeneric I like SimpleInjector, it’s great and so is the support. The guy behind SimpleInjector is a top class craftsman. However I recently discovered that SimpleInjector does not return any open generic… Read more →