Aokiri 🐸
Aokiri 🐸
CC#
Created by Aokiri 🐸 on 11/25/2023 in #help
Difference between await & GetAwaiter()/GetResult()
I'm actually doing a simple task that checks if a role exists in the database and create it if the role doesn't exists. Code:
if (!_roleManager.RoleExistsAsync(SD.Role_Customer).GetAwaiter().GetResult())
{
_roleManager.CreateAsync(new IdentityRole(SD.Role_User)).GetAwaiter().GetResult();
_roleManager.CreateAsync(new IdentityRole(SD.Role_Moderator)).GetAwaiter().GetResult();
_roleManager.CreateAsync(new IdentityRole(SD.Role_Admin)).GetAwaiter().GetResult();
_roleManager.CreateAsync(new IdentityRole(SD.Role_Invited)).GetAwaiter().GetResult();
}
if (!_roleManager.RoleExistsAsync(SD.Role_Customer).GetAwaiter().GetResult())
{
_roleManager.CreateAsync(new IdentityRole(SD.Role_User)).GetAwaiter().GetResult();
_roleManager.CreateAsync(new IdentityRole(SD.Role_Moderator)).GetAwaiter().GetResult();
_roleManager.CreateAsync(new IdentityRole(SD.Role_Admin)).GetAwaiter().GetResult();
_roleManager.CreateAsync(new IdentityRole(SD.Role_Invited)).GetAwaiter().GetResult();
}
Here's how it looks like with await:
if (!await _roleManager.RoleExistsAsync(SD.Role_Customer).GetAwaiter().GetResult())
{
await _roleManager.CreateAsync(new IdentityRole(SD.Role_User));
await _roleManager.CreateAsync(new IdentityRole(SD.Role_Moderator));
await _roleManager.CreateAsync(new IdentityRole(SD.Role_Admin));
await _roleManager.CreateAsync(new IdentityRole(SD.Role_Invited));
}
if (!await _roleManager.RoleExistsAsync(SD.Role_Customer).GetAwaiter().GetResult())
{
await _roleManager.CreateAsync(new IdentityRole(SD.Role_User));
await _roleManager.CreateAsync(new IdentityRole(SD.Role_Moderator));
await _roleManager.CreateAsync(new IdentityRole(SD.Role_Admin));
await _roleManager.CreateAsync(new IdentityRole(SD.Role_Invited));
}
10 replies
CC#
Created by Aokiri 🐸 on 10/13/2023 in #help
Confusion about Repository Pattern and Unit of Work.
Hi there. I recently applied the Repository Pattern and Unit of Work in an API I'm creating, and everything was going well. However, I got stuck trying to understand how to pass the Database Context to the implementation of one of my interfaces. I'm trying to grasp it, but I can't figure it out. I'll attach few code for reference: (Program.cs) Here is where I'll apply Dependency Injection for my ICategoryRepository with a Scoped lifetime:
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
(DataAccess/Repository/Repository.cs) Here is my implementation of my Repository Interface (only constructor and fields):
public class Repository<T> : IRepository<T> where T : class
{
private readonly ApplicationDbContext _db;
internal DbSet<T> dbSet;

public Repository(ApplicationDbContext db)
{
_db = db;
this.dbSet = _db.Set<T>();
// For example, _db.Categories == dbSet
}
public class Repository<T> : IRepository<T> where T : class
{
private readonly ApplicationDbContext _db;
internal DbSet<T> dbSet;

public Repository(ApplicationDbContext db)
{
_db = db;
this.dbSet = _db.Set<T>();
// For example, _db.Categories == dbSet
}
(DataAccess/Repository/CategoryRepository.cs) And here is my implementation of my Category Repository Interface (only constructor, as always):
public class CategoryRepository : Repository<Category>, ICategoryRepository
{
private readonly ApplicationDbContext _db;

public CategoryRepository(ApplicationDbContext db) : base(db)
{
_db = db;
}
public class CategoryRepository : Repository<Category>, ICategoryRepository
{
private readonly ApplicationDbContext _db;

public CategoryRepository(ApplicationDbContext db) : base(db)
{
_db = db;
}
Look how these two has inyected with the DbContext, but here comes my confusion... In the controller, I created an ICategoryRepository object, but I don't understand how it pass the DbContext to the controller itself...
19 replies