Book Image

Learning NServiceBus - Second Edition

By : David Boike
Book Image

Learning NServiceBus - Second Edition

By: David Boike

Overview of this book

Table of Contents (18 chapters)
Learning NServiceBus Second Edition
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Dependency injection


You already know that you can use the BusConfiguration instance provided by IConfigureThisEndpoint or INeedInitialization to set many of the options introduced in Chapter 4, Hosting, which gives you a great amount of control over how the bus operates, but you can also make your own customizations. This will usually involve dependency injection.

Let's say that we want to be able to create unit tests in order to verify how our handlers react to time. Testing with code that is time dependent is difficult since the time is always changing. So, instead of using DateTime.Now or DateTime.UtcNow directly, we'll create an interface to abstract the implementation of retrieving the current time and an implementation class that will provide the true time when we're not running a test:

public interface ITimeProvider
{
   DateTime Now { get; }
   DateTime UtcNow { get; }
}

public class DateTimeProvider : ITimeProvider
{
    public DateTime Now
    {
        get { return DateTime.Now...