本文整理汇总了C#中Store.CreateSession方法的典型用法代码示例。如果您正苦于以下问题:C# Store.CreateSession方法的具体用法?C# Store.CreateSession怎么用?C# Store.CreateSession使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Store
的用法示例。
在下文中一共展示了Store.CreateSession方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
private static void Main(string[] args)
{
var configuration = new Configuration
{
ConnectionFactory = new DbConnectionFactory<SqliteConnection>(@"Data Source=:memory:", true),
DocumentStorageFactory = new InMemoryDocumentStorageFactory()
};
var store = new Store(configuration);
store.InitializeAsync().Wait();
using (var session = store.CreateSession())
{
session.ExecuteMigration(x => x
.CreateReduceIndexTable(nameof(ArticleByWord), table => table
.Column<int>("Count")
.Column<string>("Word")
)
);
}
// register available indexes
store.RegisterIndexes<ArticleIndexProvider>();
// creating articles
using (var session = store.CreateSession())
{
session.Save(new Article { Content = "This is a white fox" });
session.Save(new Article { Content = "This is a brown cat" });
session.Save(new Article { Content = "This is a pink elephant" });
session.Save(new Article { Content = "This is a white tiger" });
}
using (var session = store.CreateSession())
{
Console.WriteLine("Simple term: 'white'");
var simple = session.QueryAsync<Article, ArticleByWord>().Where(a => a.Word == "white").List().Result;
foreach (var article in simple)
{
Console.WriteLine(article.Content);
}
Console.WriteLine("Boolean query: 'white or fox or pink'");
var boolQuery = session.QueryAsync<Article, ArticleByWord>().Where(a => a.Word.IsIn(new[] { "white", "fox", "pink" })).List().Result;
foreach (var article in boolQuery)
{
Console.WriteLine(article.Content);
}
}
}
示例2: Main
private static void Main(string[] args)
{
// 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<ArticleIndexProvider>();
// creating articles
using (var session = store.CreateSession())
{
session.Save(new Article {Content = "This is a white fox"});
session.Save(new Article {Content = "This is a brown cat"});
session.Save(new Article {Content = "This is a pink elephant"});
session.Save(new Article {Content = "This is a white tiger"});
session.Commit();
}
using (var session = store.CreateSession())
{
Console.WriteLine("Simple term: 'white'");
var simple = session.Query<Article, ArticleByWord>().Where(a => a.Word == "white").List();
foreach (var article in simple) {
Console.WriteLine(article.Content);
}
Document document = null;
Console.WriteLine("Boolean query: 'white or fox or pink'");
var boolQuery = session.Query<Article, ArticleByWord>().Where(a => a.Word.IsIn(new [] { "white", "fox", "pink" })).List();
foreach (var article in boolQuery)
{
Console.WriteLine(article.Content);
}
}
}
示例3: CreateContainer
//.........这里部分代码省略.........
cfg.IsolationLevel = IsolationLevel.ReadUncommitted;
if (!String.IsNullOrWhiteSpace(settings.TablePrefix))
{
cfg.TablePrefix = settings.TablePrefix + "_";
}
#if SQL
var sqlFactory = new SqlDocumentStorageFactory(connectionFactory);
sqlFactory.IsolationLevel = IsolationLevel.ReadUncommitted;
sqlFactory.ConnectionFactory = connectionFactory;
if (!String.IsNullOrWhiteSpace(settings.TablePrefix))
{
sqlFactory.TablePrefix = settings.TablePrefix + "_";
}
cfg.DocumentStorageFactory = sqlFactory;
#else
var storageFactory = new LightningDocumentStorageFactory(Path.Combine(_appDataFolderRoot.RootFolder, "Sites", settings.Name, "Documents"));
cfg.DocumentStorageFactory = storageFactory;
#endif
//cfg.RunDefaultMigration();
}
);
var idGenerator = new LinearBlockIdGenerator(store.Configuration.ConnectionFactory, 20, "contentitem", store.Configuration.TablePrefix);
store.RegisterIndexes(indexes);
tenantServiceCollection.AddInstance<IStore>(store);
tenantServiceCollection.AddInstance<LinearBlockIdGenerator>(idGenerator);
tenantServiceCollection.AddScoped<ISession>(serviceProvider =>
store.CreateSession()
);
}
tenantServiceCollection.AddInstance<ITypeFeatureProvider>(new TypeFeatureProvider(featureByType));
IServiceCollection moduleServiceCollection =
_serviceProvider.CreateChildContainer(_applicationServices);
foreach (var dependency in blueprint.Dependencies
.Where(t => typeof(IModule).IsAssignableFrom(t.Type)))
{
moduleServiceCollection.AddScoped(typeof(IModule), dependency.Type);
}
var moduleServiceProvider = moduleServiceCollection.BuildServiceProvider();
// Let any module add custom service descriptors to the tenant
foreach (var service in moduleServiceProvider.GetServices<IModule>())
{
service.Configure(tenantServiceCollection);
}
// Register event handlers on the event bus
var eventHandlers = tenantServiceCollection
.Select(x => x.ImplementationType)
.Distinct()
.Where(t => t != null && typeof(IEventHandler).IsAssignableFrom(t) && t.GetTypeInfo().IsClass)
.ToArray();
foreach (var handlerClass in eventHandlers)
{
tenantServiceCollection.AddScoped(handlerClass);
示例4: CreateContainer
//.........这里部分代码省略.........
tenantServiceCollection.AddSingleton<IEventBusState, EventBusState>();
// Configuring data access
var indexes = blueprint
.Dependencies
.Where(x => typeof(IIndexProvider).IsAssignableFrom(x.Type))
.Select(x => x.Type).ToArray();
if (settings.DatabaseProvider != null)
{
var store = new Store(cfg =>
{
// @"Data Source =.; Initial Catalog = test1; User Id=sa;Password=demo123!"
IConnectionFactory connectionFactory = null;
switch (settings.DatabaseProvider)
{
case "SqlConnection":
connectionFactory = new DbConnectionFactory<SqlConnection>(settings.ConnectionString);
break;
//case "SqliteConnection":
// connectionFactory = new DbConnectionFactory<SqliteConnection>(settings.ConnectionString);
// break;
default:
throw new ArgumentException("Unkown database provider: " + settings.DatabaseProvider);
}
var sqlFactory = new SqlDocumentStorageFactory(connectionFactory); ;
cfg.ConnectionFactory = connectionFactory;
cfg.DocumentStorageFactory = sqlFactory;
cfg.IsolationLevel = sqlFactory.IsolationLevel = IsolationLevel.ReadUncommitted;
if (!String.IsNullOrWhiteSpace(settings.TablePrefix))
{
cfg.TablePrefix = sqlFactory.TablePrefix = settings.TablePrefix + "_";
}
//cfg.RunDefaultMigration();
}
);
var idGenerator = new LinearBlockIdGenerator(store.Configuration.ConnectionFactory, 20, "contentitem", store.Configuration.TablePrefix);
store.RegisterIndexes(indexes);
tenantServiceCollection.AddInstance<IStore>(store);
tenantServiceCollection.AddInstance<LinearBlockIdGenerator>(idGenerator);
tenantServiceCollection.AddScoped<ISession>(serviceProvider =>
store.CreateSession()
);
}
tenantServiceCollection.AddInstance<ITypeFeatureProvider>(new TypeFeatureProvider(featureByType));
// Register event handlers on the event bus
var eventHandlers = tenantServiceCollection
.Select(x => x.ImplementationType)
.Where(t => t != null && typeof(IEventHandler).IsAssignableFrom(t) && t.GetTypeInfo().IsClass)
.ToArray();
foreach (var handlerClass in eventHandlers)
{
tenantServiceCollection.AddScoped(handlerClass);
// Register dynamic proxies to intercept direct calls if an IEventHandler is resolved, dispatching the call to
// the event bus.
foreach (var i in handlerClass.GetInterfaces().Where(t => typeof(IEventHandler).IsAssignableFrom(t)))
{
tenantServiceCollection.AddScoped(i, serviceProvider =>
{
var proxy = DefaultOrchardEventBus.CreateProxy(i);
proxy.EventBus = serviceProvider.GetService<IEventBus>();
return proxy;
});
}
}
var shellServiceProvider = tenantServiceCollection.BuildServiceProvider();
var eventBusState = shellServiceProvider.GetService<IEventBusState>();
// Register any IEventHandler method in the event bus
foreach (var handlerClass in eventHandlers)
{
foreach (var handlerInterface in handlerClass.GetInterfaces().Where(x => typeof(IEventHandler).IsAssignableFrom(x)))
{
foreach (var interfaceMethod in handlerInterface.GetMethods())
{
//var classMethod = handlerClass.GetMethods().Where(x => x.Name == interfaceMethod.Name && x.GetParameters().Length == interfaceMethod.GetParameters().Length).FirstOrDefault();
Func<IServiceProvider, IDictionary<string, object>, Task> d = (sp, parameters) => DefaultOrchardEventBus.Invoke(sp, parameters, interfaceMethod, handlerClass);
eventBusState.Add(handlerInterface.Name + "." + interfaceMethod.Name, d);
}
}
}
return shellServiceProvider;
}
示例5: Main
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.Commit();
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
},
}
});
session.Commit();
}
using(var session = store.CreateSession())
{
var p = session.QueryByMappedIndex<ProductByName, Product>(q => q.Where(x => x.Name == "Milk")).FirstOrDefault();
Console.WriteLine(p.Name + " " + p.Cost);
var orders = session.QueryByReducedIndex<OrderByCustomerName, Order>(q => q.Where(x => x.Name == "Microsoft"));
foreach (var order in orders)
{
Console.WriteLine(order.Id);
}
}
}
示例6: Main
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
}
}
}