Skip to content

Commit eccac2b

Browse files
committed
[ASP.NET Core] Code quality fixes
1 parent 0c43b2c commit eccac2b

File tree

10 files changed

+45
-38
lines changed

10 files changed

+45
-38
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace AspNetCoreTemplate.Data
2+
{
3+
using Microsoft.AspNetCore.Identity;
4+
5+
public static class IdentityOptionsProvider
6+
{
7+
public static void GetIdentityOptions(IdentityOptions options)
8+
{
9+
options.Password.RequireDigit = false;
10+
options.Password.RequireLowercase = false;
11+
options.Password.RequireUppercase = false;
12+
options.Password.RequireNonAlphanumeric = false;
13+
options.Password.RequiredLength = 6;
14+
}
15+
}
16+
}

ASP.NET Core/Services/AspNetCoreTemplate.Services.Data/ISettingsService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
namespace AspNetCoreTemplate.Services.Data
22
{
33
using System.Collections.Generic;
4+
using System.Threading.Tasks;
45

56
public interface ISettingsService
67
{
7-
int GetCount();
8+
Task<int> GetCountAsync();
89

910
IEnumerable<T> GetAll<T>();
1011
}

ASP.NET Core/Services/AspNetCoreTemplate.Services.Data/SettingsService.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
{
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Threading.Tasks;
56

67
using AspNetCoreTemplate.Data.Common.Repositories;
78
using AspNetCoreTemplate.Data.Models;
89
using AspNetCoreTemplate.Services.Mapping;
910

11+
using Microsoft.EntityFrameworkCore;
12+
1013
public class SettingsService : ISettingsService
1114
{
1215
private readonly IDeletableEntityRepository<Setting> settingsRepository;
@@ -16,9 +19,9 @@ public SettingsService(IDeletableEntityRepository<Setting> settingsRepository)
1619
this.settingsRepository = settingsRepository;
1720
}
1821

19-
public int GetCount()
22+
public async Task<int> GetCountAsync()
2023
{
21-
return this.settingsRepository.All().Count();
24+
return await this.settingsRepository.All().CountAsync();
2225
}
2326

2427
public IEnumerable<T> GetAll<T>()

ASP.NET Core/Tests/AspNetCoreTemplate.Services.Data.Tests/SettingsServiceTests.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class SettingsServiceTests
1919
{
2020
[Fact]
21-
public void GetCountShouldReturnCorrectNumber()
21+
public async Task GetCountShouldReturnCorrectNumber()
2222
{
2323
var repository = new Mock<IDeletableEntityRepository<Setting>>();
2424
repository.Setup(r => r.All()).Returns(new List<Setting>
@@ -28,16 +28,15 @@ public void GetCountShouldReturnCorrectNumber()
2828
new Setting(),
2929
}.AsQueryable());
3030
var service = new SettingsService(repository.Object);
31-
Assert.Equal(3, service.GetCount());
31+
Assert.Equal(3, await service.GetCountAsync());
3232
repository.Verify(x => x.All(), Times.Once);
3333
}
3434

3535
[Fact]
3636
public async Task GetCountShouldReturnCorrectNumberUsingDbContext()
3737
{
3838
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
39-
.UseInMemoryDatabase(databaseName: "Find_User_Database") // Give a Unique name to the DB
40-
.Options;
39+
.UseInMemoryDatabase(databaseName: "SettingsTestDb").Options;
4140
var dbContext = new ApplicationDbContext(options);
4241
dbContext.Settings.Add(new Setting());
4342
dbContext.Settings.Add(new Setting());
@@ -46,8 +45,7 @@ public async Task GetCountShouldReturnCorrectNumberUsingDbContext()
4645

4746
var repository = new EfDeletableEntityRepository<Setting>(dbContext);
4847
var service = new SettingsService(repository);
49-
var count = service.GetCount();
50-
Assert.Equal(3, count);
48+
Assert.Equal(3, await service.GetCountAsync());
5149
}
5250
}
5351
}

ASP.NET Core/Tests/Sandbox/Program.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Diagnostics;
55
using System.IO;
6+
using System.Threading.Tasks;
67

78
using AspNetCoreTemplate.Data;
89
using AspNetCoreTemplate.Data.Common;
@@ -42,18 +43,20 @@ public static int Main(string[] args)
4243
serviceProvider = serviceScope.ServiceProvider;
4344

4445
return Parser.Default.ParseArguments<SandboxOptions>(args).MapResult(
45-
opts => SandboxCode(opts, serviceProvider),
46+
opts => SandboxCode(opts, serviceProvider).GetAwaiter().GetResult(),
4647
_ => 255);
4748
}
4849
}
4950

