63
votes
9answers
12k views

Implementing a Singleton pattern in C#?

Is this the best way to implement this pattern in C#? public sealed class Singleton { private static readonly Singleton instance = new Singleton(); public static Singleton Instance { get ...
5
votes
1answer
669 views

WPF Single Instance Best Practices

This is the code I implemented so far to create a single instance WPF application: #region Using Directives using System; using System.Globalization; using System.Reflection; using System.Threading; ...
4
votes
2answers
758 views

Threadsafe DBContext in singleton

I found out the hardway that access to DbContext in .NET is not threadsafe. I have a singleton for logging things using a dbcontext. The original version uses something like public Logger{ private ...
4
votes
2answers
446 views

Three-tier application with singleton pattern

I am creating a 3-Tier Windows Forms Application. Questions Am I using the right architecture, or can you suggest a better approach? Is there any way to make the base class be a Singleton? Is ...
3
votes
1answer
108 views

Prime Numbers Store

Problem definition: Lets say we need to create a store for selling prime numbers. Users enter the store and ask to buy a number. If the number asked is a prime number, 1.1. then it's either ...
3
votes
1answer
174 views

How is the design of this Singleton implementation

I did this as an exercise just to practive/improve using generics. Independent of how useful this implementation of a Singleton is, how is my coding in terms of using generics and any other aspect of ...
0
votes
1answer
250 views

How can I improve this code without using static or singleton

I used to use static or singleton for my DAL class. However, I read some articles saying that singleton is evil and should be avoided. Therefore I try to rewrite my code like this: public class ...
0
votes
2answers
71 views

Is this code thread-safe - Singleton Implementation using Concurrent Dictionary

class Connection { private string param1; private string param2; private static readonly ConcurrentDictionary<Tuple<string, string>, Connection> connections = new ...
0
votes
1answer
48 views

adding APL logger to an application [closed]

I have an application (Legacy code) that contains interface Icomponent with save() methods and many classes that implement it. I want to add log after every save(). I thought to use the ...