SlideShare a Scribd company logo
1 of 18
Going towards Inversion of Control and better design Omar AL Zabir
Typical code  public class CustomerManager { public void Insert(Customer customer) { CustomerData data = new CustomerData(); var newCustomer = data.Insert(customer); Logger.Log("New customer inserted" + newCustomer.Name); Mailer.SendWelcomeMail(newCustomer); return newCustomer; } } Here’s a typical example of code we usually do: A business layer class A data access layer class Utility class
Visualize the design
Problems? ,[object Object],[object Object],[object Object]
Solution ,[object Object],class CustomerManager { private  ICustomerData  _customerData; private  ILogger  _logger; private  IMailer  _mailer; public CustomerManager( ICustomerData  customerData,  ILogger  logger,  IMailer  mailer) { _customerData = customerData; _logger = logger; _mailer = mailer; } public void Insert(Customer customer) { _customerData.Insert(cusotmer); _logger.Log("New customer inserted"); _mailer.SendWelcomeMail(customer); } }
Design is much better, less coupling
Principles ,[object Object],[object Object],[object Object]
Pains ,[object Object],[object Object],CustomerManager manager = new CustomerManager( new CustomerData(), new Logger(), new Mailer());
Solution ,[object Object],public class CustomerManager { private ICustomerData _customerData; private ILogger _logger; private IMailer _mailer; public CustomerManager() : this(new CustomerData(), new Logger(), new Mailer()) { } public CustomerManager(ICustomerData customerData, ILogger logger, IMailer mailer) {
Wrong! ,[object Object],[object Object]
What’s a Container ,[object Object],[object Object],[object Object]
Registering in container ,[object Object],Global.asax Application_Start() { Container.Register< ILogger ,  Logger2 >(); Container.Register< ICustomerData ,  CustomerDataLinqToSql >(); }
Using Container ,[object Object],public class CustomerManager { private ICustomerData _customerData; private ILogger _logger; private IMailer _mailer; public CustomerManager()  : this( Container.Resolve<ICustomerData>(), Container.Resolve<ILogger>(), Container.Resolve<IMailer>() ) { } public CustomerManager(ICustomerData customerData, ILogger logger, IMailer mailer) {
Benefits? ,[object Object],[object Object],[object Object],[object Object]
What problems does this solve? ,[object Object],[object Object],[object Object],[object Object],[object Object]
Common reactions to this ,[object Object],[object Object],[object Object],[object Object]
More common reactions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
How do you know you are doing it right? ,[object Object],[object Object],[object Object]

More Related Content

What's hot

An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.jsEmanuele DelBono
 
Test Data Builder Pattern
Test Data Builder PatternTest Data Builder Pattern
Test Data Builder PatternAlan Parkinson
 
Entity Manager
Entity ManagerEntity Manager
Entity Managerpatinijava
 
My journey to use a validation framework
My journey to use a validation frameworkMy journey to use a validation framework
My journey to use a validation frameworksaqibsarwar
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsAleksandar Ilić
 
OOP - Java is pass-by-value
OOP - Java is pass-by-valueOOP - Java is pass-by-value
OOP - Java is pass-by-valueMudasir Qazi
 
Basic vbscript for qtp
Basic vbscript for qtpBasic vbscript for qtp
Basic vbscript for qtpCuong Tran Van
 
React & Redux JS
React & Redux JS React & Redux JS
React & Redux JS Hamed Farag
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JSArno Lordkronos
 
ASPNET_MVC_Tutorial_06_CS
ASPNET_MVC_Tutorial_06_CSASPNET_MVC_Tutorial_06_CS
ASPNET_MVC_Tutorial_06_CStutorialsruby
 
Dot Net Accenture
Dot Net AccentureDot Net Accenture
Dot Net AccentureSri K
 
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019iFour Technolab Pvt. Ltd.
 

What's hot (18)

Vbscript
VbscriptVbscript
Vbscript
 
Vbscript
VbscriptVbscript
Vbscript
 
An introduction to React.js
An introduction to React.jsAn introduction to React.js
An introduction to React.js
 
Test Data Builder Pattern
Test Data Builder PatternTest Data Builder Pattern
Test Data Builder Pattern
 
Ejb6
Ejb6Ejb6
Ejb6
 
Entity Manager
Entity ManagerEntity Manager
Entity Manager
 
My journey to use a validation framework
My journey to use a validation frameworkMy journey to use a validation framework
My journey to use a validation framework
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App Components
 
Vb script
Vb scriptVb script
Vb script
 
OOP - Java is pass-by-value
OOP - Java is pass-by-valueOOP - Java is pass-by-value
OOP - Java is pass-by-value
 
Basic vbscript for qtp
Basic vbscript for qtpBasic vbscript for qtp
Basic vbscript for qtp
 
React & Redux JS
React & Redux JS React & Redux JS
React & Redux JS
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
ASPNET_MVC_Tutorial_06_CS
ASPNET_MVC_Tutorial_06_CSASPNET_MVC_Tutorial_06_CS
ASPNET_MVC_Tutorial_06_CS
 
Dot Net Accenture
Dot Net AccentureDot Net Accenture
Dot Net Accenture
 
Introduction to TDD
Introduction to TDDIntroduction to TDD
Introduction to TDD
 
Vb script
Vb scriptVb script
Vb script
 
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019Meetup  - Getting Started with MVVM Light for WPF - 11 may 2019
Meetup - Getting Started with MVVM Light for WPF - 11 may 2019
 

Similar to Going towards inversion of control and better design

Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The LambdaTogakangaroo
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeNaresh Jain
 
Poject documentation deepak
Poject documentation deepakPoject documentation deepak
Poject documentation deepakchetankane
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with RailsPaul Gallagher
 
Legacy Code Kata v3.0
Legacy Code Kata v3.0Legacy Code Kata v3.0
Legacy Code Kata v3.0William Munn
 
Keeping business logic out of your UIs
Keeping business logic out of your UIsKeeping business logic out of your UIs
Keeping business logic out of your UIsPetter Holmström
 
DDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFrameworkDDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFrameworkbanq jdon
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019Paulo Clavijo
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkAkhil Mittal
 
Flex Maniacs 2007
Flex Maniacs 2007Flex Maniacs 2007
Flex Maniacs 2007rtretola
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardMax Kleiner
 
Advisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScriptAdvisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScriptdominion
 
Builiding Modular monoliths that can scale to microservices. JBCNConf 2021
Builiding Modular monoliths that can scale to microservices. JBCNConf 2021Builiding Modular monoliths that can scale to microservices. JBCNConf 2021
Builiding Modular monoliths that can scale to microservices. JBCNConf 2021David Gómez García
 
Handling errors in t sql code (1)
Handling errors in t sql code (1)Handling errors in t sql code (1)
Handling errors in t sql code (1)Ris Fernandez
 
Dependency Injection in Drupal 8
Dependency Injection in Drupal 8Dependency Injection in Drupal 8
Dependency Injection in Drupal 8katbailey
 

Similar to Going towards inversion of control and better design (20)

Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Clean code
Clean codeClean code
Clean code
 
Poject documentation deepak
Poject documentation deepakPoject documentation deepak
Poject documentation deepak
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with Rails
 
Mvc acchitecture
Mvc acchitectureMvc acchitecture
Mvc acchitecture
 
Legacy Code Kata v3.0
Legacy Code Kata v3.0Legacy Code Kata v3.0
Legacy Code Kata v3.0
 
Keeping business logic out of your UIs
Keeping business logic out of your UIsKeeping business logic out of your UIs
Keeping business logic out of your UIs
 
Instant DBMS Homework Help
Instant DBMS Homework HelpInstant DBMS Homework Help
Instant DBMS Homework Help
 
DDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFrameworkDDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFramework
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
 
Flex Maniacs 2007
Flex Maniacs 2007Flex Maniacs 2007
Flex Maniacs 2007
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO Standard
 
Advisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScriptAdvisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScript
 
Builiding Modular monoliths that can scale to microservices. JBCNConf 2021
Builiding Modular monoliths that can scale to microservices. JBCNConf 2021Builiding Modular monoliths that can scale to microservices. JBCNConf 2021
Builiding Modular monoliths that can scale to microservices. JBCNConf 2021
 
Handling errors in t sql code (1)
Handling errors in t sql code (1)Handling errors in t sql code (1)
Handling errors in t sql code (1)
 
Dependency Injection in Drupal 8
Dependency Injection in Drupal 8Dependency Injection in Drupal 8
Dependency Injection in Drupal 8
 

Going towards inversion of control and better design

  • 1. Going towards Inversion of Control and better design Omar AL Zabir
  • 2. Typical code public class CustomerManager { public void Insert(Customer customer) { CustomerData data = new CustomerData(); var newCustomer = data.Insert(customer); Logger.Log(&quot;New customer inserted&quot; + newCustomer.Name); Mailer.SendWelcomeMail(newCustomer); return newCustomer; } } Here’s a typical example of code we usually do: A business layer class A data access layer class Utility class
  • 4.
  • 5.
  • 6. Design is much better, less coupling
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.