Skip to content

Commit 61da035

Browse files
committed
Add YesSql.Samples.Web
1 parent 670449f commit 61da035

14 files changed

+362
-1
lines changed

Diff for: YesSql.sln

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26215.3
4+
VisualStudioVersion = 15.0.26228.4
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{0882456B-4A70-4895-A3AE-39B9D30A1B31}"
77
EndProject
@@ -36,6 +36,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YesSql.Samples.Hi", "sample
3636
EndProject
3737
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YesSql.Samples.FullText", "samples\YesSql.Samples.FullText\YesSql.Samples.FullText.csproj", "{DC85F009-6C82-4829-9764-E328C04F023F}"
3838
EndProject
39+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YesSql.Samples.Web", "samples\YesSql.Samples.Web\YesSql.Samples.Web.csproj", "{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}"
40+
EndProject
3941
Global
4042
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4143
Debug|Any CPU = Debug|Any CPU
@@ -208,6 +210,22 @@ Global
208210
{DC85F009-6C82-4829-9764-E328C04F023F}.Release|x64.Build.0 = Release|Any CPU
209211
{DC85F009-6C82-4829-9764-E328C04F023F}.Release|x86.ActiveCfg = Release|Any CPU
210212
{DC85F009-6C82-4829-9764-E328C04F023F}.Release|x86.Build.0 = Release|Any CPU
213+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
214+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
215+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
216+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
217+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Debug|x64.ActiveCfg = Debug|Any CPU
218+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Debug|x64.Build.0 = Debug|Any CPU
219+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Debug|x86.ActiveCfg = Debug|Any CPU
220+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Debug|x86.Build.0 = Debug|Any CPU
221+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
222+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Release|Any CPU.Build.0 = Release|Any CPU
223+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
224+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
225+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Release|x64.ActiveCfg = Release|Any CPU
226+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Release|x64.Build.0 = Release|Any CPU
227+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Release|x86.ActiveCfg = Release|Any CPU
228+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C}.Release|x86.Build.0 = Release|Any CPU
211229
EndGlobalSection
212230
GlobalSection(SolutionProperties) = preSolution
213231
HideSolutionNode = FALSE
@@ -223,6 +241,7 @@ Global
223241
{45940D28-3708-4EF1-8428-D85AD47D5594} = {0C294EC4-E6EF-4839-AD34-335C1A5112F9}
224242
{2F005B29-D46A-441F-933D-CD25A0235074} = {0882456B-4A70-4895-A3AE-39B9D30A1B31}
225243
{DC85F009-6C82-4829-9764-E328C04F023F} = {0882456B-4A70-4895-A3AE-39B9D30A1B31}
244+
{F158F8FF-CA19-4C31-B17A-B28B5B6F9B3C} = {0882456B-4A70-4895-A3AE-39B9D30A1B31}
226245
EndGlobalSection
227246
GlobalSection(TestCaseManagementSettings) = postSolution
228247
CategoryFile = Store.vsmdi
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using Microsoft.AspNetCore.Mvc;
3+
using YesSql.Core.Services;
4+
using Demo.Models;
5+
6+
namespace Demo.Controllers
7+
{
8+
public class HomeController : Controller
9+
{
10+
private readonly IStore _store;
11+
12+
public HomeController(IStore store)
13+
{
14+
_store = store;
15+
//_store.InitializeAsync().Wait();
16+
//using (var session = store.CreateSession())
17+
//{
18+
// //deleting a blog posts tables
19+
// session.ExecuteMigration(builder => builder.DropTable("BlogPosts"), false);
20+
// //creating a blog posts tables
21+
// session.ExecuteMigration(builder => builder
22+
// .CreateTable("BlogPosts", table => table
23+
// .Column<int>("Id", column => column.Identity())
24+
// .Column<string>("Title")
25+
// .Column<string>("Author")
26+
// .Column<string>("Content")
27+
// .Column<DateTime>("PublishedUtc")
28+
// .Column<string>("Tags")
29+
// ), false
30+
// );
31+
//};
32+
}
33+
34+
[Route("/")]
35+
public IActionResult Index()
36+
{
37+
// creating a blog post
38+
var post = new BlogPost
39+
{
40+
Title = "Hello YesSql",
41+
Author = "Bill",
42+
Content = "Hello",
43+
PublishedUtc = DateTime.UtcNow,
44+
Tags = new[] { "Hello", "YesSql" }
45+
};
46+
47+
// saving the post to the database
48+
using (var session = _store.CreateSession())
49+
{
50+
session.Save(post);
51+
}
52+
53+
// loading an single blog post
54+
using (var session = _store.CreateSession())
55+
{
56+
post = session.QueryAsync<BlogPost>().FirstOrDefault().Result;
57+
}
58+
59+
return View(post);
60+
}
61+
}
62+
}

