public static void Main() { SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder { UserID = s_userName, Password = s_password, ApplicationName = s_applicationName }; // Bootstrap the shard map manager, register shards, and store mappings of tenants to shards // Note that you can keep working with existing shard maps. There is no need to // re-create and populate the shard map from scratch every time. Sharding shardingLayer = new Sharding(s_server, s_shardmapmgrdb, connStrBldr.ConnectionString); shardingLayer.RegisterNewShard(s_server, s_shard1, connStrBldr.ConnectionString, s_tenantId1); shardingLayer.RegisterNewShard(s_server, s_shard2, connStrBldr.ConnectionString, s_tenantId2); // Create schema on each shard. foreach (string shard in new[] { s_shard1, s_shard2 }) { CreateSchema(shard); } // Do work for tenant 1 :-) // For tenant 1, let's stay with plain vanilla Dapper // and spell out the T-SQL we use to map into objects. // Create and save a new Blog Console.Write("Enter a name for a new Blog: "); var name = Console.ReadLine(); SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => { using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( key: s_tenantId1, connectionString: connStrBldr.ConnectionString, options: ConnectionOptions.Validate)) { var blog = new Blog { Name = name }; sqlconn.Insert(blog); } }); SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => { using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( key: s_tenantId1, connectionString: connStrBldr.ConnectionString, options: ConnectionOptions.Validate)) { // Display all Blogs for tenant 1 IEnumerable <Blog> result = sqlconn.Query <Blog>(@" SELECT * FROM Blog ORDER BY Name"); Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1); foreach (var item in result) { Console.WriteLine(item.Name); } } }); // Do work for tenant 2 :-) // Here I am going to illustrate how to integrate // with DapperExtensions which saves us the T-SQL // SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => { using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( key: s_tenantId2, connectionString: connStrBldr.ConnectionString, options: ConnectionOptions.Validate)) { // Display all Blogs for tenant 2 IEnumerable <Blog> result = sqlconn.GetList <Blog>(); Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2); foreach (var item in result) { Console.WriteLine(item.Name); } } }); // Create and save a new Blog Console.Write("Enter a name for a new Blog: "); var name2 = Console.ReadLine(); SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => { using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( key: s_tenantId2, connectionString: connStrBldr.ConnectionString, options: ConnectionOptions.Validate)) { var blog = new Blog { Name = name2 }; sqlconn.Insert(blog); } }); SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => { using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(s_tenantId2, connStrBldr.ConnectionString, ConnectionOptions.Validate)) { // Display all Blogs for tenant 2 IEnumerable <Blog> result = sqlconn.GetList <Blog>(); Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2); foreach (var item in result) { Console.WriteLine(item.Name); } } }); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
public static void Main() { SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder { UserID = s_userName, Password = s_password, ApplicationName = s_applicationName }; // Bootstrap the shard map manager, register shards, and store mappings of tenants to shards // Note that you can keep working with existing shard maps. There is no need to // re-create and populate the shard map from scratch every time. Sharding shardingLayer = new Sharding(s_server, s_shardmapmgrdb, connStrBldr.ConnectionString); shardingLayer.RegisterNewShard(s_server, s_shard1, connStrBldr.ConnectionString, s_tenantId1); shardingLayer.RegisterNewShard(s_server, s_shard2, connStrBldr.ConnectionString, s_tenantId2); // Create schema on each shard. foreach (string shard in new[] {s_shard1, s_shard2}) { CreateSchema(shard); } // Do work for tenant 1 :-) // For tenant 1, let's stay with plain vanilla Dapper // and spell out the T-SQL we use to map into objects. // Create and save a new Blog Console.Write("Enter a name for a new Blog: "); var name = Console.ReadLine(); SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => { using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( key: s_tenantId1, connectionString: connStrBldr.ConnectionString, options: ConnectionOptions.Validate)) { var blog = new Blog { Name = name }; sqlconn.Insert(blog); } }); SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => { using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( key: s_tenantId1, connectionString: connStrBldr.ConnectionString, options: ConnectionOptions.Validate)) { // Display all Blogs for tenant 1 IEnumerable<Blog> result = sqlconn.Query<Blog>(@" SELECT * FROM Blog ORDER BY Name"); Console.WriteLine("All blogs for tenant id {0}:", s_tenantId1); foreach (var item in result) { Console.WriteLine(item.Name); } } }); // Do work for tenant 2 :-) // Here I am going to illustrate how to integrate // with DapperExtensions which saves us the T-SQL // SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => { using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( key: s_tenantId2, connectionString: connStrBldr.ConnectionString, options: ConnectionOptions.Validate)) { // Display all Blogs for tenant 2 IEnumerable<Blog> result = sqlconn.GetList<Blog>(); Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2); foreach (var item in result) { Console.WriteLine(item.Name); } } }); // Create and save a new Blog Console.Write("Enter a name for a new Blog: "); var name2 = Console.ReadLine(); SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => { using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey( key: s_tenantId2, connectionString: connStrBldr.ConnectionString, options: ConnectionOptions.Validate)) { var blog = new Blog { Name = name2 }; sqlconn.Insert(blog); } }); SqlDatabaseUtils.SqlRetryPolicy.ExecuteAction(() => { using (SqlConnection sqlconn = shardingLayer.ShardMap.OpenConnectionForKey(s_tenantId2, connStrBldr.ConnectionString, ConnectionOptions.Validate)) { // Display all Blogs for tenant 2 IEnumerable<Blog> result = sqlconn.GetList<Blog>(); Console.WriteLine("All blogs for tenant id {0}:", s_tenantId2); foreach (var item in result) { Console.WriteLine(item.Name); } } }); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }