I have the following interfaces defined in a base library project:
public interface IWorkContext {
T User<T>() where T : IUser;
// Note this method is kind of a shortcut so i don't
// have to keep passing in T
IUser User();
}
public interface IUser {
int Id { get; }
string UserName { get; }
}
Now I have another project which references the one above with the following class (which implements IUser).
public class User : IUser {
public virtual int Id { get; set; }
public virtual string UserName { get; set; }
// ... Code removed for brevity
}
Now i also need to implement IWorkContext. Here’s my first attempt:
public class DefaultWorkContext : IWorkContext {
private readonly IUsersService _usersService;
public DefaultWorkContext(IUsersService usersService) {
_usersService = usersService;
}
public T User<T>() where T : IUser {
return _usersService.GetUser<T>(1));
}
public User User() {
return User<User>();
}
}
However this gives the error:
The type ‘T’ cannot be used as type parameter ‘T’ in the generic type
or method ‘…GetUser(int)’. There is no boxing conversion or type
parameter conversion from ‘T’ to ‘User’.
I’m sure there’s something fundamental i’m doing wrong. I’d appreciate any advice on this model can be improved. Thanks
Edit (as requested here’s the GetUser method):
public T GetUser<T>(int id) where T : User {
return _session.Get<T>(id);
}
Note: _session is an NHibernate session.
The problem here is that you are passing a generic type as a type parameter to your
GetUser()method, that may not satisfy the condition of inheritance of the generic type accepted by yourGetUser()method.The compiler ensures that the type passing as parameters always satisfy the type conditions.
Indeed, you could write a
class NotUserthat implementsIUserwhile not being aUser.Your
User<T>()method could then be called withNotUseras the type parameter, and that would result in a call toGetUser()with a type parameter that does not inherit fromUser.Your
GetUser()method only accept a type that inherits fromUser.You need to either change
GetUser()prototype to accept a generic type that implementsIUser, or theUser<T>prototype to accept only generic types that inherits fromUser.Either this:
or this:
EDIT:
Whether you should pick the first or the second method depends on what you want to do.
1) You want your
IWorkContextto work with theIUserinterface, and you accept that theGetUser()method returns aIUser=> pick option 12) You want the
GetUser()method to take a type that inherits fromUser, and you accept that theIWorkContextinterface works withUserinstead ofIUser=> pick option 2, and modify theIWorkContextinterface.To me, option 1) is better, as you don’t need to modify the interface and you don’t break the genericity. But it depends on what you want to do exactly.