Karan
Karan

Reputation: 15104

Pass an interface object to constructor or method

I was writing a test, and I had to pass a mock into a particular method. I was wondering, is there any benefit of passing it via the constructor or directly into the method in question. Or does it not really matter.

For example

Passing interface/mock via the constructor

class User()
{
  IClock clock;

  User(IClock clock) {this.clock = clock;}

  User GetUser(){ ..}
  UpdateUser(User user) {
     ... 
     this.clock.Now();
     ...
  }
}

vs

Passing interface/mock into the method

class User()
{
  User GetUser(){ ..}
  UpdateUser(IClock clock, User user) {
     ... 
     clock.Now();
     ...
  }
}

Thanks!

EDIT

In this case, IClock will be wrapping DateTime. I do this for testability. So effectively, I will have an override that will construct the IClock within itself.

EG. For the method case:

UpdateUser(User user) {
  UpdateUser(new Clock(), user);
}

Clock will encapsulate DateTime.

Upvotes: 2

Views: 692

Answers (3)

lexicalscope
lexicalscope

Reputation: 7328

IClock in this case is a hidden singleton - it produces the current date-time from the underlying operating system. If you want to write reliable acceptance tests you will need to stub out the date-time provider in those tests.

Because of this you will need to expose this dependency all the way up to the module boundary, and then inject a common date-time source into your module (and perhaps even the whole system). You will sometimes stub this "IClock" date time source when writing your acceptance tests.

This will allow you to answer questions such as:

  • does my system work on a leap day?
  • does my system work at midnight?
  • does my system work on new-year's eve?

Having the IClock dependency in either the constructor signature or the method signature will clearly document to the user that your method or object is date-time dependent. Dependence on date-time is normally of critical importance when trying to understand the behaviour of an object so should be given prominence. Typically avoid anything that hides this dependency (such as supplying a default date-time source in an overloaded method definition) as this will prevent you from writing acceptance tests like those that I have described above.

Choosing between passing a dependency into the constructor or the method will typically depend on the responsibilities of the method to the rest of the system. The methods form the public protocol that the object will use to communicate with its peers in the running system, whereas the constructor parameters are the dependencies that this particular implementation needs to perform its role.

Ask yourself the questions:

  • Does the responsibility of this method necessarily entail knowledge of the date-time?
  • Or is it something to do with the particular implementation of the method in this particular object that means it needs the date-time?
  • Would it be reasonable for the caller of the method to be responsible for manipulating this object's view of the date-time, or is that the responsibility of the module configuration?
  • If I swapped this object with another implementation of the same interface, would it be reasonable that that implementation did not need the date-time for this method?
  • How many methods in your object need the date-time? Do they need the same view of the date-time?

Upvotes: 3

Akim
Akim

Reputation: 8679

Passing dependency (i.e. IClock) to constuctor, i.e constructor injection usually means that this is required dependency for whole object of given class. Passing dependency into method of class, i.e method injection make only this particular method to be dependent on it. There are also two other possiblities: inject dependency to property (property injection, i.e. optional dependency for object of class) and rarely used ambient context.

So this is more question of taste, design and agreement of code style in your team.

In your example IClock dependency should not be required for object of User, so method injection is more appropriate.

Upvotes: 1

casablanca
casablanca

Reputation: 70701

It depends on who knows about IClock: is it available where the User is constructed or is it available where UpdateUser is called?

If you pass it to the constructor, you can then pass the User object around and anyone can call UpdateUser, but if you pass it through UpdateUser, anyone who uses that method must hold an IClock reference.

Upvotes: 1

Related Questions