The Visitor pattern is a behavioural design pattern, allowing new operations to be created or executed by the object, without modifying its structure. For this to happen a separate algorithm (the visitor) handles the operation. When it comes to real-world scenarios, this article shows a tax example to exemplify the pattern in C#.
Deprecated package
1
| <PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
|
New package
1
| <PackageReference Include="Azure.Storage.Queues" Version="12.22.0" />
|
From
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
/// <summary>
/// A provider for queue
/// </summary>
public class QueueProvider : IQueueProvider
{
private readonly IConfiguration _configuration;
private readonly CloudQueue _unmoderatedQueue;
/// <summary>
/// Initializes a new instance of the <see cref="BlobStorageProvider"/> class.
/// </summary>
public QueueProvider(IConfiguration configuration)
{
_configuration = configuration;
// Create the queue client.
CloudQueueClient queueClient = GetCloudStorageAccount().CreateCloudQueueClient();
// Retrieve a reference to a container.
_unmoderatedQueue = queueClient.GetQueueReference(SettingsHelper.Azure.BlobContainerUnmoderated);
// Create the queue if it doesn't already exist
_unmoderatedQueue.CreateIfNotExistsAsync().GetAwaiter().GetResult();
}
/// <summary>
/// Add a message to the queue
/// </summary>
public async Task AddToQueue(string message)
{
// Create a message and add it to the queue.
CloudQueueMessage queueMessage = new CloudQueueMessage(message);
await _unmoderatedQueue.AddMessageAsync(queueMessage);
}
private CloudStorageAccount GetCloudStorageAccount()
{
// Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
_configuration.GetConnectionString("AzureStorageConnectionString"));
return storageAccount;
}
}
|
To
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| using Azure.Storage.Queues;
/// <summary>
/// A provider for queue
/// </summary>
public class QueueProvider : IQueueProvider
{
private readonly IConfiguration _configuration;
private readonly QueueClient _unmoderatedQueue;
/// <summary>
/// Initializes a new instance of the <see cref="BlobStorageProvider"/> class.
/// </summary>
public QueueProvider(IConfiguration configuration)
{
_configuration = configuration;
// Create the queue client.
_unmoderatedQueue = new QueueClient(
_configuration.GetConnectionString("AzureStorageConnectionString"),
SettingsHelper.Azure.BlobContainerUnmoderated);
// Create the queue if it doesn't already exist
_unmoderatedQueue.CreateIfNotExists();
}
/// <summary>
/// Add a message to the queue
/// </summary>
public async Task AddToQueue(string message)
{
// Create a message and add it to the queue.
await _unmoderatedQueue.SendMessageAsync(message);
}
}
|
In this example, you could see a practical (real-world) example of using visitors to solve a real-world problem. The example is available on PlayGoKids repository.
Join the conversation! Share your thoughts and connect with other readers.