Skip to content

Commit 8c49b70

Browse files
committed
#1 continue with the architecture
1 parent 28424ac commit 8c49b70

22 files changed

+452
-72
lines changed

BlogCoreSolution.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlogCore.Core", "src\BlogCo
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlogCore.Infrastructure", "src\BlogCore.Infrastructure\BlogCore.Infrastructure.csproj", "{5ACFBEA6-8815-4087-9BE4-84476D4229CF}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlogCore.Infrastructure.MigrationConsole", "src\BlogCore.Infrastructure.Migration\BlogCore.Infrastructure.MigrationConsole.csproj", "{F8C47864-9780-4FBE-99D0-8B9D2EBDB39B}"
13+
EndProject
1214
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5CD78C25-100E-41EA-BCA5-698FAEBCAC31}"
1315
ProjectSection(SolutionItems) = preProject
1416
.gitignore = .gitignore
@@ -34,6 +36,10 @@ Global
3436
{5ACFBEA6-8815-4087-9BE4-84476D4229CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
3537
{5ACFBEA6-8815-4087-9BE4-84476D4229CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
3638
{5ACFBEA6-8815-4087-9BE4-84476D4229CF}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{F8C47864-9780-4FBE-99D0-8B9D2EBDB39B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40+
{F8C47864-9780-4FBE-99D0-8B9D2EBDB39B}.Debug|Any CPU.Build.0 = Debug|Any CPU
41+
{F8C47864-9780-4FBE-99D0-8B9D2EBDB39B}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{F8C47864-9780-4FBE-99D0-8B9D2EBDB39B}.Release|Any CPU.Build.0 = Release|Any CPU
3743
EndGlobalSection
3844
GlobalSection(SolutionProperties) = preSolution
3945
HideSolutionNode = FALSE

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
# blog-core
1+
### blog-core
22
The mini blog that using the modern architecture
33
Inspired by https://github.com/ardalis/ddd-guestbook
4+
5+
### Database migration
6+
7+
`dotnet ef migrations add InitDatabase`
8+
`dotnet run`

src/BlogCore.Core/BlogCore.Core.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
</ItemGroup>
1010

1111
<ItemGroup>
12+
<PackageReference Include="System.Reactive" Version="3.1.1" />
13+
<PackageReference Include="System.Reactive.Linq" Version="3.1.1" />
1214
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.3.0" />
1315
</ItemGroup>
1416

src/BlogCore.Core/BlogFeature/Blog.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,12 @@
22
{
33
public class Blog : EntityBase
44
{
5-
public Blog(string title, string description, string theme, string image, int postsPerPage, int daysToComment, bool moderateComments)
6-
{
7-
Title = title;
8-
Description = description;
9-
Theme = theme;
10-
Image = image;
11-
PostsPerPage = postsPerPage;
12-
DaysToComment = daysToComment;
13-
ModerateComments = moderateComments;
14-
}
15-
16-
public string Title { get; private set; }
17-
public string Description { get; private set; }
18-
public string Theme { get; private set; }
19-
public string Image { get; private set; }
20-
public int PostsPerPage { get; private set; }
21-
public int DaysToComment { get; private set; }
22-
public bool ModerateComments { get; private set; }
5+
public string Title { get; set; }
6+
public string Description { get; set; }
7+
public string Theme { get; set; }
8+
public string Image { get; set; }
9+
public int PostsPerPage { get; set; }
10+
public int DaysToComment { get; set; }
11+
public bool ModerateComments { get; set; }
2312
}
2413
}

src/BlogCore.Core/BlogFeature/IBlogGateway.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace BlogCore.Core.BlogFeature
4+
{
5+
public interface IBlogService
6+
{
7+
IObservable<Blog> GetBlogs();
8+
}
9+
}

src/BlogCore.Core/BlogFeature/Impl/BlogGateway.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Reactive.Linq;
3+
4+
namespace BlogCore.Core.BlogFeature.Impl
5+
{
6+
public class BlogService : IBlogService
7+
{
8+
private readonly IRepository<Blog> _blogRepo;
9+
private readonly IDomainEventDispatcher _dispatcher;
10+
11+
public BlogService(IRepository<Blog> blogRepo, IDomainEventDispatcher dispatcher)
12+
{
13+
_blogRepo = blogRepo;
14+
_dispatcher = dispatcher;
15+
}
16+
17+
public IObservable<Blog> GetBlogs()
18+
{
19+
return _blogRepo.List().ToObservable();
20+
}
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp1.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Autofac" Version="4.5.0" />
10+
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.0.0" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="1.1.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational.Design" Version="1.1.1" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
16+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
17+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
18+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
19+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<ProjectReference Include="..\BlogCore.Core\BlogCore.Core.csproj" />
28+
<ProjectReference Include="..\BlogCore.Infrastructure\BlogCore.Infrastructure.csproj" />
29+
</ItemGroup>
30+
31+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Reflection;
3+
using BlogCore.Infrastructure.Data;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.Infrastructure;
6+
using Microsoft.Extensions.Configuration;
7+
8+
namespace BlogCore.Infrastructure.MigrationConsole
9+
{
10+
public class BlogCoreDbContextFactory : IDbContextFactory<BlogCoreDbContext>
11+
{
12+
public BlogCoreDbContext Create(DbContextFactoryOptions options)
13+
{
14+
var builder = new ConfigurationBuilder()
15+
.SetBasePath(options.ContentRootPath)
16+
.AddJsonFile("appsettings.json")
17+
.AddJsonFile($"appsettings.{options.EnvironmentName}.json", true)
18+
.AddEnvironmentVariables();
19+
20+
var config = builder.Build();
21+
var connstr = config.GetConnectionString("DefaultConnection");
22+
23+
if (string.IsNullOrWhiteSpace(connstr))
24+
throw new InvalidOperationException("Could not find a connection string named '(DefaultConnection)'.");
25+
26+
if (string.IsNullOrEmpty(connstr))
27+
throw new InvalidOperationException($"{nameof(connstr)} is null or empty.");
28+
29+
var migrationsAssembly = typeof(BlogCoreDbContextFactory).GetTypeInfo().Assembly.GetName().Name;
30+
var optionsBuilder = new DbContextOptionsBuilder<BlogCoreDbContext>();
31+
optionsBuilder.UseSqlServer(connstr, b => b.MigrationsAssembly(migrationsAssembly));
32+
33+
return new BlogCoreDbContext(optionsBuilder.Options);
34+
}
35+
}
36+
}

src/BlogCore.Infrastructure.Migration/Migrations/20170421151229_InitDatabase.Designer.cs

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.EntityFrameworkCore.Migrations;
4+
using Microsoft.EntityFrameworkCore.Metadata;
5+
6+
namespace BlogCore.Infrastructure.MigrationConsole.Migrations
7+
{
8+
public partial class InitDatabase : Migration
9+
{
10+
protected override void Up(MigrationBuilder migrationBuilder)
11+
{
12+
migrationBuilder.CreateTable(
13+
name: "Blog",
14+
columns: table => new
15+
{
16+
Id = table.Column<int>(nullable: false)
17+
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
18+
DaysToComment = table.Column<int>(nullable: false),
19+
Description = table.Column<string>(nullable: true),
20+
Image = table.Column<string>(nullable: true),
21+
ModerateComments = table.Column<bool>(nullable: false),
22+
PostsPerPage = table.Column<int>(nullable: false),
23+
Theme = table.Column<string>(nullable: true),
24+
Title = table.Column<string>(nullable: true)
25+
},
26+
constraints: table =>
27+
{
28+
table.PrimaryKey("PK_Blog", x => x.Id);
29+
});
30+
31+
migrationBuilder.CreateTable(
32+
name: "Comment",
33+
columns: table => new
34+
{
35+
Id = table.Column<int>(nullable: false)
36+
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
37+
},
38+
constraints: table =>
39+
{
40+
table.PrimaryKey("PK_Comment", x => x.Id);
41+
});
42+
43+
migrationBuilder.CreateTable(
44+
name: "Post",
45+
columns: table => new
46+
{
47+
Id = table.Column<int>(nullable: false)
48+
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
49+
},
50+
constraints: table =>
51+
{
52+
table.PrimaryKey("PK_Post", x => x.Id);
53+
});
54+
}
55+
56+
protected override void Down(MigrationBuilder migrationBuilder)
57+
{
58+
migrationBuilder.DropTable(
59+
name: "Blog");
60+
61+
migrationBuilder.DropTable(
62+
name: "Comment");
63+
64+
migrationBuilder.DropTable(
65+
name: "Post");
66+
}
67+
}
68+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Infrastructure;
4+
using Microsoft.EntityFrameworkCore.Metadata;
5+
using Microsoft.EntityFrameworkCore.Migrations;
6+
using BlogCore.Infrastructure.Data;
7+
8+
namespace BlogCore.Infrastructure.MigrationConsole.Migrations
9+
{
10+
[DbContext(typeof(BlogCoreDbContext))]
11+
partial class BlogCoreDbContextModelSnapshot : ModelSnapshot
12+
{
13+
protected override void BuildModel(ModelBuilder modelBuilder)
14+
{
15+
modelBuilder
16+
.HasAnnotation("ProductVersion", "1.1.1")
17+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
18+
19+
modelBuilder.Entity("BlogCore.Core.BlogFeature.Blog", b =>
20+
{
21+
b.Property<int>("Id")
22+
.ValueGeneratedOnAdd();
23+
24+
b.Property<int>("DaysToComment");
25+
26+
b.Property<string>("Description");
27+
28+
b.Property<string>("Image");
29+
30+
b.Property<bool>("ModerateComments");
31+
32+
b.Property<int>("PostsPerPage");
33+
34+
b.Property<string>("Theme");
35+
36+
b.Property<string>("Title");
37+
38+
b.HasKey("Id");
39+
40+
b.ToTable("Blog");
41+
});
42+
43+
modelBuilder.Entity("BlogCore.Core.PostFeature.Comment", b =>
44+
{
45+
b.Property<int>("Id")
46+
.ValueGeneratedOnAdd();
47+
48+
b.HasKey("Id");
49+
50+
b.ToTable("Comment");
51+
});
52+
53+
modelBuilder.Entity("BlogCore.Core.PostFeature.Post", b =>
54+
{
55+
b.Property<int>("Id")
56+
.ValueGeneratedOnAdd();
57+
58+
b.HasKey("Id");
59+
60+
b.ToTable("Post");
61+
});
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)