I am not sure if this is possible and I may need to write extension methods for each implementation. Here is some example code:
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;
}
}
So, let’s say I am using the UserService. When I call the extension method on the UserService, is it possible to have it return the proper implementation of IBaseService or do I need to create and Extension Method for each implementation?:
Example:
userService.EnableLazyLoading(false).FindByUsernameAndPassword("ddivita","123456")
You can do it this way: