imtiaj ahammad
imtiaj ahammad

Reputation: 31

Interface Implementation with generic type

I am trying to use this IRepository interface for all the classes. And to add other methods I have added IRoleRepository. But I am failing to implement the IRoleRepository interface in my class. How to implement the interface in class?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);
    }

}

Upvotes: 0

Views: 73

Answers (1)

wenming
wenming

Reputation: 71

IRepository<T>, T in RoleManager only one type,

first way, change 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);
        }

    }

second way, add generic type in method like

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

Upvotes: 1

Related Questions