|
1 | 1 | // Copyright (c) Microsoft. All rights reserved. |
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | 3 |
|
4 | | -using System; |
5 | | -using System.Collections.Generic; |
6 | | -using System.Data.SqlClient; |
7 | 4 | using Dapper; |
8 | 5 | using DapperExtensions; |
9 | 6 | using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement; |
| 7 | +using Microsoft.Data.SqlClient; |
| 8 | +using System; |
10 | 9 |
|
11 | 10 | //////////////////////////////////////////////////////////////////////////////////////// |
12 | 11 | // This sample illustrates the adjustments that need to be made to use Dapper |
|
16 | 15 | // Shard Map Management. |
17 | 16 | //////////////////////////////////////////////////////////////////////////////////////// |
18 | 17 |
|
19 | | -namespace ElasticDapper |
| 18 | +namespace ElasticDapper; |
| 19 | + |
| 20 | +// This sample requires three pre-created empty SQL Server databases. |
| 21 | +// The first database serves as the shard map manager database to store the Elastic Scale shard map. |
| 22 | +// The remaining two databases serve as shards to hold the data for the sample. |
| 23 | +internal class Program |
20 | 24 | { |
21 | | - // This sample requires three pre-created empty SQL Server databases. |
22 | | - // The first database serves as the shard map manager database to store the Elastic Scale shard map. |
23 | | - // The remaining two databases serve as shards to hold the data for the sample. |
24 | | - internal class Program |
| 25 | + // You need to adjust the following settings to your database server and database names in Azure Db |
| 26 | + private static readonly string s_server = "[YourSQLServerName]"; |
| 27 | + private static readonly string s_shardmapmgrdb = "[YourShardMapManagerDatabaseName]"; |
| 28 | + private static readonly string s_shard1 = "[YourShard01DatabaseName]"; |
| 29 | + private static readonly string s_shard2 = "[YourShard02DatabaseName]"; |
| 30 | + private static readonly string s_userName = "YourUserName"; |
| 31 | + private static readonly string s_password = "YourPassword"; |
| 32 | + private static readonly string s_applicationName = "ESC_Dapv1.0"; |
| 33 | + |
| 34 | + // Just two tenants for now. |
| 35 | + // Those we will allocate to shards. |
| 36 | + private static readonly int s_tenantId1 = 42; |
| 37 | + private static readonly int s_tenantId2 = 12; |
| 38 | + |
| 39 | + public static void Main() |
25 | 40 | { |
26 | | - // You need to adjust the following settings to your database server and database names in Azure Db |
27 | | - private static string s_server = "[YourSQLServerName]"; |
28 | | - private static string s_shardmapmgrdb = "[YourShardMapManagerDatabaseName]"; |
29 | | - private static string s_shard1 = "[YourShard01DatabaseName]"; |
30 | | - private static string s_shard2 = "[YourShard02DatabaseName]"; |
31 | | - private static string s_userName = "YourUserName"; |
32 | | - private static string s_password = "YourPassword"; |
33 | | - private static string s_applicationName = "ESC_Dapv1.0"; |
34 | | - |
35 | | - // Just two tenants for now. |
36 | | - // Those we will allocate to shards. |
37 | | - private static int s_tenantId1 = 42; |
38 | | - private static int s_tenantId2 = 12; |
39 | | - |
40 | | - public static void Main() |
| 41 | + var connStrBldr = new SqlConnectionStringBuilder |
| 42 | + { |
| 43 | + UserID = s_userName, |
| 44 | + Password = s_password, |
| 45 | + ApplicationName = s_applicationName |
| 46 | + }; |
| 47 | + |
| 48 | + // Bootstrap the shard map manager, register shards, and store mappings of tenants to shards |
| 49 | + // Note that you can keep working with existing shard maps. There is no need to |
| 50 | + // re-create and populate the shard map from scratch every time. |
| 51 | + var shardingLayer = new Sharding(s_server, s_shardmapmgrdb, connStrBldr.ConnectionString); |
| 52 | + shardingLayer.RegisterNewShard(s_server, s_shard1, connStrBldr.ConnectionString, s_tenantId1); |
| 53 | + shardingLayer.RegisterNewShard(s_server, s_shard2, connStrBldr.ConnectionString, s_tenantId2); |
| 54 | + |
| 55 | + // Create schema on each shard. |
| 56 | + foreach (var shard in new[] { s_shard1, s_shard2 }) |
| 57 | + { |
| 58 | + CreateSchema(shard); |
| 59 | + } |
| 60 | + |
| 61 | + // Do work for tenant 1 :-) |
| 62 | + // For tenant 1, let's stay with plain vanilla Dapper |
| 63 | + // and spell out the T-SQL we use to map into objects. |
| 64 | + |
| 65 | + // Create and save a new Blog |
| 66 | + Console.Write("Enter a name for a new Blog: "); |
| 67 | + var name = Console.ReadLine(); |
| 68 | + |
| 69 | + { |
| 70 | + using var sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( |
| 71 | + key: s_tenantId1, |
| 72 | + connectionString: connStrBldr.ConnectionString, |
| 73 | + options: ConnectionOptions.Validate); |
| 74 | + sqlconn.RetryLogicProvider = SqlDatabaseUtils.SqlRetryProvider; |
| 75 | + var blog = new Blog { Name = name }; |
| 76 | + sqlconn.Insert(blog); |
| 77 | + } |
| 78 | + |
41 | 79 | { |
42 | | - SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder |
| 80 | + using var sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( |
| 81 | + key: s_tenantId1, |
| 82 | + connectionString: connStrBldr.ConnectionString, |
| 83 | + options: ConnectionOptions.Validate); |
| 84 | + sqlconn.RetryLogicProvider = SqlDatabaseUtils.SqlRetryProvider; |
| 85 | + // Display all Blogs for tenant 1 |
| 86 | + var result = sqlconn.Query<Blog>(@" |
| 87 | + SELECT * |
| 88 | + FROM Blog |
| 89 | + ORDER BY Name"); |
| 90 | + |
| 91 | + Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1); |
| 92 | + foreach (var item in result) |
43 | 93 | { |
44 | | - UserID = s_userName, |
45 | | - Password = s_password, |
46 | | - ApplicationName = s_applicationName |
47 | | - }; |
48 | | - |
49 | | - // Bootstrap the shard map manager, register shards, and store mappings of tenants to shards |
50 | | - // Note that you can keep working with existing shard maps. There is no need to |
51 | | - // re-create and populate the shard map from scratch every time. |
52 | | - Sharding shardingLayer = new Sharding(s_server, s_shardmapmgrdb, connStrBldr.ConnectionString); |
53 | | - shardingLayer.RegisterNewShard(s_server, s_shard1, connStrBldr.ConnectionString, s_tenantId1); |
54 | | - shardingLayer.RegisterNewShard(s_server, s_shard2, connStrBldr.ConnectionString, s_tenantId2); |
55 | | - |
56 | | - // Create schema on each shard. |
57 | | - foreach (string shard in new[] {s_shard1, s_shard2}) |
| 94 | + Console.WriteLine(item.Name); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Do work for tenant 2 :-) |
| 99 | + // Here I am going to illustrate how to integrate |
| 100 | + // with DapperExtensions which saves us the T-SQL |
| 101 | + // |
| 102 | + { |
| 103 | + using var sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( |
| 104 | + key: s_tenantId2, |
| 105 | + connectionString: connStrBldr.ConnectionString, |
| 106 | + options: ConnectionOptions.Validate); |
| 107 | + sqlconn.RetryLogicProvider = SqlDatabaseUtils.SqlRetryProvider; |
| 108 | + // Display all Blogs for tenant 2 |
| 109 | + var result = sqlconn.GetList<Blog>(); |
| 110 | + Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2); |
| 111 | + foreach (var item in result) |
58 | 112 | { |
59 | | - CreateSchema(shard); |
| 113 | + Console.WriteLine(item.Name); |
60 | 114 | } |
| 115 | + } |
61 | 116 |
|
62 | | - // Do work for tenant 1 :-) |
63 | | - // For tenant 1, let's stay with plain vanilla Dapper |
64 | | - // and spell out the T-SQL we use to map into objects. |
| 117 | + // Create and save a new Blog |
| 118 | + Console.Write("Enter a name for a new Blog: "); |
| 119 | + var name2 = Console.ReadLine(); |
65 | 120 |
|
66 | | - // Create and save a new Blog |
67 | | - Console.Write("Enter a name for a new Blog: "); |
68 | | - var name = Console.ReadLine(); |
| 121 | + { |
| 122 | + using var sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( |
| 123 | + key: s_tenantId2, |
| 124 | + connectionString: connStrBldr.ConnectionString, |
| 125 | + options: ConnectionOptions.Validate); |
| 126 | + sqlconn.RetryLogicProvider = SqlDatabaseUtils.SqlRetryProvider; |
| 127 | + var blog = new Blog { Name = name2 }; |
| 128 | + sqlconn.Insert(blog); |
| 129 | + } |
69 | 130 |
|
70 | | - SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => |
71 | | - { |
72 | | - using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( |
73 | | - key: s_tenantId1, |
74 | | - connectionString: connStrBldr.ConnectionString, |
75 | | - options: ConnectionOptions.Validate)) |
76 | | - { |
77 | | - var blog = new Blog { Name = name }; |
78 | | - sqlconn.Insert(blog); |
79 | | - } |
80 | | - }); |
81 | | - |
82 | | - SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => |
83 | | - { |
84 | | - using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( |
85 | | - key: s_tenantId1, |
86 | | - connectionString: connStrBldr.ConnectionString, |
87 | | - options: ConnectionOptions.Validate)) |
88 | | - { |
89 | | - // Display all Blogs for tenant 1 |
90 | | - IEnumerable<Blog> result = sqlconn.Query<Blog>(@" |
91 | | - SELECT * |
92 | | - FROM Blog |
93 | | - ORDER BY Name"); |
94 | | - |
95 | | - Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1); |
96 | | - foreach (var item in result) |
97 | | - { |
98 | | - Console.WriteLine(item.Name); |
99 | | - } |
100 | | - } |
101 | | - }); |
102 | | - |
103 | | - // Do work for tenant 2 :-) |
104 | | - // Here I am going to illustrate how to integrate |
105 | | - // with DapperExtensions which saves us the T-SQL |
106 | | - // |
107 | | - SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => |
108 | | - { |
109 | | - using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( |
110 | | - key: s_tenantId2, |
111 | | - connectionString: connStrBldr.ConnectionString, |
112 | | - options: ConnectionOptions.Validate)) |
113 | | - { |
114 | | - // Display all Blogs for tenant 2 |
115 | | - IEnumerable<Blog> result = sqlconn.GetList<Blog>(); |
116 | | - Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2); |
117 | | - foreach (var item in result) |
118 | | - { |
119 | | - Console.WriteLine(item.Name); |
120 | | - } |
121 | | - } |
122 | | - }); |
123 | | - |
124 | | - // Create and save a new Blog |
125 | | - Console.Write("Enter a name for a new Blog: "); |
126 | | - var name2 = Console.ReadLine(); |
127 | | - |
128 | | - SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => |
129 | | - { |
130 | | - using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( |
131 | | - key: s_tenantId2, |
132 | | - connectionString: connStrBldr.ConnectionString, |
133 | | - options: ConnectionOptions.Validate)) |
134 | | - { |
135 | | - var blog = new Blog { Name = name2 }; |
136 | | - sqlconn.Insert(blog); |
137 | | - } |
138 | | - }); |
139 | | - |
140 | | - SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => |
| 131 | + { |
| 132 | + using var sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(s_tenantId2, connStrBldr.ConnectionString, ConnectionOptions.Validate); |
| 133 | + sqlconn.RetryLogicProvider = SqlDatabaseUtils.SqlRetryProvider; |
| 134 | + // Display all Blogs for tenant 2 |
| 135 | + var result = sqlconn.GetList<Blog>(); |
| 136 | + Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2); |
| 137 | + foreach (var item in result) |
141 | 138 | { |
142 | | - using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(s_tenantId2, connStrBldr.ConnectionString, ConnectionOptions.Validate)) |
143 | | - { |
144 | | - // Display all Blogs for tenant 2 |
145 | | - IEnumerable<Blog> result = sqlconn.GetList<Blog>(); |
146 | | - Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2); |
147 | | - foreach (var item in result) |
148 | | - { |
149 | | - Console.WriteLine(item.Name); |
150 | | - } |
151 | | - } |
152 | | - }); |
153 | | - |
154 | | - Console.WriteLine("Press any key to exit..."); |
155 | | - Console.ReadKey(); |
| 139 | + Console.WriteLine(item.Name); |
| 140 | + } |
156 | 141 | } |
157 | 142 |
|
158 | | - private static void CreateSchema(string shardName) |
| 143 | + Console.WriteLine("Press any key to exit..."); |
| 144 | + _ = Console.ReadKey(); |
| 145 | + } |
| 146 | + |
| 147 | + private static void CreateSchema(string shardName) |
| 148 | + { |
| 149 | + var connStrBldr = new SqlConnectionStringBuilder |
159 | 150 | { |
160 | | - SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder |
161 | | - { |
162 | | - UserID = s_userName, |
163 | | - Password = s_password, |
164 | | - ApplicationName = s_applicationName, |
165 | | - DataSource = s_server, |
166 | | - InitialCatalog = shardName |
167 | | - }; |
168 | | - |
169 | | - using (SqlConnection conn = new SqlConnection(connStrBldr.ToString())) |
170 | | - { |
171 | | - conn.Open(); |
172 | | - conn.Execute(@" |
| 151 | + UserID = s_userName, |
| 152 | + Password = s_password, |
| 153 | + ApplicationName = s_applicationName, |
| 154 | + DataSource = s_server, |
| 155 | + InitialCatalog = shardName |
| 156 | + }; |
| 157 | + |
| 158 | + using var conn = new SqlConnection(connStrBldr.ToString()); |
| 159 | + conn.Open(); |
| 160 | + _ = conn.Execute(@" |
173 | 161 | IF (OBJECT_ID('[dbo].[Blog]', 'U') IS NULL) |
174 | 162 | CREATE TABLE [dbo].[Blog]( |
175 | 163 | [BlogId] [int] IDENTITY(1,1) PRIMARY KEY, |
176 | 164 | [Name] [nvarchar](max) NULL, |
177 | 165 | [Url] [nvarchar](max) NULL, |
178 | 166 | )"); |
179 | | - } |
180 | | - } |
181 | 167 | } |
182 | 168 | } |
0 commit comments