Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to Implement a Singleton class

$
0
0

This example demonstrates how to implement a singleton - a class that can have a single instance that cannot be removed. For details, refer to the How to: Implement a Singleton Class and Show its Detail View topic.

Question Comments

Added By: Andrey Kucher 1 at: 12/15/2014 6:29:57 AM    

I have found a bug for this singleton implementation:
If you have no records in db follow code create several records:
Singleton.GetInstance(session); // For example for default creation
Singleton.GetInstance(session); // For exapmle for set some settings

So I have fixed it with this code:
public static Singleton GetInstance(Session session)
{
var result = session.QueryInTransaction<Singleton>().FirstOrDefault();
if (result == null)
{
result = new Singleton(session)
{
// some default init
}
}

return result;
}

The key is using QueryInTransaction, but as far I understand it doesn't prevent using several sessions here.


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>