Skip to content

@akimboyko /gist:4502263
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Configuration by Convention using Ninject.Extensions.Conventions
void Main()
{
// 4 from your Compositon Root ...
using(var kernel = InitializeKernel())
{
// 4.1 resolve delivery services by names
var upsWorldShip = kernel.Get<IShippingCompanyService>("ShippingUpsWorldShip");
var fedExDesktopApps = kernel.Get<IShippingCompanyService>("ShippingFedExDesktopApps");
// 4.2 delivery processing
upsWorldShip.Delivery();
fedExDesktopApps.Delivery();
// 5 PROFIT!
}
}
// 1 define "delivery" interface
interface IShippingCompanyService
{
void Delivery();
}
// 2.1 — first assembly "Ups.Services.dll"
public class ShippingUpsWorldShip : IShippingCompanyService
{
public void Delivery()
{
"Ship with UPS WorldShip".Dump();
}
}
// 2.1 — first assembly "Ups.Services.dll"
public class ShippingFedExDesktopApps : IShippingCompanyService
{
public void Delivery()
{
"Ship with FedEX Desktop Apps".Dump();
}
}
// 3 kernel configuration
public static IKernel InitializeKernel()
{
var kernel = new StandardKernel();
kernel.Bind(x => x
// 3.1 search in current assembly
.FromThisAssembly()
.SelectAllClasses() // 3.2 select all classes implement "special" interface
.InheritedFrom<IShippingCompanyService>()
// 3.3 search all assemblies by wildcards
.Join.FromAssembliesMatching("./*Services.dll")
.SelectAllClasses() // 3.2 select all classes implement "special" interface
.InheritedFrom<IShippingCompanyService>()
// 3.4 bind to "special" interface
.BindAllInterfaces()
// 3.5 configure lifetime management and dependency name
.Configure((b, c) =>
b.InTransientScope().Named(c.Name)));
return kernel;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.