c# - passing timer through dependency injection and configuring / starting a timer -


i have situation here, need timer in class invokes method based on time interval.

on normal scenarios have instantiated timer , configured in constructor itself. want in dependency injection style. passing timer in constructor easy, in order bind method ontimeelapsed tricky, should factory instantiating class, configure timer? how should go ahead without violating dpendency injection principles.

thanks

edit1

ok, let me rephrase wanted ask.

  1. according videos posted misko
  2. during other talks (never test framework: .net framework here)
  3. in code review guide done beyond field assignments in constructor considered bad.

my main goal of achieving dependency injection need unit test on particular elapsed time whether method has been called or not.

so question is: should bind ontimerelapsed event? trying test timer here? seem lost here

kindly help.

if question is, want dependency inject timer can later bind event to, should simple.

just create timer class follow itimer interface , create methods on actions want.

public class calendar {     public calendar(itimer timer)     {         // timer dependency injected timer         timer.setevent(eventreminder, 3600);     }      public void eventreminder()     {         console.write("hey, it's time appointment!");     }  }  public interface itimer {     void setevent(action callbackmethod, int interval); } 

in case, have calendar application, , want application have timer. don't care how timer works, or kind of timer (maybe want timer works on minutes, or hours, or works other way). know want timer, dependency inject one.

you have create interface, defines timer do, because although doesn't care timer use, care capabilities of timer. in our case, timer can 1 thing - set event occur after interval.

so inject timer, in order able use it, or set up, use interface define methods. never know how works internally - don't know has onelapsedevent, , don't care. leave creator of timer, want 1 method task, , that's code above demonstrates.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -