i want to send commend in Firestore using a API .net core 3.1, and i am working using clean architecture. and an CQRS pattern. like this:
namespace SmartRestaurant.Infrastructure.Services
{
public class FirebaseConfig
{
public string BasePath { get; set; }
}
public class FirebaseRepository : IFirebaseRepository
{
readonly string _DataBaseBasepath;
readonly FirestoreDb _db;
public FirebaseRepository(IOptions<FirebaseConfig> conf)
{
_DataBaseBasepath = conf.Value.BasePath;
if (string.IsNullOrEmpty(_DataBaseBasepath))
{
throw new ArgumentNullException("fireBase Path not found in appsettings");
}
var pathConfigFile = Path.Combine(AppContext.BaseDirectory, "jsonProjectConfogFile.json");
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", pathConfigFile);
_db = FirestoreDb.Create("^projectName");
}
public async Task<T> AddAsync<T>(string path, T data, CancellationToken cancellationToken)
{
try
{
DocumentReference doc = _db.Document(_DataBaseBasepath + "/" + path);
var objectTosend = getOrderToDictionary(data);
await doc.SetAsync(objectTosend, null, cancellationToken);
return data;
}
catch (Exception exe)
{
throw exe;
}
}
public async Task<T> UpdateAsync<T>(string path, T data, CancellationToken cancellationToken)
{
try
{
DocumentReference doc = _db.Document(_DataBaseBasepath + "/" + path);
var objectTosend = getOrderToDictionary(data);
await doc.UpdateAsync(objectTosend, null, cancellationToken);
return data;
}
catch (Exception exe)
{
throw exe;
}
}
}
}
that code is in the infrastrecture layer and the interface of this service is in the application layer
public interface IFirebaseRepository
{
Task<T> AddAsync<T>(string path,T data,CancellationToken cancellationToken);
Task<T> UpdateAsync<T>(string path,T data, CancellationToken cancellationToken);
}
the injection of this service is in the infrastrecture layer like this
services.AddTransient < IFirebaseRepository, FirebaseRepository> ();
i use this service in the application layer like this :
public class OrdersCommandsHandlers : IRequestHandler<CreateOrderCommand, OrderDto>,
IRequestHandler<UpdateOrderCommand, NoContent>,
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IUserService _userService;
private readonly IFirebaseRepository _fireBase;
private readonly string CreateAction = "CreateAction";
private readonly string UpdateAction = "UpdateAction";
public OrdersCommandsHandlers(IApplicationDbContext context,
IMapper mapper,
IUserService userService,
IFirebaseRepository fireBase)
{
_context = context;
_mapper = mapper;
_userService = userService;
_fireBase = fireBase;
}
public async Task<OrderDto> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
{
var validator = new CreateOrderCommandValidator();
var result = await validator.ValidateAsync(request, cancellationToken).ConfigureAwait(false);
if (!result.IsValid) throw new ValidationException(result);
....
await _context.SaveChangesAsync(cancellationToken);
var orderDto = _mapper.Map<OrderDto>(order);
orderDto.CurrencyExchange = CurrencyConverter.GetDefaultCurrencyExchangeList(orderDto.TotalToPay, foodBusiness.DefaultCurrency);
var path = request.FoodBusinessId + "/Orders/" + orderDto.OrderId;
await _fireBase.AddAsync(path, orderDto, cancellationToken);
......
return _mapper.Map<OrderDto>(newOrder);
}
public async Task<NoContent> Handle(UpdateOrderCommand request, CancellationToken cancellationToken)
{
var validator = new UpdateOrderCommandValidator();
var result = await validator.ValidateAsync(request, cancellationToken).ConfigureAwait(false);
if (!result.IsValid) throw new ValidationException(result);
......
var orderDto = _mapper.Map<OrderDto>(order);
var foodBusiness = await _context.FoodBusinesses.FindAsync(order.FoodBusinessId);
if (foodBusiness != null)
orderDto.CurrencyExchange = CurrencyConverter.GetDefaultCurrencyExchangeList(orderDto.TotalToPay, foodBusiness.DefaultCurrency);
var path = order.FoodBusinessId + "/Orders/" + order.OrderId;
await _fireBase.UpdateAsync(path,orderDto, cancellationToken);
return default;
}
}
when i deploiye this code in the server i have this erreur :
Error constructing handler for request of type MediatR.IRequestHandler`2[SmartRestaurant.Application.Orders.Commands.CreateOrderCommand,SmartRestaurant.Application.Common.Dtos.OrdersDtos.OrderDto]. Register your handlers with the container. See the samples in GitHub for examples.
i try to using fireStor in the NetCore 3 api with CQRS, and a erreur Appear when i use the hendler that contaie firebase service.
and locally all work well , the probleme appear in the server when i deploie the code.