Last active
December 10, 2015 22:28
-
-
Save akimboyko/4502263 to your computer and use it in GitHub Desktop.
Configuration by Convention using Ninject.Extensions.Conventions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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