Suppose you wanted to implement a spelling correction service. Input a word/phrase and out pops a corrected version of the word/phrase. What if you were depending on a large, online provider that went down for a day.
Ideally, you would implement a backup in case the request to said provider failed. In that backup, you might call your secondary service and hope they're online. Maybe in a last ditch effort in the event they're both down, you implement an offline provider to correct spelling based on a local dictionary that exists on the file system How do you manage multiple providers like this?
Before public class SpellCheck { public string FixText(string text) { string CorrectedText = string. Empty; // create HTTP request to Online Provider #1 // return CorrectedText; } } After public class SpellCheck { private readonly IOnlineSpellService _onlineSpell; private readonly IOfflineSpellService _offlineSpell; public SpellCheck(IOnlineSpellService onlineSpell, IOfflineSpellService offlineSpell) { this. _onlineSpell = onlineSpell; this.
_offlineSpell = offlineSpell; } // same method as before, just a slightly different implementation public string FixText(string text) { string fixedText = _onlineSpell. FixText(text); if(_onlineSpell. Failed) fixedText = _offlineSpell.
FixText(text); return fixedText; } } Of course, you'd need to implement the interface ISpellOnlineService (and similar interface for ISpellOfflineService public interface ISpellOnlineService { public bool Failed { get; set; } public string FixText(string text); } Next, implement your spelling providers (both online and offline or even multiple instances of both to gain more flexibility ) public class OnlineProviderNo1 : IOnlineSpellService { public string FixText(string text) { // implement online logic this. Failed = false; } } Finally, register your services by overriding the CreateKernel method and subclassing your MvcApplication from NinjectHttpApplication And apparently with Ninject you replace Application_Start with an override called OnApplicationStarted like so: This done in global. Asax public class MvcApplication : NinjectHttpApplication { // ... protected override void OnApplicationStarted() { base.
OnApplicationStarted(); AreaRegistration. RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters. Filters); RegisterRoutes(RouteTable.
Routes); } protected override IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind().To(); kernel.Bind().To(); return kernel; } } Yes, this is a real world example as I've implemented it (I actually used Castle Windsor, but it's the identical concept with Ninject--just adapted above) with 3 online providers and 2 offline providers. In CreateKernel you may choose to include logic to examine a database, config file, etc to determine which provider to register. For example, maybe one service has announced 24 hours of downtime (hopefully, you're lucky enough to have that announced!) so during that time you want to switch to another online provider for which you've implemented an instance of IOnlineSpellService.
Suppose you wanted to implement a spelling correction service. Input a word/phrase and out pops a corrected version of the word/phrase. What if you were depending on a large, online provider that went down for a day.
Ideally, you would implement a backup in case the request to said provider failed. In that backup, you might call your secondary service and hope they're online. Maybe in a last ditch effort in the event they're both down, you implement an offline provider to correct spelling based on a local dictionary that exists on the file system.
How do you manage multiple providers like this? Before public class SpellCheck { public string FixText(string text) { string CorrectedText = string. Empty; // create HTTP request to Online Provider #1 // return CorrectedText; } } After public class SpellCheck { private readonly IOnlineSpellService _onlineSpell; private readonly IOfflineSpellService _offlineSpell; public SpellCheck(IOnlineSpellService onlineSpell, IOfflineSpellService offlineSpell) { this.
_onlineSpell = onlineSpell; this. _offlineSpell = offlineSpell; } // same method as before, just a slightly different implementation public string FixText(string text) { string fixedText = _onlineSpell. FixText(text); if(_onlineSpell.
Failed) fixedText = _offlineSpell. FixText(text); return fixedText; } } Of course, you'd need to implement the interface ISpellOnlineService (and similar interface for ISpellOfflineService public interface ISpellOnlineService { public bool Failed { get; set; } public string FixText(string text); } Next, implement your spelling providers (both online and offline, or even multiple instances of both to gain more flexibility) public class OnlineProviderNo1 : IOnlineSpellService { public string FixText(string text) { // implement online logic this. Failed = false; } } Finally, register your services by overriding the CreateKernel method and subclassing your MvcApplication from NinjectHttpApplication.
And apparently with Ninject you replace Application_Start with an override called OnApplicationStarted like so: **This done in global. Asax public class MvcApplication : NinjectHttpApplication { // ... protected override void OnApplicationStarted() { base. OnApplicationStarted(); AreaRegistration.
RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters. Filters); RegisterRoutes(RouteTable. Routes); } protected override IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind().To(); kernel.Bind().To(); return kernel; } } Yes, this is a real world example, as I've implemented it (I actually used Castle Windsor, but it's the identical concept with Ninject--just adapted above) with 3 online providers and 2 offline providers.In CreateKernel you may choose to include logic to examine a database, config file, etc to determine which provider to register.
For example, maybe one service has announced 24 hours of downtime (hopefully, you're lucky enough to have that announced! ) so during that time you want to switch to another online provider for which you've implemented an instance of IOnlineSpellService.
Remo Gloor My mistake--I copied that out of a test case. – David Jun 3 at 20:10 better but still wrong: The kernel will still be collected when the Application_Start method scope is left. Rename the method the CreateKernel and return the kernel as it should normally be done by MVC Apps using Ninject.
See github. Com/ninject/ninject.web. Mvc/wiki/… – Remo Gloor Jun 3 at 20:19 @Remo Gloor Wow--didn't realize all that had to be for Ninject.
Thanks for the fix – David Jun 3 at 20:20 Amazing! Thank you so much for taking the time to convert this code into an answer. I believe I am starting to get the idea of extracting an interface... – bulltorious Jun 3 at 23:16 @bulltorious No problem--Thanks to Remo as well for pointing out the syntactical and architectural differences between Ninject and Castle Windsor – David Jun 37 at 16:45.
The MVC StoreFront series covers dependency injection using StructureMap. The project starts off without IoC and adds it later on in episode #13: ASP. NET MVC Storefront Part 13: Dependency Injection.
Exactly the kind of stuff I was looking for. It is a good video, but it loses rhythym at the end they seem to tangent..., but another good example for me to study. – bulltorious Jun 3 at 23:15.
Finally, register your services by overriding the CreateKernel method and subclassing your MvcApplication from NinjectHttpApplication. **This done in global. Yes, this is a real world example, as I've implemented it (I actually used Castle Windsor, but it's the identical concept with Ninject--just adapted above) with 3 online providers and 2 offline providers.
In CreateKernel you may choose to include logic to examine a database, config file, etc to determine which provider to register.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.