I'm looking to implement any good pattern(s) in a web application (asp.net 4.6.1 WebForms, Legacy, C#). I have large (code-behind) class with a huge Click event.I want to evaluate any Conditions and execute Commands (or Actions), and do error handling for communicate to UI.I cannot use full transaction (the commands-actions involves modify ActiveDirectory, WCF WebServices calls, send emails and database updates).Each command can be fail, error handling is required and notifications to UI. This is a bit of a hack.I get the impression much of the logic can and should be in a business logic class (or multiple), with communication between UI being limited to a parameter class being used to pass parameters from the UI to the back-end, while another class returns the results (success/error) and various messages.Maybe, it would be another good pattern better for handling the flow using multiple Conditions and Commands, and error handling to communicate to UI.code (fragment) for the Click Event (without error handling)  if (ActionTypePage == ActionType.Modificacion || ActionTypePage == ActionType.MyDataM)  {            var password = DataPersonales.GenerarPassword();            var personalData= fillDataPersonales(password);            var userPortal = fillUserPortal();            var isEmployer = userPortal.IsEmployerRoleAorRoleB();            var existsInAD = ADOperations.ExistsUserInActiveDirectory(personalData.User);            if (userPortal.IsEnabled.Value && isEmployer)            {                // 1a) If RoleA, RoleB (employer) NOT exists in AD ==> Error                // 1b) If RoleA, RoleB (employer) exists in AD ==> Enable user in CRM (if false, Request New CRM)                if (!existsInAD)                {                    var msg = String.Format(The employer {0:-10} no exists in ActiveDirectory, personalData.User);                    var log = String.Format(User: {0, -15} - Error: {1:-100}, personalData.User, msg);                    LoggerAD.Trace([AdminDataUsers] - Update ERROR. ExistsUserInActiveDirectory.  + log);                    ((NsiMaster)Master.Master.Master).ShowNotification(msg, CommunicationType.Error, true);                    return;                }                // Enable in CRM (if false, Request New CRM)                var okEnable = AdminManager.EnableUserEnCrm(personalData.User);                if (!okEnable)                {                    var msg = Cannot enable user in CRM. Please, do new Request CRM;                    ((NsiMaster)Master.Master.Master).ShowNotification(msg, CommunicationType.Error, true);                    return;                }            }            if (userPortal.IsEnabled.Value && !isEmployer)            {                // 2a) If NOT RoleA, RoleB (not employeer) NOT exists in AD ==> Create in AD and Enable user in CRM (if false, Request New CRM)                if (!existsInAD)                {                    var okToCreateInAD = (UserLogado.IsUserWithRoleMA() || UserLogado.IsEmployerRoleAorRoleB())                        && !userPortal.IsEmployerRoleAorRoleB();                    var okAD = false;                    if (okToCreateInAD)                    {                        var msgStateAD= ;                        okAD = ADOperations.AddUserInActiveDirectory(personalData.User, personalData.Password,                            personalData.Name, personalData.Surname +   + personalData.Surname2                            , personalData.FullName                            , isEmployer, UserConsultado.UserSystemAire, UserConsultado.CustomValue                            , personalData.Email, personalData.Telephone, personalData.Mobile                            , out msgEstadoAD);                        if (!okAD)                        {                            var log = String.Format(User: {0, -15} - Error: {1:-100}, personalData.User, msgEstadoAD);                            LoggerAD.Trace([AdminDataUsers] - Update ERROR. AddUserInActiveDirectory.  + log);                        }                        if (okAD)                        {                            var log = String.Format(User: {0, -15} - Password: {1:-10}, personalData.User, PassManager.ShowPassword(personalData.Password));                            LoggerAD.Trace([AdminDataUsers] -  Update. AddUserInActiveDirectory.  + log);                        }                    }                    // Enable in CRM (if false, Request New CRM)                    var okEnable = AdminManager.EnableUserEnCrm(personalData.User);                    if (!okEnable)                    {                         var msg = Cannot enable user in CRM. Please, do new Request CRM;                        ((NsiMaster)Master.Master.Master).ShowNotification(msg, CommunicationType.Error, true);                        return;                    }                }                // 2b) If NOT RoleA, RoleB (not employeer) exists in AD ==> Send Warning (Email) and Enable in CRM (if false, Request New CRM)                if (existsInAD)                {                    //  Warning (Email)                    AdminManager.SendEmailWarning(personalData.User);                    // Enable in CRM (if false, Request New CRM)                    var okEnable = AdminManager.EnableUserInCrm(personalData.User);                    if (!okEnable)                    {                        var msg = Cannot enable user in CRM. Please, do new Request CRM;                        ((NsiMaster)Master.Master.Master).ShowNotification(msg, CommunicationType.Error, true);                        return;                    }                }            }            // Disable            // 1a) If RoleA, RoleB (employer) NOT exists in AD ==> Error            // 1b) If RoleA, RoleB (employer) exists in AD ==> not Enable in CRM (Delete en CRM) and Delete License CRM             if (!userPortal.IsEnabled.Value && isEmployer)            {                if (!existsInAD)                {                    var msg = String.Format(The employer {0:-10} not exists in ActiveDirectory, personalData.User);                    var log = String.Format(User: {0, -15} - Error: {1:-100}, personalData.User, msg);                    LoggerAD.Trace([AdminDataUsers] - Update ERROR. ExistsUserInActiveDirectory.  + log);                    ((NsiMaster)Master.Master.Master).ShowNotification(msg, CommunicationType.Error, true);                    return;                }                // Not Enable en CRM (Delete in CRM)                AdminManager.DisablesUserInCrm(personalData.User);                // Delete License CRM                 AdminManager.DeleteUserCrmActive(personalData.User);            }            if (!userPortal.IsEnabled.Value && !isEmployer)            {                // 2a) If NOT RoleA, RoleB (not employeer) NOT exists in AD ==> Send Warning (Email) and Not Enable in CRM (Delete in CRM) - Delete License CRM                 if (!existsInAD)                {                    // Send Warning (Email)                    AdminManager.SendEmailWarning(personalData.User);                   // Not Enable en CRM (Delete in CRM)                   AdminManager.DisablesUserInCrm(personalData.User);                   // Delete License CRM                    AdminManager.DeleteUserCrmActive(personalData.User);                }                // 2b) If NOT RoleA, RoleB (not employeer) exists in AD ==> Delete in AD and Not Enable in CRM (Delete in CRM) - Delete License CRM                 if (!existsInAD)                {                    // Delete in AD                    var res = ADOperations.DeleteUserInActiveDirectory(personalData.User);                    // Not Enable en CRM (Delete in CRM)                    AdminManager.DisablesUserInCrm(personalData.User);                    // Delete License CRM                     AdminManager.DeleteUserCrmActive(personalData.User);                }    }Any suggestions for refactoring, not code-smell, maybe with error handling too?
Design Pattern for Multiple Conditions and Actions in Click Event WebForms
c#;design patterns;asp.net