c# - Authenticator. Class design -
have base class authenticatorbase has ability log in users different authentication subjects - login & password , token. class looks this:
public abstract class authenticatorbase { public abstract loginresult login(object authsubject); public abstract void logout(string sessionkey); }
the sub-class authenticates login & password
public class basicauthenticator : authenticatorbase { public override loginresult authenticate(object authsubject) { logindata credentials = authsubject logindata; if (dbcontext.security.authorize(credentials)) ............... } }
the sub-class authenticates token
public class tokenbasedauthenticator: authenticatorbase { public override loginresult authenticate(object authsubject) { string token = authsubject string; user user; if (dbcontext.security.loginwithtoken(token, out user)) ........... } }
what don't in code parameter authsubject of type system.object, need cast in sub-classes. if use, basicauthenticator, might pass invalid type parameter. know can make generic type authenticatorbase , have 2 sub-classes basicauthenticator: authenticatorbase<logindata
> , tokenbasedauthenticator : authenticatorbase<string
>. if need have them in list, both generic classes don't have common interface. create additional interface this:
public interface iauthenticator { loginresult login(object authsubject); } public abstract class authenticatorbase<tauthsubject>: iauthenticator { public abstract loginresult login(tauthsubject authsubject); // implementing interface loginresult iauthenticator.login(object authsubject) { return login((tauthsubject)authsubject); } }
now can store instances of iauthenticator in list, seems overwhelmed. what's best way rid of parameter authsubject type system.object??? want call , pass correct instance of type authenticators without having think correct types authenticator accepts.
Comments
Post a Comment