c# - An static Controller Handler to manage the methods that are used in controllers...¿good? -


i have created static class methods used in controller actions, idea not put logic inside controller actions example of class is:

namespace mytestproject.infrastructure.handler { public static class controllerhandler {     public static string getusername(getloggeduserresponse user)     {         string username = string.empty;          if (user != null)         {             username = user.nombre;         }          return username;     }     private static list<menu> getvisiblechildmenus(getloggeduserresponse user, int idmenu, bool tab)     {         return user.menusperfiles.where(mp => mp.menu.idpadre == idmenu                                               && mp.menu.visible.value                                               && mp.menu.tab.value == tab)                                                      .select(mp => mp.menu)                                                      .orderby(m => m.orden)                                                      .tolist();     }      public static list<navbar> getnavbar(getloggeduserresponse user, pathstring requestpath)     {         list<navbar> navbar = new list<navbar>();          if (user != null)         {             //menu correspondiente la pantalla actual             menu menu = user.menusperfiles.where(mp => mp.menu.ruta.startswith("/") && requestpath.startswithsegments(mp.menu.ruta))                                           .select(mp => mp.menu)                                           .firstordefault();              if (menu != null)             {                 //hijos en modo tab                 list<menu> menus = getvisiblechildmenus(user, menu.id.value, true);                  menus.foreach(m =>                 {                     //hijos del menu tab (se mostraran en forma de lista desplegable)                     list<menu> menustab = getvisiblechildmenus(user, m.id.value, false);                     var navbarmenus = new list<navbarmenu>();                      menustab.foreach(mt =>                     {                         navbarmenus.add(new navbarmenu                         {                             link = mt.ruta,                             title = mt.nombre                         });                     });                      navbar.add(new navbar                     {                         title = m.nombre,                         icon_css = m.cssicon,                         class_css = menustab == null || menustab.count() > 0 ? "" : "dropdown",                         link = m.ruta,                                                    menus = navbarmenus                     });                 });             }         }          return navbar;     }      public static sidebar getsidebar(getloggeduserresponse user)     {         sidebar sidebar = null;          if (user != null)         {             sidebar = new sidebar();              list<menu> menus = user.menusperfiles.where(mp => mp.menu.visible.value && !mp.menu.idpadre.hasvalue).select(mp => mp.menu).orderby(m => m.orden).tolist();              menus.foreach(m =>             {                 var sidebarmenus = new menus();                  //hijos en modo tab                 list<menu> childmenus = getvisiblechildmenus(user, m.id.value, false);                  childmenus.foreach(cm =>                 {                     sidebarmenus.add(new sidebarmenu {title = cm.nombre, url = cm.ruta, position = cm.orden.value });                 });                  sidebar.add(new sidebarbutton                 {                     title = m.nombre,                     icon_css = m.cssicon,                     position = m.orden.value,                     id = m.id.value,                     visible = true,                     menus = sidebarmenus                 });             });                         }         else         {             sidebar = new sidebar {                new sidebarbutton {                    title = "dashboard", icon_css = "s7-monitor", position = 1, id = 1, visible = true,                    menus = new menus(){                         new sidebarmenu { title = "main dashboard", url = "/", position = 1 }}}                            };         }          return sidebar;     }      public static reportviewmodel getreport(string reportid, string accesstoken)     {         getreportresponse report = apihandler.getreport(reportid, accesstoken);          var reportsviewmodel = new reportviewmodel() { reports = new list<report>() };          report.reports.foreach(r =>         {             reportsviewmodel.reports.add(new report { id = r.reportid,                                                       embedurl = r.embedurl,                                                       accesstoken = r.accesstoken,                                                       visiblescreensize = r.visiblescreensize,                                                       hiddenscreensize = r.hiddenscreensize });         });          return reportsviewmodel;     }      public static async task<getnotificationresponse> getnotifications(string all, string accesstoken)     {         getnotificationresponse result = await apihandler.getnotifications(all, accesstoken);          return result;     } 

...

but seems me bad idea because class have more 1 responsability correct? it's better split class? in case don't in controller actions have deal instance lot of classes , idea have "single point" or helps controller access methods yout think? there design pattern this?.

also kind of methods (get user name, build menus, user notifications) methods put under infrastructure namespace?

you using static class more library common methods helpful controller. it's not uncommon developers create such static libraries of commonly used methods , library contain methods might have little in common.

however in case since methods intended used controller instead created class, perhaps called base controller inherits controller , implements these methods (not statics). can have controllers in website inherit basecontroller. make these convenience methods available controller without having reference static class. approach used , better choice in particular case.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -