Unity Application Block 1.0 series (2): Construction sub Injection (constructor injection)

Source: Internet
Author: User
Tags constructor require

Under what circumstances use constructor injection

Automatic instantiation of dependent objects when the parent object is instantiated

Easy way to see the items that each class relies on in your code

The constructor of the parent object does not require many constructors that have an associated relationship with each other

The constructor for the parent object does not require many parameters

To encapsulate a field value so that it cannot be seen by exposing it to the calling program without using properties and methods

Control which objects can be injected without altering the parent object or application by modifying the dependent object's code

Preparatory work

public class Song
{
  public string Singer
  {
    get
    {
      return "Westlife";
    }
  }

  public string Name
  {
    get
    {
      return "My Love";
    }
  }
}

public class Owner
{
  public string Name
  {
    get
    {
      return "Inrie";
    }
  }

  public int Age
  {
    get
    {
      return 24;
    }
  }
}

public interface IPlayer
{
  void Play();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.