Reputation: 6021
I have generic repository implemented. Problem is my virtual types in the main returned type are null. How to include them in async call? Here is my type:
public class Translation : Entity
{
public string Text { get; private set; }
public Guid TranslationKeyID { get; set; }
[ForeignKey("TranslationKeyID")]
public virtual TranslationKey TranslationKey { get; set; }
}
Now Here is the method where I want that TranslationKey
Entity too in my results but I only get TranslationKeyId
.
Method of my service :
public async Task<List<TranslationDto>> ListTanslationsAsync()
{
var translations = await _translationRepository.GetAllAsync().
if (translations != null)
{
return translations.ProjectedAs<List<TranslationDto>>();
}
return null;
}
What to write instead of GetAllAsync()??
Upvotes: 0
Views: 709
Reputation: 62268
What is TranslationDto and how did you implement the repository pattern around EF? In short you might have lazy loading off or you did not call Include but here is more on how you could make your implementation better.
I also have implemented a Repository pattern around EF but have created a generic repository where the methods are generic and not the type. The type itself has an instance of an DbContext and the methods make the corresponding calls on Set so there are no hard coded DbSet instances in the DbContext. Here is an example of what you could use, then you can wire it together using Autofac or some other IoC framework.
public interface IMyGenericRepository
{
TEntity Find<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class;
IQueryable<TEntity> Filter<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class;
}
public sealed class MyGenericRepository : IMyGenericRepository
{
private DbContext _dbContext;
public virtual TEntity Find<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
{
return _dbContext.Set<TEntity>().FirstOrDefault(predicate);
}
public virtual IQueryable<TEntity> Filter<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
{
return DbSet<TEntity>().Where(predicate).AsQueryable<TEntity>();
}
}
You could adapt this to make it return an instance of List and leave out the Dto all together by implementing your access logic in the generic repository. Also you can implement the async/await keywords when necessary or by default.
Now for part 2, getting objects in your object graph. This can be achieved by using the Include extension to ensure that EF populates part of your graph when it retrieves your objects. This can be done by the caller or you can implement a subclass (inherit) from your generic repository to make the call to Include. The code above could be called like this:
var result = await _genericRepository.Filter<Translation>(add lambda here).Include(x => TranslationKey).ToListAsync();
In short there are lots of possibilities using this pattern, its just a matter of figuring out what you need and where to put it.
Upvotes: 1