具有泛型类型的接口实现

时间:2021-07-10 14:26:18

标签: c# interface

我正在尝试将这个 IRepository 接口用于所有类。为了添加其他方法,我添加了 IRoleRepository。 但是我没有在我的班级中实现 IRoleRepository 接口。 如何在类中实现接口?enter image description here

public interface IRepository<T> where T : class
{
    public T Post(T t);
    public T Get();
    public T Get(T t);
    public T Put(T t);
    public T Delete(string id);
    public T Delete();
}
public interface IRoleRepository<T>:IRepository<T> where T:class
{
    public  T RoleExistsAsync(T roleName);

    public  T CreateRole(T user, T identityRole);

    public T GetRoles();

    public  T GetRoleById(T id);

    public  T UpdateRole(T identityRole);

    public  T DeleteRoleById(T identityRole);

    public  T GetClaimsByRole(T identityRole);

    public  T AddClaimToRole(T claim, T role);

    public  T RemoveClaimFromRole(T claim, T identityRole);
}



public class RoleManager : IRoleRepository<T> where T:class
{
    private readonly RoleManager<IdentityRole> _roleManager;
    public RoleManager( RoleManager<IdentityRole> roleManager)
    {
        this._roleManager = roleManager;
    }
            public async Task<bool> RoleExistsAsync(string roleName)
    {
        return await _roleManager.RoleExistsAsync(roleName);
    }
    public async Task<IdentityResult> CreateRole(ApplicationUser user, IdentityRole identityRole)
    {
        return  await _roleManager.CreateAsync(identityRole);
    }
    public IEnumerable<IdentityRole> GetRoles()
    {
        return _roleManager.Roles;
    }
    public async Task<IdentityRole> GetRoleById(string id)
    {
        return await _roleManager.FindByIdAsync(id);
    }

}

1 个答案:

答案 0 :(得分:1)

IRepository<T>, T 在 RoleManager 中只有一种类型,

第一种方式,更改IRoleRepository

    public interface IRoleRepository<TResult, TUser, TRole> : IRepository<TResult>
        where TResult : class where TUser : class where TRole : class
    {
        public Task<bool> RoleExistsAsync(string roleName);

        public Task<TResult> CreateRole(TUser user, TRole identityRole);

        public IEnumerable<TRole> GetRoles();

        public Task<TRole> GetRoleById(string id);
    }
    }

RoleManger

public class RoleManager : IRoleRepository<IdentityResult, ApplicationUser, IdentityRole>
    {
        private readonly RoleManager _roleManager;
        public RoleManager(RoleManager roleManager)
        {
            this._roleManager = roleManager;
        }
        public async Task<bool> RoleExistsAsync(string roleName)
        {
            return await _roleManager.RoleExistsAsync(roleName);
        }
        public async Task<IdentityResult> CreateRole(ApplicationUser user, IdentityRole identityRole)
        {
            throw new NotImplementedException();
        }
        public IEnumerable<IdentityRole> GetRoles()
        {
            return _roleManager.Roles;
        }
        public async Task<IdentityRole> GetRoleById(string id)
        {
            return await _roleManager.FindByIdAsync(id);
        }

    }

第二种方式,在方法中添加泛型类型

 public Task<TResult> CreateRole<TUser, TRole>(TUser user, TRole identityRole);