Entity Framework - 延迟加载或额外的 async/await 查询方法?
Entity Framework - lazy loading or additional async/await query method?
我有这些领域模型
public class Topic
{
public int TopicId { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public int? TopicId { get; set; }
public virtual Topic Topic { get; set; }
}
例如,我想实现方法 TestAsync,在那里我想使用 Topic 对象和相关的 Posts 对象。
我可以使用异步方法和 topicId 作为参数获得主题模型。
public async Task<bool> TestAsync(int topicId)
{
var topic = await topicService.GetByIdAsync(topicId);
// posts ...
}
而我有两种方法,如何获取相关帖子。
但是,如果我将使用 LazyLoading 或只是另一个异步查询有什么区别?
// Example: 1 (LazyLoading)
var posts = topic.Posts;
// OR Example: 2 (Async method)
var posts = await postService.GetAllByTopicIdAsync(topicId);
所以,我认为 Example:1 将同步工作,而且我失去了 async/await 代码的所有优点。
但是示例:2 让我觉得,这可能是我不知道延迟加载的所有魅力:)
谁能阐明我应该使用什么解决方案以及为什么?谢谢:)
两者都不应该。我假设您的 GetByIdAsync() 是这样实现的。
public async Task<Topic> GetByIdAsync(int id)
{
return await context.Topics.FirstAsync(t=>t.Id == id);
}
你应该把它改成
public async Task<Topic> GetByIdAsync(int id)
{
return await context.Topics.Include(t=>t.Posts).FirstAsync(t=>t.Id == id);
}
延迟加载总是同步的,这是不幸的。例如,EF Core 具有异步优先的思想,(目前)还不支持延迟加载。
其他选项是按照 Peter 的建议进行连接(预先加载),异步执行单个查询;或者进行显式的第二个异步查询。您选择哪一个取决于您的模型通常如何使用。
就我个人而言,如果模型总是一起使用,我会选择进行预加载,否则进行多个异步查询。我自己不使用延迟加载,尽管没有什么可以阻止它工作。
public async Task<TProperty> GetReferenceAsync<TEntity, TProperty>(DBContext context, TEntity entity, Expression<Func<TEntity, TProperty>> property)
where TEntity : class, IIdentifiable where TProperty : class
{
var getProperty = property.Compile();
var value = getProperty(entity);
if (value != null) { return value; }
await context.Entry(entity).Reference(property).LoadAsync();
return getProperty(entity);
}
用法:var posts = await GetReferenceAsync(context, topic, e => e.Posts);
我有这些领域模型
public class Topic
{
public int TopicId { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public int? TopicId { get; set; }
public virtual Topic Topic { get; set; }
}
例如,我想实现方法 TestAsync,在那里我想使用 Topic 对象和相关的 Posts 对象。
我可以使用异步方法和 topicId 作为参数获得主题模型。
public async Task<bool> TestAsync(int topicId)
{
var topic = await topicService.GetByIdAsync(topicId);
// posts ...
}
而我有两种方法,如何获取相关帖子。 但是,如果我将使用 LazyLoading 或只是另一个异步查询有什么区别?
// Example: 1 (LazyLoading)
var posts = topic.Posts;
// OR Example: 2 (Async method)
var posts = await postService.GetAllByTopicIdAsync(topicId);
所以,我认为 Example:1 将同步工作,而且我失去了 async/await 代码的所有优点。 但是示例:2 让我觉得,这可能是我不知道延迟加载的所有魅力:) 谁能阐明我应该使用什么解决方案以及为什么?谢谢:)
两者都不应该。我假设您的 GetByIdAsync() 是这样实现的。
public async Task<Topic> GetByIdAsync(int id)
{
return await context.Topics.FirstAsync(t=>t.Id == id);
}
你应该把它改成
public async Task<Topic> GetByIdAsync(int id)
{
return await context.Topics.Include(t=>t.Posts).FirstAsync(t=>t.Id == id);
}
延迟加载总是同步的,这是不幸的。例如,EF Core 具有异步优先的思想,(目前)还不支持延迟加载。
其他选项是按照 Peter 的建议进行连接(预先加载),异步执行单个查询;或者进行显式的第二个异步查询。您选择哪一个取决于您的模型通常如何使用。
就我个人而言,如果模型总是一起使用,我会选择进行预加载,否则进行多个异步查询。我自己不使用延迟加载,尽管没有什么可以阻止它工作。
public async Task<TProperty> GetReferenceAsync<TEntity, TProperty>(DBContext context, TEntity entity, Expression<Func<TEntity, TProperty>> property)
where TEntity : class, IIdentifiable where TProperty : class
{
var getProperty = property.Compile();
var value = getProperty(entity);
if (value != null) { return value; }
await context.Entry(entity).Reference(property).LoadAsync();
return getProperty(entity);
}
用法:var posts = await GetReferenceAsync(context, topic, e => e.Posts);