This is a mirror of official site: http://jasper-net.blogspot.com/

Dependency Injection

| Tuesday, September 14, 2010
  Dependency injection is a technique used in object-oriented programming languages. It promotes loose coupling of components by passing dependencies to an object, rather than having an object instantiate its own dependencies.

What is Dependency Injection?

  Object-oriented design can lead to the creation of structures containing components that are dependent upon other classes, known as dependencies. In such situations, you may decide to instantiate a class' dependencies from within the dependent class, using the new keyword.
   As an example, consider the code below. This defines a class, named "PaymentTerms", that holds the details required to calculate the monthly payments for a loan. The class includes three properties that define the price of an item being purchased, the deposit being paid and the term of the agreement. The GetMonthlyPayment method calculates the monthly fees using a PaymentCalculator object. This calculator is a dependency of PaymentTerms and is initialised when an object is instantiated.
   NB: The operation of the PaymentCalculator class has been oversimplified but this is unimportant for the subject of this article. The properties are defined using C# 3.0 automatically implemented property syntax. For earlier versions of the .NET framework you should expand these and include a backing store.

Read more: BlackWasp

Posted via email from .NET Info

0 comments: