N0b0dy
N0b0dy
CC#
Created by N0b0dy on 8/5/2024 in #help
Certain services could not be instantiated due to iss
Hi , I am trying implement Unit of Work design pattern.
C# public class Repository<T> : IRepository<T> where T : class
{
protected readonly DbContext _context;
protected readonly DbSet<T> _dbSet;
protected readonly ILogger _logger;
public Repository(DbContext context, ILogger logger)
{
_context = context;
_dbSet = context.Set<T>();
_logger = logger;
}

public virtual async Task<T?> GetByIdAsync(int id)
{
var data = await _dbSet.FindAsync(id);

if (data == null) { return null; }
else { return data; }
}

public virtual async Task<IEnumerable<T>> GetAllAsync()
{
return await _dbSet.ToListAsync();
}

public virtual async Task AddAsync(IEnumerable<T> entities)
{
await _dbSet.AddRangeAsync(entities);
await _context.SaveChangesAsync();
}

public virtual async Task UpdateAsync(IEnumerable<T> entities)
{
_dbSet.UpdateRange(entities);
await _context.SaveChangesAsync();
}


}

public class DepartmentRepository : Repository<Department>, IDepartmentRepository
{
private readonly DbContext _context;
public DepartmentRepository(DbContext context, ILogger logger) : base(context, logger)
{
_context = context;
}


public override async Task<IEnumerable<Department>> GetAllAsync()
{
try
{
var result = await _dbSet.Where(x => x.IsActive == true).ToListAsync();
return result.AsEnumerable();
}
catch (Exception ex)
{
_logger.LogError((ex), "{Repo} GetAllAsync ", typeof(DepartmentRepository));
return null;
}
}
}

Program.cs
builder.Services.AddScoped<IDepartmentRepository, DepartmentRepository>();
C# public class Repository<T> : IRepository<T> where T : class
{
protected readonly DbContext _context;
protected readonly DbSet<T> _dbSet;
protected readonly ILogger _logger;
public Repository(DbContext context, ILogger logger)
{
_context = context;
_dbSet = context.Set<T>();
_logger = logger;
}

public virtual async Task<T?> GetByIdAsync(int id)
{
var data = await _dbSet.FindAsync(id);

if (data == null) { return null; }
else { return data; }
}

public virtual async Task<IEnumerable<T>> GetAllAsync()
{
return await _dbSet.ToListAsync();
}

public virtual async Task AddAsync(IEnumerable<T> entities)
{
await _dbSet.AddRangeAsync(entities);
await _context.SaveChangesAsync();
}

public virtual async Task UpdateAsync(IEnumerable<T> entities)
{
_dbSet.UpdateRange(entities);
await _context.SaveChangesAsync();
}


}

public class DepartmentRepository : Repository<Department>, IDepartmentRepository
{
private readonly DbContext _context;
public DepartmentRepository(DbContext context, ILogger logger) : base(context, logger)
{
_context = context;
}


public override async Task<IEnumerable<Department>> GetAllAsync()
{
try
{
var result = await _dbSet.Where(x => x.IsActive == true).ToListAsync();
return result.AsEnumerable();
}
catch (Exception ex)
{
_logger.LogError((ex), "{Repo} GetAllAsync ", typeof(DepartmentRepository));
return null;
}
}
}

Program.cs
builder.Services.AddScoped<IDepartmentRepository, DepartmentRepository>();
I am unable to locate the mistake here. Could someone please guide me here?
38 replies
CC#
Created by N0b0dy on 8/21/2022 in #help
Get method for API
public Order GetById(int orderId)
{
var customerData = _orders.FirstOrDefault(x => x.Id == orderId);
StandardDelivery delivery = new StandardDelivery(orderData: customerData);
delivery.State = StandardDeliveryState.Prepare;
delivery.State = StandardDeliveryState.Dispatch;
delivery.State = StandardDeliveryState.Delivery;
customerData.DeliveryDate = delivery.deliveryDate;
delivery.Dispose();

return customerData;
}
public Order GetById(int orderId)
{
var customerData = _orders.FirstOrDefault(x => x.Id == orderId);
StandardDelivery delivery = new StandardDelivery(orderData: customerData);
delivery.State = StandardDeliveryState.Prepare;
delivery.State = StandardDeliveryState.Dispatch;
delivery.State = StandardDeliveryState.Delivery;
customerData.DeliveryDate = delivery.deliveryDate;
delivery.Dispose();

return customerData;
}
Is it an ideal approach to set properties values before getting sending the JSON?
7 replies
CC#
Created by N0b0dy on 8/20/2022 in #help
[Help] understanding Architecture of a parcel system
Hi, I am working on a post system which has two classes, standard and express. I am using Finite state machine to track my order from dispatch to Deliver. My architecture is like
abstract class post
abstract class post
where I have all the details about the order
class standPost:post
class standPost:post
standpost inheritances all the properties of the post class and I have added a couple status for this class. Also, I have different status for standard deliver, such as dispatch, in transit, and many more. Is there any way that I can change these standPost status automatically, so the client get notification? If someone can help me or guide, that would be appreciated. thanks
87 replies