Skip to content

Commit 43fae15

Browse files
committed
Refactored solution to reduce duplicates.
1 parent 824e592 commit 43fae15

File tree

11 files changed

+133
-59
lines changed

11 files changed

+133
-59
lines changed

src/AbpCompanyName.AbpProjectName.Core/AbpProjectNameConsts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
public class AbpProjectNameConsts
44
{
55
public const string LocalizationSourceName = "AbpProjectName";
6+
7+
public const string ConnectionStringName = "Default";
68
}
79
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections.Concurrent;
2+
using Abp.Extensions;
3+
using Microsoft.Extensions.Configuration;
4+
5+
namespace AbpCompanyName.AbpProjectName.Configuration
6+
{
7+
public static class AppConfigurations
8+
{
9+
private static readonly ConcurrentDictionary<string, IConfigurationRoot> ConfigurationCache;
10+
11+
static AppConfigurations()
12+
{
13+
ConfigurationCache = new ConcurrentDictionary<string, IConfigurationRoot>();
14+
}
15+
16+
public static IConfigurationRoot Get(string path, string environmentName = null)
17+
{
18+
var cacheKey = path + "#" + environmentName;
19+
return ConfigurationCache.GetOrAdd(
20+
cacheKey,
21+
_ => BuildConfiguration(path, environmentName)
22+
);
23+
}
24+
25+
private static IConfigurationRoot BuildConfiguration(string path, string environmentName = null)
26+
{
27+
var builder = new ConfigurationBuilder()
28+
.SetBasePath(path)
29+
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
30+
31+
if (!environmentName.IsNullOrWhiteSpace())
32+
{
33+
builder = builder.AddJsonFile($"appsettings.{environmentName}.json", optional: true);
34+
}
35+
36+
builder = builder.AddEnvironmentVariables();
37+
38+
return builder.Build();
39+
}
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace AbpCompanyName.AbpProjectName.Web
6+
{
7+
/// <summary>
8+
/// This class is used to find root path of the web project in;
9+
/// unit tests (to find views) and entity framework core command line commands (to find conn string).
10+
/// </summary>
11+
public static class WebContentDirectoryFinder
12+
{
13+
public static string CalculateContentRootFolder()
14+
{
15+
var coreAssemblyDirectoryPath = Path.GetDirectoryName(typeof(AbpProjectNameCoreModule).Assembly.Location);
16+
if (coreAssemblyDirectoryPath == null)
17+
{
18+
throw new ApplicationException("Could not find location of AbpCompanyName.AbpProjectName.Core assembly!");
19+
}
20+
21+
var directoryInfo = new DirectoryInfo(coreAssemblyDirectoryPath);
22+
while (!DirectoryContains(directoryInfo.FullName, "AbpCompanyName.AbpProjectName.sln"))
23+
{
24+
if (directoryInfo.Parent == null)
25+
{
26+
throw new ApplicationException("Could not find content root folder!");
27+
}
28+
29+
directoryInfo = directoryInfo.Parent;
30+
}
31+
32+
return Path.Combine(directoryInfo.FullName, @"src\AbpCompanyName.AbpProjectName.Web");
33+
}
34+
35+
private static bool DirectoryContains(string directory, string fileName)
36+
{
37+
return Directory.GetFiles(directory).Any(filePath => string.Equals(Path.GetFileName(filePath), fileName));
38+
}
39+
}
40+
}

src/AbpCompanyName.AbpProjectName.Core/project.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"version": "1.0.0.0-*",
33

44
"dependencies": {
5-
"Abp": "0.10.3"
5+
"Abp": "0.10.3",
6+
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
7+
"Microsoft.Extensions.Configuration.Json": "1.0.0"
68
},
79

810
"frameworks": {

src/AbpCompanyName.AbpProjectName.EntityFrameworkCore/EntityFrameworkCore/AbpProjectNameDbContextFactory.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
using Microsoft.EntityFrameworkCore;
1+
using AbpCompanyName.AbpProjectName.Configuration;
2+
using AbpCompanyName.AbpProjectName.Web;
3+
using Microsoft.EntityFrameworkCore;
24
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.Extensions.Configuration;
36

47
namespace AbpCompanyName.AbpProjectName.EntityFrameworkCore
58
{
6-
/* This class is needed to run "dotnet ef ..." commands from command line.
7-
*/
9+
/* This class is needed to run "dotnet ef ..." commands from command line on development. Not used anywhere else */
810
public class AbpProjectNameDbContextFactory : IDbContextFactory<AbpProjectNameDbContext>
911
{
1012
public AbpProjectNameDbContext Create(DbContextFactoryOptions options)
1113
{
12-
//TODO: Get connection string from a common place
1314
var builder = new DbContextOptionsBuilder<AbpProjectNameDbContext>();
14-
builder.UseSqlServer("Server=localhost; Database=AbpProjectNameDb; Trusted_Connection=True;");
15+
var configuration = AppConfigurations.Get(WebContentDirectoryFinder.CalculateContentRootFolder());
16+
17+
DbContextOptionsConfigurer.Configure(
18+
builder,
19+
configuration.GetConnectionString(AbpProjectNameConsts.ConnectionStringName)
20+
);
21+
1522
return new AbpProjectNameDbContext(builder.Options);
1623
}
1724
}

src/AbpCompanyName.AbpProjectName.EntityFrameworkCore/EntityFrameworkCore/AbpProjectNameEntityFrameworkCoreModule.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ namespace AbpCompanyName.AbpProjectName.EntityFrameworkCore
99
typeof(AbpEntityFrameworkCoreModule))]
1010
public class AbpProjectNameEntityFrameworkCoreModule : AbpModule
1111
{
12-
public override void PreInitialize()
13-
{
14-
Configuration.DefaultNameOrConnectionString = "Default";
15-
}
16-
1712
public override void Initialize()
1813
{
1914
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace AbpCompanyName.AbpProjectName.EntityFrameworkCore
4+
{
5+
public static class DbContextOptionsConfigurer
6+
{
7+
public static void Configure(
8+
DbContextOptionsBuilder<AbpProjectNameDbContext> dbContextOptions,
9+
string connectionString
10+
)
11+
{
12+
/* This is the single point to configure DbContextOptions for AbpProjectNameDbContext */
13+
dbContextOptions.UseSqlServer(connectionString);
14+
}
15+
}
16+
}

src/AbpCompanyName.AbpProjectName.Web/Startup/AbpProjectNameWebModule.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using System.Reflection;
22
using Abp.AspNetCore;
33
using Abp.AspNetCore.Configuration;
4+
using Abp.EntityFrameworkCore.Configuration;
45
using Abp.Modules;
6+
using AbpCompanyName.AbpProjectName.Configuration;
57
using AbpCompanyName.AbpProjectName.EntityFrameworkCore;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.Extensions.Configuration;
610

711
namespace AbpCompanyName.AbpProjectName.Web.Startup
812
{
@@ -12,8 +16,22 @@ namespace AbpCompanyName.AbpProjectName.Web.Startup
1216
typeof(AbpAspNetCoreModule))]
1317
public class AbpProjectNameWebModule : AbpModule
1418
{
19+
private readonly IConfigurationRoot _appConfiguration;
20+
21+
public AbpProjectNameWebModule(IHostingEnvironment env)
22+
{
23+
_appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName);
24+
}
25+
1526
public override void PreInitialize()
1627
{
28+
Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(AbpProjectNameConsts.ConnectionStringName);
29+
30+
Configuration.Modules.AbpEfCore().AddDbContext<AbpProjectNameDbContext>(options =>
31+
{
32+
DbContextOptionsConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
33+
});
34+
1735
Configuration.Navigation.Providers.Add<AbpProjectNameNavigationProvider>();
1836

1937
Configuration.Modules.AbpAspNetCore()

src/AbpCompanyName.AbpProjectName.Web/Startup/Startup.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
11
using System;
22
using Abp.AspNetCore;
33
using Abp.AspNetCore.Mvc;
4-
using AbpCompanyName.AbpProjectName.EntityFrameworkCore;
54
using Castle.Facilities.Logging;
65
using Microsoft.AspNetCore.Builder;
76
using Microsoft.AspNetCore.Hosting;
8-
using Microsoft.EntityFrameworkCore;
9-
using Microsoft.Extensions.Configuration;
107
using Microsoft.Extensions.DependencyInjection;
118
using Microsoft.Extensions.Logging;
129

1310
namespace AbpCompanyName.AbpProjectName.Web.Startup
1411
{
1512
public class Startup
1613
{
17-
public IConfigurationRoot Configuration { get; }
18-
19-
public Startup(IHostingEnvironment env)
20-
{
21-
var builder = new ConfigurationBuilder()
22-
.SetBasePath(env.ContentRootPath)
23-
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
24-
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
25-
.AddEnvironmentVariables();
26-
Configuration = builder.Build();
27-
}
28-
2914
public IServiceProvider ConfigureServices(IServiceCollection services)
3015
{
31-
services.AddDbContext<AbpProjectNameDbContext>(
32-
options => options.UseSqlServer(Configuration.GetConnectionString("Default"))
33-
);
34-
3516
services.AddMvc(options =>
3617
{
3718
options.AddAbp(services); //Add ABP infrastructure to MVC

src/AbpCompanyName.AbpProjectName.Web/project.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
"version": "1.0.0-preview2-final",
2121
"type": "build"
2222
},
23-
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
24-
"Microsoft.Extensions.Configuration.Json": "1.0.0",
2523
"Microsoft.Extensions.Logging": "1.0.0",
2624
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
2725
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",

0 commit comments

Comments
 (0)