Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions DependencyInjection/DIMedia.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
21 changes: 21 additions & 0 deletions DependencyInjection/DevicePlayer/CDPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace DIMedia.DevicePlayer
{
using DIMedia.Media;
using System;

public class CDPlayer : DevicePlayer
{
private readonly MediaType mediaType = MediaType.CDDisk;

public CDPlayer(MediaContent mediaContent) :
base(mediaContent)
{
if (this.mediaType != mediaContent.MediaType)
{
throw new Exception($"{this.GetType().Name} cannot play {mediaContent.MediaType}.");
}

Console.WriteLine($"Loading {mediaContent.MediaType} in {this.GetType().Name}...");
}
}
}
21 changes: 21 additions & 0 deletions DependencyInjection/DevicePlayer/CasettePlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace DIMedia.DevicePlayer
{
using DIMedia.Media;
using System;

public class CasettePlayer : DevicePlayer
{
private readonly MediaType mediaType = MediaType.Tape;

public CasettePlayer(MediaContent mediaContent) :
base(mediaContent)
{
if (this.mediaType != mediaContent.MediaType)
{
throw new Exception($"{this.GetType().Name} cannot play {mediaContent.MediaType}.");
}

Console.WriteLine($"Loading {mediaContent.MediaType} in {this.GetType().Name}...");
}
}
}
21 changes: 21 additions & 0 deletions DependencyInjection/DevicePlayer/DVDPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace DIMedia.DevicePlayer
{
using DIMedia.Media;
using System;

public class DVDPlayer : DevicePlayer
{
private readonly MediaType mediaType = MediaType.DVDDisk;

public DVDPlayer(MediaContent mediaContent) :
base(mediaContent)
{
if (this.mediaType != mediaContent.MediaType)
{
throw new Exception($"{this.GetType().Name} cannot play {mediaContent.MediaType}.");
}

Console.WriteLine($"Loading {mediaContent.MediaType} in {this.GetType().Name}...");
}
}
}
33 changes: 33 additions & 0 deletions DependencyInjection/DevicePlayer/DevicePlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using DIMedia.Media;
using System;

