views:

107

answers:

2

I have a class at the moment and within this class it has 15 or so private methods that perform certain tasks on a loop run by a timer. Some of these methods call of to the database and some don't.

The question is...how can I arrange these so that I can setup the class so I can fake a repos or executing process??

This is a simplified version of what I have now.

public class Manager : IManager
{
    System.Timers.Timer tm;
    private bool runningAsService;
    private List<Database> Databases = new List<Database>();
    private LogClass Log;

    public Manager(bool RunningAsService, LogClass log)
    {
        runningAsService = RunningAsService;
        Log = log;

        tm = new System.Timers.Timer(Settings.idle_time * 1000);
        tm.Elapsed += new System.Timers.ElapsedEventHandler(delegate { PerformDuties(); });
    }

    public void Start()
    {
        tm.Start();
        PerformDuties();
    }

    private PerformDuties()
    {
        //Call the other 10-15 private methods to perform all the tasks needed.
    }
}
A: 

If I understand your question correctly then this sounds like a need for dependency injection where you supply the objects Manager depends on to do its work. They would be real objects (an actual database connection) or a fake one that just acts like it.

Am I understanding your question correctly?

Colin Burnett
Yeah...think so....just not sure how to structure it so that I don't have to hit a db to test it.
Schotime
+1  A: 

Each Db operation would end being either of CRUD operations. So, you can extract out IDatabase from your Database class in other words abstract out your Data Access Layer, to something like this:

public interface IDatabase<T> 
{
     T CreateItem();
     T ReadItem(Id id);
     bool UpdateItem(T item);
     bool DeleteItem(T item)
}

And inject List of Databases using Dependency Injection using a DI framework like Castle Windsor, Spring.NET etc either by Setter or Ctor injections to your Manager class

Hope this makes sense.. :)

AB Kolan
I understand the DB side its more bout the other methods so that if i have a ITaskRunner that i pass into the ctor and it also needs the log, then does it need to also pass the instance of the log to the TaskRunner as well as the Manager??
Schotime