Search code examples
c#asp.net-web-apiiprincipaliidentity

Should User class implement IPrincipal and IIdentity


I'm building my own token based authentication for asp.net web api 2 and I have a question about the user class.

Currently my user class implements IPrincipal and IIdentity, but I don't know what is the best practice. Would you divide IPrincipal and IIdentity and make the IIdentity implementation a member of IPrincipal?

Currently I use the following class declaration

class User : IPrincipal, IIdentity

Thank you!


Solution

  • Would you divide IPrincipal and IIdentity and make the IIdentity implementation a member of IPrincipal?

    I don't think this is a good approach, since if the Identity was similar to IPrincipal there shouldn't be any reason for being separated.

    Furthermore, as of now your class implements both interfaces. That means your class implements the methods of both interfaces. If you decide later for any reason that your class shouldn't implement one or another interface, you should just have to remove the implementation of the methods that are tied to the interface you want to remove.

    In addition to the above keep in mind the following:

    The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use

    This is one of the five SOLID principles, which are some basic principles of object oriented programming and design.