Wanted to solicit whether composition over inheritance makes sense here, and if so a good way to go about it.
I have an interface like so:
public interface IUser
{
string FirstName {get; set}
string LastName {get; set}
}
There's an 'advanced' user interface as well, which has the same fields as IUser, but with some extras. PhoneNumber, EmailAddress, etc.
Should this IAdvancedUser interface inherit from IUser and add its own properties, or would containing an instance of IUser within it, and using setter injection to instantiate that member, make sense as well? If the latter, are there some .Net/C# idioms that work well under these circumstances-- for instance, exposing the IUser members as IAdvancedUser ones via wrapping or another approach?
These are DTO objects and won't have methods in them, only properties.
The answer is: Must an IAdvancedUser always be an IUser?
In that case: No, use inheritance.
However, "composition over inheritance" do not apply to interfaces since they doesn't follow the same rules as classes.
You could for instance do this:
interface IAdvancedFeatures
interface IUser
interface IAdvancedUser : IUser, IAdvancedFeatures
In that way you could enable anything to implement the advanced features while not forcing it to be a user, while in other cases everyone must be advanced users.
I usually try to treat interfaces as defining features instead of roles. So you could say that you should favor composition (like my IAdvancedUser), but not in the same sense as for classes (where you compose internally).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With