-
For simple, I provide a minimal example that disappear some code about async or external implementation public static class RepositoryExtensions
{
public static TDto Get<TEntity, TKey, TDto>(this IRepository<TEntity, TKey> repository, TKey id)
{
return Map<TEntity, TDto>(repository.Get(id));
}
} Then, I can call it: IRepository<User, Guid> repository = ...;
var dto = repository.Get<User, Guid, UserDto>(id); But it's not what I want. I want something like It may related #2821 What do you think? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
The I don't think we should make the duplicate code such like this. Especially I can't use |
Beta Was this translation helpful? Give feedback.
-
This is a duplicate of the championed proposal #1349. |
Beta Was this translation helpful? Give feedback.
-
I know you're requesting this for the broader case, but for your specific example if you're using c# 8 you could always pull DIMs into the mix: public interface IRepository<TEntity, TKey>
{
TEntity Get(TKey id);
TDto Get<TDto>(TKey id) {
return Map<TEntity, TDto>(Get(Id));
}
} Depending on what/where Map is (ie, if it's being called on something external to the instance/interface) you can combine the two approaches by delegating the DIM with one generic to the extension with three. You'd have to specify all the generics for the invocation, but you'd only need to do it in a single place. The user would call the dim directly. The downside to this approach is it only works if you have a variable of the interface type (and not one of the concrete repository) and it adds a method to the interface surface. The plus side being that the Dim could be overridden ie. for custom mapping logic on a specific repository |
Beta Was this translation helpful? Give feedback.
This is a duplicate of the championed proposal #1349.