Unity Application Block 1.0 series (4): Method invocation Injection (methods call injection)

Source: Internet
Author: User

Under what circumstances use method callinjection

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 parent object has many constructors that are related to each other, resulting in inconvenient debugging and maintenance

The parent object contains a number of parameter constructors, especially if the parameter types are similar only through the position of the parameter

Hide dependent objects, not exposed as attributes

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

Preparatory work

public interface IPlayer
{
  void Play();
}

public class Mp3Player : IPlayer
{
  public Song mSong;

  [InjectionMethod]
  public void Init(Song song)
  {
    this.mSong = song;
  }

  public void Play()
  {
    Console.WriteLine(string.Format("{0}: Now Playing [{1}] Singing by ({2})", this.Name, this.mSong.Name, this.mSong.Singer));
  }

  public string Name
  {
    get
    {
      return "Mp3 Player";
    }
  }
}

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.