Schotime
Schotime

Reputation: 15997

Organising methods for performing tests - C#

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.
    }
}

Upvotes: 0

Views: 145

Answers (2)

abhilash
abhilash

Reputation: 5651

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.. :)

Upvotes: 1

Colin Burnett
Colin Burnett

Reputation: 11548

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?

Upvotes: 0

Related Questions