namespace DIMedia.DevicePlayer
{
public abstract class DevicePlayer : IDevicePlayer
{
protected readonly MediaContent mediaContent;

public DevicePlayer(MediaContent mediaContent)
{
this.mediaContent = mediaContent ?? throw new ArgumentNullException(nameof(mediaContent));
}

public virtual void Play()
{
Console.WriteLine($"Playing {mediaContent.Title} with {this.GetType().Name}.");

foreach (string item in mediaContent.Content)
{
Console.WriteLine($"Playing {item}...");
}

Console.WriteLine($"Eject {mediaContent.MediaType}...");
Console.WriteLine();
}

public virtual void Pause()
{
Console.WriteLine($"Pause.");
}
}
}
8 changes: 8 additions & 0 deletions DependencyInjection/DevicePlayer/IDevicePlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DIMedia.DevicePlayer
{
public interface IDevicePlayer
{
void Pause();
void Play();
}
}
27 changes: 27 additions & 0 deletions DependencyInjection/Injector/DevicePlayerSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace DIMedia.Selector
{
using DIMedia.DevicePlayer;
using DIMedia.Media;
using DIMedia.Player;

public class DevicePlayerSelector : IDevicePlayerSelector
{
public IMediaPlayer GetDevicePlayer(MediaContent mediaContent)
{
switch (mediaContent.MediaType)
{
case MediaType.Tape:
return new MediaPlayer(new CasettePlayer(mediaContent));

case MediaType.CDDisk:
return new MediaPlayer(new CDPlayer(mediaContent));

case MediaType.DVDDisk:
return new MediaPlayer(new DVDPlayer(mediaContent));

default:
return null;
}
}
}
}
10 changes: 10 additions & 0 deletions DependencyInjection/Injector/IDevicePlayerSelector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace DIMedia.Selector
{
using DIMedia.Media;
using DIMedia.Player;

public interface IDevicePlayerSelector
{
IMediaPlayer GetDevicePlayer(MediaContent mediaContent);
}
}
11 changes: 11 additions & 0 deletions DependencyInjection/Media/MediaContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DIMedia.Media
{
using System.Collections.Generic;

public class MediaContent
{
public MediaType MediaType { get; set; }
public object Title { get; internal set; }
public List<string> Content { get; set; }
}
}
11 changes: 11 additions & 0 deletions DependencyInjection/Media/MediaType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DIMedia.Media
{
public enum MediaType
{
Vinyl,
Tape,
CDDisk,
DVDDisk,
Digital
};
}
7 changes: 7 additions & 0 deletions DependencyInjection/Player/IMediaPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DIMedia.Player
{
public interface IMediaPlayer
{
void PlayMediaContent();
}
}
33 changes: 33 additions & 0 deletions DependencyInjection/Player/MediaPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace DIMedia.Player
{
using DIMedia.DevicePlayer;

public class MediaPlayer : IMediaPlayer
{
// this line could be uncommented in the case of constructor/method injection
// private IDevicePlayer devicePlayer;

// in case of constructor/method, the setter should be private
public IDevicePlayer DevicePlayer { get; set; }

public MediaPlayer()
{ }

// with constructor
public MediaPlayer(IDevicePlayer devicePlayer)
{
this.DevicePlayer = devicePlayer;
}

// with method
public void Register(IDevicePlayer devicePlayer)
{
this.DevicePlayer = devicePlayer;
}

public void PlayMediaContent()
{
this.DevicePlayer.Play();
}
}
}
83 changes: 83 additions & 0 deletions DependencyInjection/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
namespace DIMedia
{
using DIMedia.DevicePlayer;
using DIMedia.Media;
using DIMedia.Player;
using DIMedia.Selector;
using System.Collections.Generic;

class Program
{
static void Main()
{
MediaContent tapeContent = new MediaContent()
{
MediaType = MediaType.Tape,
Title = "Oldies, but Goldies!",
Content = new List<string>() {
"1 - Name - Old Song",
"2 - Name - Old Song",
"3 - Name - Old Song"
},
};

MediaContent cdContent = new MediaContent()
{
MediaType = MediaType.CDDisk,
Title = "Jazz",
Content = new List<string>() {
"1 - Name - Jazz Song",
"2 - Name - Jazz Song",
"3 - Name - Jazz Song"
},
};

MediaContent dvdContent = new MediaContent()
{
MediaType = MediaType.DVDDisk,
Title = "Rock!",
Content = new List<string>() {
"1 - Name - Rock Song",
"2 - Name - Rock Song",
"3 - Name - Rock Song"
},
};

// using the injector class
IDevicePlayerSelector playerSelector = new DevicePlayerSelector();

// get the player by content
IMediaPlayer mediaCasettePlayer = playerSelector.GetDevicePlayer(tapeContent);
mediaCasettePlayer.PlayMediaContent();

IMediaPlayer mediaCdPlayer = playerSelector.GetDevicePlayer(cdContent);
mediaCdPlayer.PlayMediaContent();

IMediaPlayer mediaDvdPlayer = playerSelector.GetDevicePlayer(dvdContent);
mediaDvdPlayer.PlayMediaContent();

// constructor injection
CasettePlayer casettePlayer = new CasettePlayer(tapeContent);

// registration
IMediaPlayer mediaPlayer = new MediaPlayer(casettePlayer);
mediaPlayer.PlayMediaContent();

// method injection
CDPlayer cdPlayer = new CDPlayer(cdContent);
MediaPlayer anotherMediaPlayer = new MediaPlayer();
// registration
anotherMediaPlayer.Register(cdPlayer);
anotherMediaPlayer.PlayMediaContent();

// setter injection
DVDPlayer dvdPlayer = new DVDPlayer(dvdContent);
// registration
MediaPlayer newMediaPlayer = new MediaPlayer()
{
DevicePlayer = dvdPlayer,
};
newMediaPlayer.PlayMediaContent();
}
}
}