2017-08-09 50 views
2

我有一個通用的接口:註冊一個通用接口與ASP.NET核心依賴注入非通用實現

public interface IValidationResult<T> where T : struct { 
     T Status { set; get; } 
     User User { set; get; } 
} 

和實現通用接口的類:

public class ValidationResult : IValidationResult<ValidationResult.ValidationStatus> { 
    public enum ValidationStatus { UserNotFound, InvalidPassword, Valid, UserBlankOrSpace } 
    public ValidationStatus Status { set; get; } 
    public User User { set; get; } 

    public ValidationResult (User user, ValidationStatus status) { 
     if (user == null) { 
      throw new ArgumentNullException ("The User is Null"); 
     } 
     Status = status; 
     User = user; 
    } 
} 

我想註冊我的接口,以便我可以使用DI(依賴注入),如果我的類和接口都是通用的,我將它們註冊爲:

services.AddScoped(typeof(IGenericRepository<>), typeof(EFGenericRepository<>)); 

但是可以使用內置的依賴注入在ASP.NET Core中註冊一個通用接口和非通用實現?由於

回答

3

怎麼樣

services.AddScoped<IValidationResult<ValidationResult.ValidationStatus>, 
        ValidationResult>(); 
+0

非常感謝你,它的工作 –