7. Terms Service —An object that performs a well-defined function when called upon Client—Any consumer of a service; an object that calls upon a service to perform a well-understood function
8. Terms Dependency —A specific service that is required by another object to fulfill its function. Dependent—A client object that needs a dependency (or dependencies) in order to perform its function.
11. Ex1:Composition public class SpellCheckerService{} public class TextEditor { private SpellCheckerService _spellCheckerService; public TextEditor() { _spellCheckerService = new SpellCheckerService(); } } class Program { static void Main(string[] args) { TextEditor textEditor = new TextEditor(); } }
24. What’s bad You have to maintain factory or service locator Dependent is still looking for Dependencies by himself. Dependencies are encapsulated and are not obvious.
25. Service Locator Unfortunately, being a kind of Factory, Service Locators suffer from the same problems of testability and shared state.
26. Inversion of Control Common Flow TextEditor creates its dependency by himself. IoC Flow TextEditor requests factory to create dependency for him.
28. Inversion of Control IoC – is a common characteristic of frameworks. Inversion of Control serves as a design guideline. According to Martin Fowler the etymology of the phrase dates back to 1988.
29. Two principles of IOC Main classes aggregating other classes should not depend on the direct implementation of the aggregated classes. Both the classes should depend on abstraction. Abstraction should not depend on details, details should depend on abstraction.
32. Ex3:Dependency Injection public class TextEditor { private readonlyISpellCheckerService _spellCheckerService; public TextEditor(ISpellCheckerServicespellCheckerService) { _spellCheckerService = spellCheckerService; } public string CheckSpelling() { return _spellCheckerService.CheckSpelling(); } }
33. Ex3: Unit Testing DI Almost same as for Ex2 // Mock ISpellCheckerService mock = new SpellCheckerServiceMock(); // Instantiate TextEditortextEditor = new TextEditor(mock); // Check Assert.AreEqual(“Mock”, textEditor.CheckSpelling());
34. What changed TextEditor lost its “Sovereignty” and is not able to resolve dependencies by himself.
35. What’s good Dependencies are obvious. Dependency resolution is not encapsulated. Unit Testing got little bit easier Architecture is much better
36. What’s bad We are resolving dependencies manually while creating instances of TextEditor.
38. Dependency Injection DI is IoC Inversion of Control is too generic a term DI pattern – describes the approach used to lookup a dependency. Dependency resolution is moved to Framework.
47. Ex6: Dependency tree public interface IAdditionalDependency{} public class AdditionalDependency : IAdditionalDependency{} public class SpellCheckingService: ISpellCheckerService { public SpellCheckingService( IAdditionalDependency dependency){} }
48. Ex6: Dependency Tree UnityContainer container = new UnityContainer();container.RegisterType<ISpellCheckerService, SpellCheckingService>();container.RegisterType<IAdditionalDependency, AdditionalDependency>();TextEditor textEditor = container.Resolve<TextEditor>();
51. Ex7: Defining Injection Constructor public class TextEditor { private readonly ISpellCheckerService _spellCheckerService; [InjectionConstructor] public TextEditor(ISpellCheckerService spellCheckerService) { _spellCheckerService = spellCheckerService; } public TextEditor(ISpellCheckerService spellCheckerService,string name) { _spellCheckerService = spellCheckerService; } }
52. Ex8: Property Injection public class TextEditor { public ISpellCheckerService SpellCheckerService {get; set;} [Dependency] public ISpellCheckerService YetAnotherSpellcheckerService{get;set;} } UnityContainer container = new UnityContainer(); container.RegisterType<TextEditor>(new InjectionProperty("SpellCheckerService")); container.RegisterType<ISpellCheckerService, SpellCheckingService>(); TextEditor textEditor = container.Resolve<TextEditor>();
53. Ex9: Method call injection public class TextEditor { public ISpellCheckerService SpellcheckerService {get; set;} [InjectionMethod] public void Initialize (ISpellCheckerService spellcheckerService) { _spellCheckerService = spellcheckerService; } } UnityContainer container = new UnityContainer();//container.RegisterType<TextEditor>( new InjectionMethod("SpellcheckerService"));container.RegisterType<ISpellCheckerService, SpellCheckingService>();TextEditor textEditor = container.Resolve<TextEditor>();
54. Lifetime Managers TransientLifetimeManagerReturns a new instance of the requested type for each call. (default behavior) ContainerControlledLifetimeManagerImplements a singleton behavior for objects. The object is disposed of when you dispose of the container.
55. Lifetime Managers ExternallyControlledLifetimeManagerImplements a singleton behavior but the container doesn't hold a reference to object which will be disposed of when out of scope. HierarchicalifetimeManagerImplements a singleton behavior for objects. However, child containers don't share instances with parents.
56. Lifetime Managers PerResolveLifetimeManagerImplements a behavior similar to the transient lifetime manager except that instances are reused across build-ups of the object graph. PerThreadLifetimeManagerImplements a singleton behavior for objects but limited to the current thread.