We are creating a Search Function to return Matching Address Records in large database.
Address Table contains 20+ columns in SQL table, needs to query using different OData parameters, (EqualTo, Contains, Starts With, etc).
Trying to use OData, without injecting DBContext into API Controller. This is not allowed in current architecture. How can code written to allow this?
*We have it working utilizing ProjectTo. However OData Http specific features are being passed into Non-Api levels. Currently have ODataQueryOptions in the AppServices.
How Can We convert the Whole ODataQueryOptions to a Linq expression query (regardless of what is passed in), this resource is only for the filter, How to transform OData filter to a LINQ expression?
DBContext-->DomainLayer-->Dto--> Then API Level,
The two middle layers. Mappers map from EF DBContext to Domain to Dto.
Repository methods bring data.
AppService Methods converts to Dto.
Current Solution with ProjectTo:
Controller API:
[HttpGet]
public async Task<ActionResult<IList<AddressTypeDto>> GetAddressAPI(ODataQueryOptions<AddressTypeDto> queryOptions)
{
return Ok(await _service.GetAddressTypeDto(queryOptions));
}
Application Service:
(How can we apply ODataModelBuilder mapping in Startup here?)
public async Task<AddressTypeResponse> GetAddressTypeDto(ODataQueryOptions<AddressTypeDto> queryOptions)
{
var dto = (IQueryable<AddressTypeDto>)queryOptions.ApplyTo(_addressRepository.GetAddressTypes().ProjectTo<AddressTypeDto>(_mapper.ConfigurationProvider));
var dtoList = await dto.ToListAsync();
return dto;
}
Repository:
[EnableQuery]
public IQueryable<LkAddressType> GetAddressTypesData()
{
return _ctx.LkAddressType.AsNoTracking();
}
Classes:
public class AddressTypeDto
{
public int AddressTypeId { get; set; }
public int? LastModifiedByUserId { get; set; }
public string AddressTypeCode { get; set; }
public string AddressTypeDescription { get; set; }
public bool? Status { get; set; }
public DateTime? CreateDate { get; set; }
}
public LkAddressType()
{
public int LkAddressTypeId { get; set; }
public int? LastModifiedByUserId { get; set; }
public string AddressTypeCode { get; set; }
public string AddressTypeDescription { get; set; }
public bool? Status { get; set; }
public DateTime? CreateDate { get; set; }
public DateTime? EffectiveStartDate { get; set; }
public DateTime? EffectiveEndDate { get; set; }
}
Using C# Net Core 2.2, Entity Framework, Sql Server Database
Resource: Attempting to utilize this also
How do I map an OData query against a DTO to another entity?