Vb.net Azure的SnowMaker.Net库
我正在我的vb.net web应用程序中使用上述库。开发snowmaker的人说,不要每次需要ID时都创建一个新实例,应该使用基本的单例 我知道什么是单身,但从来没有用过。我在堆栈溢出时遇到了这个问题Vb.net Azure的SnowMaker.Net库,vb.net,azure,singleton,snowflake-cloud-data-platform,Vb.net,Azure,Singleton,Snowflake Cloud Data Platform,我正在我的vb.net web应用程序中使用上述库。开发snowmaker的人说,不要每次需要ID时都创建一个新实例,应该使用基本的单例 我知道什么是单身,但从来没有用过。我在堆栈溢出时遇到了这个问题 Public NotInheritable Class MySingleton Private Shared ReadOnly _instance As New Lazy(Of MySingleton)(Function() New MySingleton(), Syste
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
下面是我用来生成ID的代码
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)
这是可行的,但我如何将其合并到singleton类中,以便从我的web应用程序中只调用它一次?singleton是一个静态对象,您可以根据需要多次调用它,并确保它一次只运行一个调用 您不需要实例化一个单例,它就像您只需调用的类级别或全局对象。您尚未包含UniqueIdGenerator的代码,但您的代码可能如下所示:
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
然后你的单例代码会调用你的生成器
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
singleton是一个静态对象,您可以根据需要多次调用它,并确保它一次只运行一个调用 您不需要实例化一个单例,它就像您只需调用的类级别或全局对象。您尚未包含UniqueIdGenerator的代码,但您的代码可能如下所示:
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
然后你的单例代码会调用你的生成器
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
谢谢你的帮助,但我还是不知道。当我将其粘贴到VisualStudio2012中时,我会收到各种各样的IntelliSense错误。基本上我所做的就是这样,当您将它称为
Dim storageAccount作为CloudStorageAccount=CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings(“blobStorage”).ConnectionString)Dim ds作为新的BlobOptimisticDataStore(storageAccount,“xploite id”)Dim generator=New UniqueIdGenerator(ds)时,它就起作用了Dim orderNumber=generator.NextId(“orderNumbers”)
但是,多次运行它会返回以下ID号1、101、201、301,这是因为这些ID是以100为一批发布的。我想做的是得到1、2、3、4(只在批处理用完后运行该函数)。我在任何地方都找不到任何示例。基本上,您需要一次推入数据源和表,但需要多次调用nextid。nextid方法是snowmaker包dllSee的一部分。谢谢您的帮助,但我仍然没有任何线索。当我将其粘贴到VisualStudio2012中时,我会收到各种各样的IntelliSense错误。基本上我所做的就是这样,当您将它称为Dim storageAccount作为CloudStorageAccount=CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings(“blobStorage”).ConnectionString)Dim ds作为新的BlobOptimisticDataStore(storageAccount,“xploite id”)Dim generator=New UniqueIdGenerator(ds)时,它就起作用了Dim orderNumber=generator.NextId(“orderNumbers”)
但是,多次运行它会返回以下ID号1、101、201、301,这是因为这些ID是以100为一批发布的。我想做的是得到1、2、3、4(只在批处理用完后运行该函数)。我在任何地方都找不到示例。基本上,您需要一次推入数据源和表,但要多次调用nextid。nextid方法是snowmaker包dllSee的一部分