Diff for: samples/YesSql.Samples.Web/Data/DbProviderOptions.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using YesSql.Core.Services;
2+
3+
namespace YesSql.Data
4+
{
5+
public class DbProviderOptions : IDbProviderOptions
6+
{
7+
public string ProviderName { get; set; }
8+
9+
public Configuration Configuration { get; set; }
10+
}
11+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using YesSql.Core.Services;
2+
3+
namespace YesSql.Data
4+
{
5+
public interface IDbProviderOptions
6+
{
7+
string ProviderName { get; set; }
8+
9+
Configuration Configuration { get; set; }
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.Data.Sqlite;
2+
using System;
3+
using System.Data;
4+
using YesSql.Core.Services;
5+
using YesSql.Data;
6+
using YesSql.Storage.InMemory;
7+
8+
namespace YesSql.Extensions.InMemory
9+
{
10+
public static class InMemoryDbProviderOptionsExtensions
11+
{
12+
public static void UseInMemory(
13+
this IDbProviderOptions options)
14+
{
15+
if (options == null)
16+
{
17+
throw new ArgumentNullException(nameof(options));
18+
}
19+
20+
var connectionString = "Data Source=:memory:";
21+
22+
var configuration = new Configuration
23+
{
24+
ConnectionFactory = new DbConnectionFactory<SqliteConnection>(connectionString, true),
25+
DocumentStorageFactory = new InMemoryDocumentStorageFactory(),
26+
IsolationLevel = IsolationLevel.Serializable
27+
};
28+
29+
options.ProviderName = "InMemory";
30+
options.Configuration = configuration;
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using YesSql.Core.Services;
3+
using YesSql.Data;
4+
5+
namespace Microsoft.Extensions.DependencyInjection
6+
{
7+
public static class ServiceCollectionExtensions
8+
{
9+
public static IServiceCollection AddDbProvider(
10+
this IServiceCollection services,
11+
Action<IDbProviderOptions> setupAction)
12+
{
13+
if (services == null)
14+
{
15+
throw new ArgumentNullException(nameof(services));
16+
}
17+
18+
if (setupAction == null)
19+
{
20+
throw new ArgumentNullException(nameof(setupAction));
21+
}
22+
23+
var options = new DbProviderOptions();
24+
setupAction.Invoke(options);
25+
services.AddSingleton<IStore>(new Store(options.Configuration));
26+
27+
return services;
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.Data.Sqlite;
2+
using System;
3+
using System.Data;
4+
using YesSql.Core.Services;
5+
using YesSql.Data;
6+
using YesSql.Storage.Sql;
7+
8+
namespace YesSql.Extensions.SqLite
9+
{
10+
public static class SqLiteDbOptionsExtensions
11+
{
12+
public static void UseSqLite(
13+
this IDbProviderOptions options, string connectionString)
14+
{
15+
if (options == null)
16+
{
17+
throw new ArgumentNullException(nameof(options));
18+
}
19+
20+
var configuration = new Configuration
21+
{
22+
ConnectionFactory = new DbConnectionFactory<SqliteConnection>(connectionString, true),
23+
DocumentStorageFactory = new SqlDocumentStorageFactory(),
24+
IsolationLevel = IsolationLevel.Serializable
25+
};
26+
27+
options.ProviderName = "SQLite";
28+
options.Configuration = configuration;
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Data;
3+
using System.Data.SqlClient;
4+
using YesSql.Core.Services;
5+
using YesSql.Data;
6+
using YesSql.Storage.Sql;
7+
8+
namespace YesSql.Extensions.SqlServer
9+
{
10+
public static class SqlServerDbOptionsExtensions
11+
{
12+
public static void UseSqlServer(
13+
this IDbProviderOptions options, string connectionString)
14+
{
15+
if (options == null)
16+
{
17+
throw new ArgumentNullException(nameof(options));
18+
}
19+
20+
var configuration = new Configuration
21+
{
22+
ConnectionFactory = new DbConnectionFactory<SqlConnection>(connectionString, true),
23+
DocumentStorageFactory = new SqlDocumentStorageFactory(),
24+
IsolationLevel = IsolationLevel.ReadUncommitted
25+
};
26+
27+
options.ProviderName = "SQL Server";
28+
options.Configuration = configuration;
29+
}
30+
}
31+
}

Diff for: samples/YesSql.Samples.Web/Models/BlogPost.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace Demo.Models
4+
{
5+
public class BlogPost
6+
{
7+
public int Id { get; set; }
8+
9+
public string Title { get; set; }
10+
11+
public string Author { get; set; }
12+
13+
public string Content { get; set; }
14+
15+
public DateTime PublishedUtc { get; set; }
16+
17+
public string[] Tags { get; set; }
18+
}
19+
}

Diff for: samples/YesSql.Samples.Web/Program.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.IO;
2+
using Microsoft.AspNetCore.Hosting;
3+
4+
namespace Demo
5+
{
6+
public class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
var host = new WebHostBuilder()
11+
.UseKestrel()
12+
.UseContentRoot(Directory.GetCurrentDirectory())
13+
.UseIISIntegration()
14+
.UseStartup<Startup>()
15+
.UseEnvironment(EnvironmentName.Development)
16+
.Build();
17+
18+
host.Run();
19+
}
20+
}
21+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:7910/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"YesSql.Samples.Web": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:7911"
25+
}
26+
}
27+
}

Diff for: samples/YesSql.Samples.Web/Startup.cs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Logging;
5+
using YesSql.Extensions.InMemory;
6+
using YesSql.Extensions.SqLite;
7+
using YesSql.Extensions.SqlServer;
8+
9+
namespace Demo
10+
{
11+
public class Startup
12+
{
13+
public void ConfigureServices(IServiceCollection services)
14+
{
15+
//services.AddDbProvider(options => options.UseInMemory());
16+
17+
//services.AddDbProvider(options =>
18+
// options.UseSqLite("Filename=YesSql.db"));
19+
20+
services.AddDbProvider(options =>
21+
options.UseSqlServer("Server=.;Database=YesSql;Integrated Security=True"));
22+
23+
services.AddMvc();
24+
}
25+
26+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
27+
{
28+
loggerFactory.AddConsole();
29+
30+
if (env.IsDevelopment())
31+
{
32+
app.UseDeveloperExceptionPage();
33+
}
34+
35+
app.UseMvc();
36+
}
37+
}
38+
}

Diff for: samples/YesSql.Samples.Web/Views/Home/Index.cshtml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@using Demo.Models
2+
3+
@model BlogPost
4+
5+
<h2>@Model.Title</h2>

0 commit comments

Comments
 (0)