Skip to content

Commit 6afc455

Browse files
committed
[ASP.NET Core] Code improvements
1 parent 0fcae15 commit 6afc455

File tree

5 files changed

+20
-25
lines changed

5 files changed

+20
-25
lines changed

ASP.NET Core/Data/AspNetCoreTemplate.Data/DesignTimeDbContextFactory.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using Microsoft.EntityFrameworkCore;
66
using Microsoft.EntityFrameworkCore.Design;
7-
using Microsoft.EntityFrameworkCore.Diagnostics;
87
using Microsoft.Extensions.Configuration;
98

109
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
@@ -17,9 +16,7 @@ public ApplicationDbContext CreateDbContext(string[] args)
1716
.Build();
1817

1918
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
20-
2119
var connectionString = configuration.GetConnectionString("DefaultConnection");
22-
2320
builder.UseSqlServer(connectionString);
2421

2522
return new ApplicationDbContext(builder.Options);

ASP.NET Core/Data/AspNetCoreTemplate.Data/EfExpressionHelper.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ public static Expression<Func<TEntity, bool>> BuildByIdPredicate<TEntity>(
3131
}
3232

3333
var entityType = typeof(TEntity);
34-
3534
var entityParameter = Expression.Parameter(entityType, "e");
36-
3735
var keyProperties = dbContext.Model.FindEntityType(entityType).FindPrimaryKey().Properties;
38-
3936
var predicate = BuildPredicate(keyProperties, new ValueBuffer(id), entityParameter);
4037

4138
return Expression.Lambda<Func<TEntity, bool>>(predicate, entityParameter);
@@ -52,18 +49,14 @@ private static BinaryExpression BuildPredicate(
5249
for (var i = 0; i < keyProperties.Count; i++)
5350
{
5451
var property = keyProperties[i];
55-
var equalsExpression =
56-
Expression.Equal(
57-
Expression.Call(
58-
EfPropertyMethod.MakeGenericMethod(property.ClrType),
59-
entityParameter,
60-
Expression.Constant(property.Name, StringType)),
61-
Expression.Convert(
62-
Expression.Call(
63-
keyValuesConstant,
64-
ValueBufferGetValueMethod,
65-
Expression.Constant(i)),
66-
property.ClrType));
52+
var equalsExpression = Expression.Equal(
53+
Expression.Call(
54+
EfPropertyMethod.MakeGenericMethod(property.ClrType),
55+
entityParameter,
56+
Expression.Constant(property.Name, StringType)),
57+
Expression.Convert(
58+
Expression.Call(keyValuesConstant, ValueBufferGetValueMethod, Expression.Constant(i)),
59+
property.ClrType));
6760

6861
predicate = predicate == null ? equalsExpression : Expression.AndAlso(predicate, equalsExpression);
6962
}

ASP.NET Core/Data/AspNetCoreTemplate.Data/Repositories/EfDeletableEntityRepository.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ public EfDeletableEntityRepository(ApplicationDbContext context)
2727

2828
public Task<TEntity> GetByIdWithDeletedAsync(params object[] id)
2929
{
30-
var byIdPredicate = EfExpressionHelper.BuildByIdPredicate<TEntity>(this.Context, id);
31-
32-
return this.AllWithDeleted().FirstOrDefaultAsync(byIdPredicate);
30+
var getByIdPredicate = EfExpressionHelper.BuildByIdPredicate<TEntity>(this.Context, id);
31+
return this.AllWithDeleted().FirstOrDefaultAsync(getByIdPredicate);
3332
}
3433

3534
public void HardDelete(TEntity entity) => base.Delete(entity);
@@ -38,15 +37,13 @@ public void Undelete(TEntity entity)
3837
{
3938
entity.IsDeleted = false;
4039
entity.DeletedOn = null;
41-
4240
this.Update(entity);
4341
}
4442

4543
public override void Delete(TEntity entity)
4644
{
4745
entity.IsDeleted = true;
4846
entity.DeletedOn = DateTime.UtcNow;
49-
5047
this.Update(entity);
5148
}
5249
}

ASP.NET Core/Data/AspNetCoreTemplate.Data/Repositories/EfRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public EfRepository(ApplicationDbContext context)
2525

2626
public virtual IQueryable<TEntity> AllAsNoTracking() => this.DbSet.AsNoTracking();
2727

28-
public virtual async Task AddAsync(TEntity entity) => await this.DbSet.AddAsync(entity);
28+
public virtual Task AddAsync(TEntity entity) => this.DbSet.AddAsync(entity).AsTask();
2929

3030
public virtual void Update(TEntity entity)
3131
{

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
namespace AspNetCoreTemplate.Web.Controllers
22
{
3+
using System.Diagnostics;
4+
5+
using AspNetCoreTemplate.Web.ViewModels;
6+
37
using Microsoft.AspNetCore.Mvc;
48

59
public class HomeController : BaseController
@@ -15,6 +19,10 @@ public IActionResult Privacy()
1519
}
1620

1721
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
18-
public IActionResult Error() => this.View();
22+
public IActionResult Error()
23+
{
24+
return this.View(
25+
new ErrorViewModel { RequestId = Activity.Current?.Id ?? this.HttpContext.TraceIdentifier });
26+
}
1927
}
2028
}

0 commit comments

Comments
 (0)