-
-
Save hanguksaram/69695558c6a7df35e1190c729dc94bfb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Interface: | |
public interface IProjectService<T, K>: IService<T, K> where T: Project where K: ProjectRes{} | |
//Injected implementation: | |
public class ProjectService<T, K> : Service<T, K>, IProjectService<T, K> where T : Project where K : ProjectRes, new() | |
{ | |
private readonly IRepo<T, K> repo; | |
private readonly IUnitOfWork uOfWork; | |
private readonly IMapper mapper; | |
public ProjectService(IProjectRepo<T, K> repo, IUnitOfWork uOfWork, IMapper mapper) : base(repo, uOfWork, mapper) | |
{ | |
this.repo = repo; | |
this.uOfWork = uOfWork; | |
this.mapper = mapper; | |
} | |
public async override Task<IQueryable<K>> GetAll(){ | |
var projects = this.repo.GetAll(); | |
return projects.MapToProjectResource() as IQueryable<K>; | |
//var projecResourse = | |
} | |
} | |
//DI container in StartUp.cs: | |
//compiler allow me implement only such explicit declaration with real classes types in generic params | |
services.AddScoped(typeof(IProjectService<Project, ProjectRes>), typeof(ProjectService<Project, ProjectRes>)); | |
// actually i curios if there is better solution than this one. |
I'm facing a similar issue where what I'd like to do would be something like this
services.AddScoped(typeof(IService<>), typeof(SpecificImplementationService<,TSpecificImplementation>));
So switching from open generic to semi open generic
Any idea how to do that ?
@Poltuu try creating a wrapper class for DI class OnlyForDI<T> : SpecificImplementationService<T,TSpecificImplementation>)
and then register services.AddScoped(typeof(IService<>), typeof(OnlyForDI<>));
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can confirm this works.