i never use Dependency Injection in apps. i go through few article with Dependency Injection and found the concept is interesting but believe hard to implement in real life. now i want to implement Dependency Injection in my win form apps.
our company work with many shipping company like UPS, Fedex, Purolator etc for now but in future they have plan to works with many other shipping company. i have developed separate separate class library projects for all those shipping company like UPS, Fedex, Purolator and include those dll into our main form apps. the problem is many time we hard code few things in our code like country code etc.
for example i have one form where 4 buttons are there. like those buttons are “Ship with UPS WorldShip”, another button there called “Ship with UPS WebAPI”, another button there called “Ship with FedEX Desktop Apps” and another last button called “Ship with Fedex WebAPI”.
when user click on UPS WorldShip button then a flat file generate in a folder.
when user click on UPS WebAPI button then a request goes to UPS site.
when user click on FedEX WinApps button then a flat file generate in a folder.
when user click on FedEX WebAPI button then a request goes to FedEx site.
so what i do now when user click on any button then i call a specific function exist in dll to complete the task.
everything is working fine at my end but the problem is when our company start working with another new shipping company then i have to create another class library for that company.
i said i never use DI ever in my apps and have no experience. so some one guide me how could i handle my situation with DI as a result when a new shipping company will join then i do not have to write any extra code. so guide me how to implement DI in my apps and also guide me with how to handle my situation with sample DI code for guidance.
My second phase question
1) when i need to call InitializeKernel() function ? when application load or when form load
2) i am not familiar with ninject so i just do not understand what is the meaning of this line of code
.Configure((b, c) =>
b.InTransientScope().Named(c.Name)));
3) what c.Name would return?
4) i found no config file entry. ninject does not require config file entry like unity DI?
the code u gave looks very professional. if possible please answer all my points.
at last tell me is there any pdf available for ninject for learning DI and ninject code usage.
thanks
MY 3rd phase of question
very sorry and u r right InitializeKernel() declare once.
u said Ninject supports fluent configuration only then a problem could occur because when a new shipping company join then i have to change code with in this block like
using(var kernel = InitializeKernel())
{
// 4.1 resolve delivery services by names
var upsWorldShip = kernel.Get<IShippingCompanyService>("ShippingUpsWorldShip");
var fedExDesktopApps = kernel.Get<IShippingCompanyService>("ShippingFedExDesktopApps");
var PurolatorExDesktopApps = kernel.Get<IShippingCompanyService>("PurolatorFedExDesktopApps");
// 4.2 delivery processing
upsWorldShip.Delivery();
fedExDesktopApps.Delivery();
// 5 PROFIT!
}
so here i need to add this line
var PurolatorExDesktopApps = kernel.Get<IShippingCompanyService> ("PurolatorFedExDesktopApps");
the problem is whenever a new shipping related dll will be added then i have to add one line of code in the above block….that is not desirable.
if i could add all dll’s related info in config file and load & instantiated all classed from all dll from there it would be better. so i am looking for ur suggestion. thanks
Seems that you’re asking for Configuration by Convention. This means that you identify groups of components that should share a common configuration and then specify that configuration in a single statement once read more about it
In practice, this mean that you’ll deploy all libraries with selected functionality “delivery” into “special” location in your project, and inherit all implementation from “special” interface. Finally, make DI container to find them and configure all for you.
Define Convention
Here is a simple example how to achieve this using
Ninject.Extensions.Conventions.Pay an attention to comments
Define Configuration
Build configuration using Ninject Kernel (
StandardKernelin this case)How to resolve dependencies
From Composition root of application resolve delivery services by names
All sources available here
Summary
Configuration by Convention is very helpful approach already adopted in many projects. Nevertheless, I recomment you to read Mark Seemann book “Dependency Injection in .NET” and watch his talk about conventions.
Answers
.Configure((b, c) => b.InTransientScope().Named(c.Name)))c.Namewould return?InitializeKernel()the 2ndInitializeKernel()is for what?InitializeKernel()