示例#1
0
        private static void Main()
        {
            var store = new Store(cfg =>
            {
                cfg.ConnectionFactory = new DbConnectionFactory<SQLiteConnection>(@"Data Source=:memory:", true);
                cfg.DocumentStorageFactory = new InMemoryDocumentStorageFactory();

                cfg.Migrations.Add(builder => builder
                    .CreateMapIndexTable(nameof(BlogPostByAuthor), table => table
                        .Column<string>("Author")
                    )
                    .CreateReduceIndexTable(nameof(BlogPostByDay), table => table
                        .Column<int>("Count")
                        .Column<int>("Day")
                    )
                );
            });

            // register available indexes
            store.RegisterIndexes<BlogPostIndexProvider>();

            // creating a blog post
            var post = new BlogPost
            {
                Title = "Hello YesSql",
                Author = "Bill",
                Content = "Hello",
                PublishedUtc = DateTime.UtcNow,
                Tags = new[] {"Hello", "YesSql"}
            };

            // saving the post to the database
            using(var session = store.CreateSession())
            {
                session.Save(post);
            }

            // loading a single blog post
            using(var session = store.CreateSession())
            {
                var p = session.QueryAsync().For<BlogPost>().FirstOrDefault().Result;
                Console.WriteLine(p.Title); // > Hello YesSql
            }

            // loading blog posts by author
            using (var session = store.CreateSession())
            {
                var ps = session.QueryAsync<BlogPost, BlogPostByAuthor>().Where(x => x.Author.StartsWith("B")).List().Result;

                foreach (var p in ps)
                {
                    Console.WriteLine(p.Author); // > Bill
                }
            }

            // loading blog posts by day of publication
            using (var session = store.CreateSession())
            {
                var ps = session.QueryAsync<BlogPost, BlogPostByDay>(x => x.Day == DateTime.UtcNow.ToString("yyyyMMdd")).List().Result;

                foreach (var p in ps)
                {
                    Console.WriteLine(p.PublishedUtc); // > [Now]
                }
            }

            // counting blog posts by day
            using (var session = store.CreateSession())
            {
                var days = session.QueryIndexAsync<BlogPostByDay>().List().Result;

                foreach (var day in days)
                {
                    Console.WriteLine(day.Day + ": " + day.Count); // > [Today]: 1
                }
            }
        }
示例#2
0
        private static void Main()
        {
            // configure the store to use a local SqlCe database
            InitializeDatabase();
            var store = new Store().Configure(MsSqlCeConfiguration.MsSqlCe40.ConnectionString("Data Source=Store.sdf"));

            // register available indexes
            store.RegisterIndexes<BlogPostIndexProvider>();

            // creating a blog post
            var post = new BlogPost
            {
                Title = "Hello YesSql",
                Author = "Bill",
                Content = "Hello",
                PublishedUtc = DateTime.UtcNow,
                Tags = new[] {"Hello", "YesSql"}
            };

            // saving the post to the database
            using(var session = store.CreateSession())
            {
                session.Save(post);
                session.Commit();
            }

            // loading a single blog post
            using(var session = store.CreateSession())
            {
                var p = session.QueryDocument<BlogPost>(query => query.FirstOrDefault());
                Console.WriteLine(p.Title); // > Hello YesSql
            }

            // loading blog posts by author
            using (var session = store.CreateSession())
            {
                var ps = session.QueryByMappedIndex<BlogPostByAuthor, BlogPost>(
                    query => query.Where(x => x.Author.StartsWith("B"))
                );

                foreach (var p in ps)
                {
                    Console.WriteLine(p.Author); // > Bill
                }
            }

            // loading blog posts by day of publication
            using (var session = store.CreateSession())
            {
                var ps = session.QueryByReducedIndex<BlogPostByDay, BlogPost>(
                    query => query.Where(x => x.Day == DateTime.UtcNow.ToString("yyyyMMdd"))
                    );

                foreach (var p in ps)
                {
                    Console.WriteLine(p.PublishedUtc); // > [Now]
                }
            }

            // counting blog posts by day
            using (var session = store.CreateSession())
            {
                var days = session.QueryIndex<BlogPostByDay>().ToList();

                foreach (var day in days)
                {
                    Console.WriteLine(day.Day + ": " + day.Count); // > [Today]: 1
                }
            }
        }