I am using the above library in my vb.net web app. The person who developed snowmaker said that you shouldn't create a new instance every time you want an ID, you should use a basic singleton.
I know what singletons are, but have never used them. I have come across this on stack overflow
Public NotInheritable Class MySingleton
Private Shared ReadOnly _instance As New Lazy(Of MySingleton)(Function() New
MySingleton(), System.Threading.LazyThreadSafetyMode.ExecutionAndPublication)
Private Sub New()
End Sub
Public Shared ReadOnly Property Instance() As MySingleton
Get
Return _instance.Value
End Get
End Property
End Class
Here is the code I'm using to generate the ID's
Dim storageAccount As CloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings("blobStorage").ConnectionString)
Dim ds As New BlobOptimisticDataStore(storageAccount, "container-name")
Dim generator = New UniqueIdGenerator(ds)
Dim ret = generator.NextId(table)
which works, but how do I incorporate that into the singleton class so that I only call it once from my web app?
A singleton is a static object that you can call as many times as you want and be assured that it will only run one call at a time.
You don't instantiate a singleton, it is like a class level or global object that you just call. You havent included the code for UniqueIdGenerator, but your code might look something like this:
Imports SnowMaker
Imports Microsoft.WindowsAzure.Storage
Module Module1
Sub Main()
Dim storageAccount = CloudStorageAccount.Parse("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
Dim ds As New BlobOptimisticDataStore(storageAccount, "vhds")
MySingleton.Instance.DataSource = ds
MySingleton.Instance.Table = "table"
Dim myid = MySingleton.Instance.NextId
Dim myid2 = MySingleton.Instance.NextId
Dim myid3 = MySingleton.Instance.NextId
Dim myid4 = MySingleton.Instance.NextId
End Sub
End Module
Then your singleton code would call your generator
Imports SnowMaker
Public NotInheritable Class MySingleton
Private Shared ReadOnly _instance = New Lazy(Of MySingleton)(Function() New MySingleton(), System.Threading.LazyThreadSafetyMode.ExecutionAndPublication)
Private _generator As UniqueIdGenerator
Private Sub New()
End Sub
Public Shared ReadOnly Property Instance() As MySingleton
Get
Return _instance.Value
End Get
End Property
Private _ds As BlobOptimisticDataStore
Public Property DataSource() As BlobOptimisticDataStore
Get
Return _ds
End Get
Set(ByVal value As BlobOptimisticDataStore)
_ds = value
End Set
End Property
Private _tableName As String
Public Property Table() As String
Get
Return _tableName
End Get
Set(ByVal value As String)
_tableName = value
End Set
End Property
Private _Id As Integer
Public ReadOnly Property NextId() As Integer
Get
If _generator Is Nothing Then
_generator = New UniqueIdGenerator(_ds)
End If
Return _generator.NextId(_tableName)
End Get
End Property
End Class