Quick Exercise in Dependency Injection

I like the reusability with DI
& once implemented it really makes for cleaner code out on the front end.

Here is my SalesPlanning report which checks if sales Threshold has been reached.

public interface ISalesReport
{
bool AchievedSales();
}

public class SalesReport : ISalesReport
{
public bool AchievedSales()
{
return DTX.AreSalesAboveQuota(); //A boolean result for Threshold reached
}
}

public class SalesPlanning
{
private ISalesReport _sales;

public SalesPlanning(ISalesReport )
{
_sales = sales;
}

public void CheckSales()
{
bool result = _sales.AchievedSales();
if (result)
Console.WriteLine(‘Ring the Bell’);
else
Console.WriteLine(‘Play Cold Winter Breeze MP3’);
}
}
class Program
{
static void Main(string[] args)
{
SalesPlanning _planning = new SalesPlanning(new SalesReport());
_planning.CheckSales();
}
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s