Click here to Skip to main content
15,897,187 members
Home / Discussions / C#
   

C#

 
GeneralRe: Unit Testing a Singleton Pin
Dustin Metzgar11-Jul-06 6:22
Dustin Metzgar11-Jul-06 6:22 
GeneralRe: Unit Testing a Singleton Pin
Jun Du11-Jul-06 7:07
Jun Du11-Jul-06 7:07 
GeneralRe: Unit Testing a Singleton Pin
Kevin McFarlane11-Jul-06 6:17
Kevin McFarlane11-Jul-06 6:17 
GeneralRe: Unit Testing a Singleton Pin
Colin Angus Mackay11-Jul-06 13:01
Colin Angus Mackay11-Jul-06 13:01 
GeneralRe: Unit Testing a Singleton Pin
Judah Gabriel Himango11-Jul-06 13:08
sponsorJudah Gabriel Himango11-Jul-06 13:08 
GeneralRe: Unit Testing a Singleton Pin
Colin Angus Mackay11-Jul-06 22:50
Colin Angus Mackay11-Jul-06 22:50 
GeneralRe: Unit Testing a Singleton Pin
Judah Gabriel Himango12-Jul-06 6:52
sponsorJudah Gabriel Himango12-Jul-06 6:52 
AnswerRe: Unit Testing a Singleton Pin
Judah Gabriel Himango11-Jul-06 5:47
sponsorJudah Gabriel Himango11-Jul-06 5:47 
Colin Angus Mackay wrote:
So, for the purposes of unit testing it would be useful to have more than one instance of the class available. Or perhaps there is another solution.


I'm not sure this solution will work for you, Colin, but one thing I've done for classes that are typically single instance only classes is something like this:

public class SomeClass()
{ 
    private static SomeClass instance = new SomeClass();
 
    private SomeClass()
    {
    }

    public SomeClass Current
    {
        get
        {
            return instance;
        }
    }
}


This way, SomeClass can still implement interfaces necessary for using mocks (whereas static classes cannot implement interfaces), it can be instanciated (using reflection or Activator.CreateInstance), and is quite easier to test than a pure static class. In fact, I only use static classes for places that have totally, 100% "functional" functions; where each function relies only on itself or other functions in the same static class; no variables or shared state at all. If you find yourself requiring lots of state and shared data; go with an instance class, IMO.

If you need static access to the instance class and you need only 1 instance of the class, the above code works well.


Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Messianic Instrumentals (with audio)
The apostle Paul, modernly speaking: Epistles of Paul

Judah Himango


AnswerInherit the singleton Pin
Ennis Ray Lynch, Jr.11-Jul-06 6:08
Ennis Ray Lynch, Jr.11-Jul-06 6:08 
Questionfont script Pin
TAREQ F ABUZUHRI11-Jul-06 4:45
TAREQ F ABUZUHRI11-Jul-06 4:45 
AnswerRe: font script Pin
mav.northwind11-Jul-06 4:46
mav.northwind11-Jul-06 4:46 
GeneralRe: font script Pin
TAREQ F ABUZUHRI11-Jul-06 4:52
TAREQ F ABUZUHRI11-Jul-06 4:52 
GeneralRe: font script Pin
Christian Graus11-Jul-06 5:13
protectorChristian Graus11-Jul-06 5:13 
GeneralRe: font script Pin
mav.northwind11-Jul-06 21:37
mav.northwind11-Jul-06 21:37 
GeneralRe: font script Pin
TAREQ F ABUZUHRI11-Jul-06 4:59
TAREQ F ABUZUHRI11-Jul-06 4:59 
GeneralRe: font script Pin
Werdna11-Jul-06 6:03
Werdna11-Jul-06 6:03 
GeneralRe: font script Pin
TAREQ F ABUZUHRI11-Jul-06 16:06
TAREQ F ABUZUHRI11-Jul-06 16:06 
QuestionIntercepting and blocking Webpages Pin
CJayMeister11-Jul-06 2:41
CJayMeister11-Jul-06 2:41 
AnswerRe: Intercepting and blocking Webpages Pin
J4amieC11-Jul-06 2:54
J4amieC11-Jul-06 2:54 
GeneralRe: Intercepting and blocking Webpages Pin
CJayMeister11-Jul-06 3:09
CJayMeister11-Jul-06 3:09 
GeneralRe: Intercepting and blocking Webpages Pin
Eran Aharonovich11-Jul-06 4:44
Eran Aharonovich11-Jul-06 4:44 
AnswerRe: Intercepting and blocking Webpages Pin
led mike11-Jul-06 5:22
led mike11-Jul-06 5:22 
AnswerCreate a proxy Pin
Ennis Ray Lynch, Jr.11-Jul-06 9:51
Ennis Ray Lynch, Jr.11-Jul-06 9:51 
AnswerProxy server Pin
Daniel Turini11-Jul-06 9:51
Daniel Turini11-Jul-06 9:51 
Questionlock() and Thread.Interrupt() Pin
Jon Hulatt11-Jul-06 0:37
Jon Hulatt11-Jul-06 0:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.