이것이 가능한지 잘 모르겠습니다. 각 구현에 확장 메서드를 작성해야 할 수도 있습니다. 다음은 몇 가지 예제 코드입니다.올바른 인터페이스 구현을 반환하는 확장 메서드?
public interface IBaseService<T>
{
IUnitOfwork UnitOfWork {get;}
}
public interface IService<T>: IBaseService<T>
{
IEnumerable<T> GetAll();
T GetById(Guid id);
}
public interface IUserService: IService<User>
{
User FindByUsernameAndPassword(string username, string password)
}
public class BaseService<T>: IService<T>
{
public BaseService(IRepository<T> repository)
{
_repository = repository
}
public virtual IEnumerable<T> GetAll(){....};
public virtual T GetById(Guid id){....};
IUnitOfWork UnitOfWork {get {return _repository.UnitOfWork;}}
}
public class UserService: BaseService<User>, IUserService
{
...
}
public static class ServiceExtensions
{
public static IBaseService<T> EnableLazyLoading<T>(this IBaseService<T> service, bool lazyLoad = True)
{
service.UnitOfWork.EnableLazyLoad(lazyLoad);
return service;
}
}
그래서 UserService를 사용하고 있다고 가정 해 보겠습니다.
userService.EnableLazyLoading(false).FindByUsernameAndPassword("ddivita","123456")
그래서 분명히하기 위해 그것이 무엇이든간에 새로운 버전을 만들길 원하십니까? –
올바른 구현을 반환하는 IBaseServcie의 모든 구현에서 사용할 하나의 확장 메서드 만 작성하고 싶습니다. 내 예제에서는 EnableLazyLoading 확장을 호출 할 때 IUserService를 반환하는 방법을 찾아야합니다. –
DDiVita
오! 알 수 있듯이, 호출 된 특정 유형을 반환하고 새 인스턴스를 생성하지 않기를 원합니다. –