Posts

Showing posts with the label Unity

Register Types By Convention With Unity

Using Microsoft's Unity IoC container, you can register types by convention rather than having to explicitly register each interface to a concrete class. The convention is that interfaces beginning with "I" will map to concrete classes with the same name, minus the "I" (for example, interface IMyClass would map to concrete class MyClass). I only recently learned this, after watching a Pluralsight course that showed how to do the same thing using StructureMap. I'd used StructureMap a few years ago, but was unaware of this feature at the time, and am not sure if it existed then or not. The example shown below will register types by convention (for example, uses of interface IMyClass yield concrete types of MyClass).             container             .RegisterTypes(                  AllCla...

Don't Do This With Microsoft's Unity IoC Container

I recently did something pretty dumb when using Microsoft's Unity IoC container. Because one of my classes depended on a string parameter for its constructor (a configuration value that I pass into it), I had to register the type rather than simply relying on dependency chaining. See below (this code is an example of the real code, but not the actual code as to protect my employer's intellectual property guidelines): .RegisterType< IProjectReposito ry , ProjectRepository >(      new InjectionConstructor (         new ResolvedParameter ( typeof ( IREST ServiceHelpers )),             Config urationManager .AppSettings[ " CentralServi ceURL" ],         new ResolvedParameter ( typeof ( ICach eManager )),         new WidgetFactory() )) See the problem? It's that "new WidgetFactory()" stateme...