50-
private static int SandboxCode(SandboxOptions options, IServiceProvider serviceProvider)
51+
private static async Task<int> SandboxCode(SandboxOptions options, IServiceProvider serviceProvider)
5152
{
5253
var sw = Stopwatch.StartNew();
54+
5355
var settingsService = serviceProvider.GetService<ISettingsService>();
54-
Console.WriteLine($"Count of settings: {settingsService.GetCount()}");
56+
Console.WriteLine($"Count of settings: {await settingsService.GetCountAsync()}");
57+
5558
Console.WriteLine(sw.Elapsed);
56-
return 0;
59+
return await Task.FromResult(0);
5760
}
5861

5962
private static void ConfigureServices(ServiceCollection services)
@@ -64,19 +67,13 @@ private static void ConfigureServices(ServiceCollection services)
6467
.Build();
6568

6669
services.AddSingleton<IConfiguration>(configuration);
70+
6771
services.AddDbContext<ApplicationDbContext>(
6872
options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"))
6973
.UseLoggerFactory(new LoggerFactory()));
7074

71-
services.AddDefaultIdentity<ApplicationUser>(
72-
options =>
73-
{
74-
options.Password.RequireDigit = false;
75-
options.Password.RequireLowercase = false;
76-
options.Password.RequireUppercase = false;
77-
options.Password.RequireNonAlphanumeric = false;
78-
options.Password.RequiredLength = 6;
79-
}).AddRoles<ApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>();
75+
services.AddDefaultIdentity<ApplicationUser>(IdentityOptionsProvider.GetIdentityOptions)
76+
.AddRoles<ApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>();
8077

8178
services.AddScoped(typeof(IDeletableEntityRepository<>), typeof(EfDeletableEntityRepository<>));
8279
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));

ASP.NET Core/Web/AspNetCoreTemplate.Web/Areas/Administration/Controllers/DashboardController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace AspNetCoreTemplate.Web.Areas.Administration.Controllers
22
{
3+
using System.Threading.Tasks;
4+
35
using AspNetCoreTemplate.Services.Data;
46
using AspNetCoreTemplate.Web.Areas.Administration.ViewModels.Dashboard;
57

@@ -14,9 +16,9 @@ public DashboardController(ISettingsService settingsService)
1416
this.settingsService = settingsService;
1517
}
1618

17-
public IActionResult Index()
19+
public async Task<IActionResult> Index()
1820
{
19-
var viewModel = new IndexViewModel { SettingsCount = this.settingsService.GetCount(), };
21+
var viewModel = new IndexViewModel { SettingsCount = await this.settingsService.GetCountAsync(), };
2022
return this.View(viewModel);
2123
}
2224
}

ASP.NET Core/Web/AspNetCoreTemplate.Web/Controllers/SettingsController.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
namespace AspNetCoreTemplate.Web.Controllers
22
{
33
using System;
4-
using System.Linq;
54
using System.Threading.Tasks;
65

76
using AspNetCoreTemplate.Data.Common.Repositories;
87
using AspNetCoreTemplate.Data.Models;
98
using AspNetCoreTemplate.Services.Data;
10-
using AspNetCoreTemplate.Services.Mapping;
119
using AspNetCoreTemplate.Web.ViewModels.Settings;
1210

1311
using Microsoft.AspNetCore.Mvc;

ASP.NET Core/Web/AspNetCoreTemplate.Web/Startup.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,8 @@ public void ConfigureServices(IServiceCollection services)
3636
services.AddDbContext<ApplicationDbContext>(
3737
options => options.UseSqlServer(this.configuration.GetConnectionString("DefaultConnection")));
3838

39-
services.AddDefaultIdentity<ApplicationUser>(
40-
options =>
41-
{
42-
options.Password.RequireDigit = false;
43-
options.Password.RequireLowercase = false;
44-
options.Password.RequireUppercase = false;
45-
options.Password.RequireNonAlphanumeric = false;
46-
options.Password.RequiredLength = 6;
47-
}).AddRoles<ApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>();
48-
39+
services.AddDefaultIdentity<ApplicationUser>(IdentityOptionsProvider.GetIdentityOptions)
40+
.AddRoles<ApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>();
4941

5042
services.Configure<CookiePolicyOptions>(
5143
options =>

ASP.NET Core/Web/AspNetCoreTemplate.Web/Views/Shared/_CookieConsentPartial.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<script>
1818
(function () {
1919
var button = document.querySelector("#cookieConsent button[data-cookie-string]");
20-
button.addEventListener("click", function (event) {
20+
button.addEventListener("click", function () {
2121
document.cookie = button.dataset.cookieString;
2222
}, false);
2323
})();

ASP.NET Core/Web/AspNetCoreTemplate.Web/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</head>
1717
<body>
1818
<header>
19-
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
19+
<nav class="navbar navbar-expand-sm navbar-light bg-white border-bottom box-shadow mb-3">
2020
<div class="container">
2121
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">AspNetCoreTemplate</a>
2222
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"

0 commit comments

Comments
 (0)