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();
}