Skip to content
This repository
tree: 6c1d5d964f
Fetching contributors…

Cannot retrieve contributors at this time

file 82 lines (72 sloc) 2.771 kb
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
using System;
using FluentNHibernate.Cfg.Db;
using NHibernate.Tool.hbm2ddl;
using YesSql.Core.Data;
using YesSql.Samples.Shards.Indexes;
using YesSql.Samples.Shards.Models;

namespace YesSql.Samples.Shards
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var store = new Store().Configure(s => {
                var config0 = s.CreateConfiguration("Shard0", () =>
                    MsSqlConfiguration.MsSql2008
                        .ConnectionString("Server=.;Database=Shard0;Integrated Security = True")
                        );

                var config1 = s.CreateConfiguration("Shard1", () =>
                    MsSqlConfiguration.MsSql2008
                        .ConnectionString("Server=.;Database=Shard1;Integrated Security = True")
                        );

                new SchemaUpdate(config0).Execute(false, true);
                new SchemaUpdate(config1).Execute(false, true);
            })
            .SetShardingStrategy(new ShardStrategyFactory())
            .RegisterIndexes<ProductIndexProvider>()
            .RegisterIndexes<OrderIndexProvider>()
            ;
            
            using(var session = store.CreateSession())
            {
                var product = new Product {
                    Cost = 3.99m,
                    Name = "Milk",
                };

                session.Save(product);
                session.Flush();

                session.Save(new Order {
                    Customer = "Microsoft",
                    OrderLines =
                        {
                            new OrderLine
                            {
                                ProductId = product.Id,
                                Quantity = 3
                            },
                        }
                });

                session.Save(new Order {
                    Customer = "Microsoft",
                    OrderLines =
                        {
                            new OrderLine
                            {
                                ProductId = product.Id,
                                Quantity = 5
                            },
                        }
                });

            }

            using(var session = store.CreateSession())
            {
                var p = session.Query<Product, ProductByName>().Where(x => x.Name == "Milk").FirstOrDefault();
                Console.WriteLine(p.Name + " " + p.Cost);

                var orders = session.Query<Order, OrderByCustomerName>().Where(x => x.Name == "Microsoft").List();
                foreach (var order in orders)
                {
                    Console.WriteLine(order.Id);
                }
            }
        }
    }
}
Something went wrong with that request. Please try again.