C# Interfaces & Dependency Injection -


i trying understand little more interface injection , how hangs together.

my system has different types of users. each user see different set of menu on home page. rather having big switch statement checking type of user & load menu accordingly, created "user" base class , derived classes implement imenu interface. on page_load(), still need know type of user create before can call loadmenu() method. question is, how can away hard-coding instantiation of type of user object? or when retrieve data db , create type of user object, still need check type of user , use switch. there way away that?

below code

//base class public class user {     private string _username;     private string _name; }   public interface imenu {     void loadmenu(); }  public class manager : user, imenu {      public override void loadmenu(){          //loads manager's menu      } }  public class employee: user, imenu {      public override void loadmenu(){          //loads employee's menu      } }   protected void page_load() {      //retrieve user details database      //instantiate object of derived `user` type.      //call `loadmenu()` method. } 

if want use dependency injection load different types adhere common interface, should typed factories (at least that's how called in - same concept should found in other frameworks)

the principle following: have interface returns common interface you're interested in

public interface iuserfactory {     iuser getuser(string usertype); } 

then register types deriving common interface want resolve, discriminating information; can type of class, or other information.

finally inform factory link between types have been registered , discriminating information; example if had used class type name of component in windsor factory, tell factory request iuser should resolved using parameter pass component name. in windsor can done inheriting defaulttypedfactorycomponentselector

public class customtypedfactorycomponentselector : defaulttypedfactorycomponentselector {     protected override string getcomponentname(methodinfo method, object[] arguments)     {         if(method.name == "getuser" && arguments.length == 1 && arguments[0] string)         {             return (string)arguments[0];         }         return base.getcomponentname(method, arguments);     } } 

you

var dbuser = db.loaduser(1234); iuser user = iuserfactory.getuser(dbuser.type); user.loadmenu(); 

as matter of style, i'd recommend not giving user responsibility load menu; instead perhaps in better place if menu loaded passing user, or best interface describes authorized actions. way can load menu user, menu not limited , loaded machines, groups, etc... architecture country, won't stray further comments under question pointers :)


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -