Skip to content

Commit 7810dec

Browse files
committed
Project Init Commit
1 parent 9867788 commit 7810dec

File tree

137 files changed

+17154
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+17154
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.vs/
2+
IdentityServer/bin/
3+
CodeForFun.Core/obj/
4+
CodeForFun.Data/obj/
5+
CodeForFun.Services/obj/
6+
IdentityServer/obj/
7+
CodeForFun/.vscode/
8+
CodeForFun.Core/bin/
9+
CodeForFun.Data/bin/
10+
CodeForFun.Services/bin/
11+
IdentityServer/tempkey.rsa
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace CodeForFun.Core.SeedData
2+
{
3+
public interface IDbInitializer
4+
{
5+
void Initialize();
6+
}
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<RuntimeFrameworkVersion>2.1.3</RuntimeFrameworkVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.3" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.3" />
11+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\CodeForFun.Core\CodeForFun.Core.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<Folder Include="Migrations\" />
20+
<Folder Include="SeedData\" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using CodeForFun.Data.Extensions;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace CodeForFun.Data.DbContexts
5+
{
6+
public class CodeForFunDbContext : DbContext
7+
{
8+
public CodeForFunDbContext(DbContextOptions<CodeForFunDbContext> options) : base(options)
9+
{
10+
}
11+
12+
protected override void OnModelCreating(ModelBuilder modelBuilder)
13+
{
14+
base.OnModelCreating(modelBuilder);
15+
modelBuilder.AddEntityConfigurationsFromAssembly();
16+
}
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Design;
3+
using Microsoft.Extensions.Configuration;
4+
using System;
5+
6+
namespace CodeForFun.Data.DbContexts
7+
{
8+
public class DbContextFactory : IDesignTimeDbContextFactory<CodeForFunDbContext>
9+
{
10+
public CodeForFunDbContext CreateDbContext(string[] args)
11+
{
12+
var projectPath = AppDomain.CurrentDomain.BaseDirectory.Split(new string[] { @"bin\" }, StringSplitOptions.None)[0];
13+
var configuration = new ConfigurationBuilder()
14+
.SetBasePath(projectPath)
15+
.AddJsonFile("appsettings.local.json")
16+
.Build();
17+
18+
var connectionString = configuration.GetConnectionString("DefaultConnection");
19+
20+
var optionsBuilder = new DbContextOptionsBuilder<CodeForFunDbContext>();
21+
optionsBuilder.UseSqlServer(connectionString);
22+
23+
return new CodeForFunDbContext(optionsBuilder.Options);
24+
}
25+
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace CodeForFun.Data.Entities
2+
{
3+
public class BaseEntity<TId>
4+
{
5+
public TId Id { get; set; }
6+
}
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
7+
namespace CodeForFun.Data.Extensions
8+
{
9+
public static class ModelBuilderExtensions
10+
{
11+
public static void AddEntityConfigurationsFromAssembly(this ModelBuilder modelBuilder)
12+
{
13+
var entityTypeConfigs = GetEntityTypeConfigurations().Select(Activator.CreateInstance);
14+
15+
foreach (dynamic config in entityTypeConfigs)
16+
{
17+
modelBuilder.ApplyConfiguration(config);
18+
}
19+
20+
IEnumerable<Type> GetEntityTypeConfigurations()
21+
{
22+
return Assembly.GetExecutingAssembly().GetTypes().Where(
23+
type => type.IsClass && !type.IsAbstract && IsEntityTypeConfiguration(type)
24+
);
25+
26+
bool IsEntityTypeConfiguration(Type type)
27+
{
28+
return type.GetInterfaces().Any(_interface =>
29+
{
30+
return _interface.IsGenericType && _interface.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>);
31+
});
32+
};
33+
}
34+
}
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using CodeForFun.Data.DbContexts;
2+
using CodeForFun.Data.Repositories;
3+
using CodeForFun.Data.Repositories.Contracts;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace CodeForFun.Data.Extensions
9+
{
10+
public static class ServiceCollectionExtension
11+
{
12+
public static IServiceCollection AddDataModule(this IServiceCollection services)
13+
{
14+
// Configure DbContext
15+
services.AddDbContext<CodeForFunDbContext>(options =>
16+
{
17+
options.UseSqlServer(GetConnectionString());
18+
});
19+
20+
// Repository
21+
services.AddScoped(typeof(IRepository<>), typeof(BaseRepository<>));
22+
services.AddScoped(typeof(IRepository<,>), typeof(BaseRepository<,,>));
23+
24+
return services;
25+
26+
string GetConnectionString()
27+
{
28+
var configuration = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
29+
return configuration.GetConnectionString("DefaultConnection");
30+
}
31+
}
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using CodeForFun.Data.DbContexts;
2+
using CodeForFun.Data.Entities;
3+
using CodeForFun.Data.Repositories.Contracts;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.ChangeTracking;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
9+
namespace CodeForFun.Data.Repositories
10+
{
11+
public class BaseRepository<TContext, TEntity, TId> : IRepository<TEntity, TId>
12+
where TEntity : BaseEntity<TId>
13+
where TContext : DbContext
14+
{
15+
private readonly TContext _context;
16+
17+
public BaseRepository(TContext context) => _context = context;
18+
19+
public DbSet<TEntity> DbSet => _context.Set<TEntity>();
20+
21+
public IEnumerable<TEntity> GetAll() => DbSet.ToList();
22+
23+
public TEntity GetById(TId id) => DbSet.SingleOrDefault(x => x.Id.Equals(id));
24+
25+
public EntityEntry Create(TEntity entity) => DbSet.Add(entity);
26+
27+
public EntityEntry Update(TEntity entity) => DbSet.Update(entity);
28+
29+
public EntityEntry Delete(TEntity entity) => DbSet.Remove(entity);
30+
}
31+
32+
public class BaseRepository<TEntity> : BaseRepository<CodeForFunDbContext, TEntity, int> where TEntity : BaseEntity<int>
33+
{
34+
public BaseRepository(CodeForFunDbContext context) : base(context)
35+
{
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)