我对 DDD 结构有疑问,我想我不太理解该模式。
简短。 我有三个 AggrgateRoot(AppOrders - AppTests - AppAntibiotics)。 AppOrders 具有嵌套列表 AppOrderItems 链接(一对一)与 AppMicrobiologicalReportResults(每种类型的 Ef 表)每个项目都有三个列表(敏感 - 中度 - 耐药) AppMicrobiologicalReportResults 和 AppAntibiotics 之间的 AppAntibiotics (多对多关系)这里是问题
问题。 如何通过 Ef Core 查询的最佳实践提取此输出而不与 DDD 模式冲突
{
"sample": "string",
"pusCells": "string",
"gramStainedFilms": "string",
"znStainedFilms": "string",
"aerobic": "string",
"viableCount": "Mild growth",
"mycobacterialCulture": "string",
"mycologicalCulture": "string",
"sensitiveItems": [
{
"antibioticId": 2,
"antibioticName": "Amoxicillin" //<== here is my problem
},
{
"antibioticId": 1,
"antibioticName": "Penicillins" //<== here is my problem
}
],
"intermediateItems": [],
"resistantItems": [],
"testId": "1dfa0d50-4ed8-fb4d-5ed3-3a0f8876cf1e",
"testName": "Microbiological Report",
"status": 0,
"resultDate": null,
"resultPrinted": false,
"price": 0,
"id": "20557835-bcdd-1cb4-ef22-3a0f887707ba"
}
一些笔记。 1-我使用Abp框架,Netcore 7,SqlLite 2- 这是所有者的操作方法
public async Task<MicrobiologicalReportResultDto> GetMicrobiologicalReport(Guid id,Guid itemId)
{
var itemsQuery = await _itemsRepository.WithDetailsAsync();
itemsQuery = itemsQuery.Where(x => x.OrderId == id && x.Id == itemId);
var testQuery = await _testRepository.GetQueryableAsync();
// This is Antibiotic Query
var antibioticsQuery = await _antibioticsRepository.GetQueryableAsync();
var query =
from item in itemsQuery
join test in testQuery
on item.TestId equals test.Id
select new { item, test};
var entity = await AsyncExecuter.FirstOrDefaultAsync(query);
if (entity is null)
{
throw new EntityNotFoundException();
}
if (entity.item is not MicrobiologicalReportResult)
{
throw new BusinessException("Error Types");
}
var rc = ObjectMapper.Map<MicrobiologicalReportResult,MicrobiologicalReportResultDto>(
entity.item.As<MicrobiologicalReportResult>());
rc.TestName = entity.test.Name;
return rc;
}
3-这是 ef Fluent api 配置
builder
.HasMany(x => x.Sensitive)
.WithMany()
.UsingEntity<MicrobiologicalAntibiotic>("MicrobiologicalReportSensitiveAntibiotics",
l=>l.HasOne<Antibiotic>()
.WithMany()
.HasForeignKey(c=>c.AntibioticId),
r=>r.HasOne<MicrobiologicalReportResult>()
.WithMany(x=>x.SensitiveItems)
.HasForeignKey(c=>c.OrderItemId));
// Others Removed for lack of complexity
4-这是我写来测试的Sql
select * from AppOrderItems itemsQuery
left outer join AppTests testsQuery
on testsQuery.Id = itemsQuery.TestId
left outer join MicrobiologicalReportSensitiveAntibiotics sens
on sens.OrderItemId = itemsQuery.Id
left outer join AppAntibiotics antbiotic
on sens.AntibioticId = antbiotic.Id
如果有不清楚的地方,我深表歉意。
非常感谢你。
我尝试了 linq group into 但它很困惑,因为它需要子列表 Queryables
如果我理解正确,我认为你的问题中有两件事需要考虑: