简体   繁体   中英

Passing parameters into an Action?

I am trying to fully understand how I can simplify the following:

    public ActionResult Create(string ds) {
            InitializeServices(ds, "0000");
            vm.Account = new Account {
                    PartitionKey = "0000",
                    RowKey = "0000",
                    Created = DateTime.Now,
                    CreatedBy = User.Identity.Name
            };
        }
        catch (ServiceException ex) {
            ModelState.Merge(ex.Errors);
        }
        catch (Exception e) {
            Trace.Write(e);
            ModelState.AddModelError("", "Database access error: " + e.Message);
        }
        return View("CreateEdit", vm);
    }

I had a few great answers and the following was suggested:

    private void HandleException(Action action) {
        try { 
            action(); 
        } 
        catch (ServiceException ex) { 
            ModelState.Merge(ex.Errors); 
        } 
        catch (Exception e) 
        { 
            Trace.Write(e); 
            ModelState.AddModelError("", "Database access error: " + e.Message); 
        } 
    } 

    RunAndHandleExceptions(new Action(() =>                 
   {                      
        //Do some computing                 }
    )); 

This looks like a really great solution but I still don't understand how I can pass in my parameters into the action. What I need to do is to pass in the following:

     string ds
     System.Web.Mvc.ModelState ModelState  (passed as a reference)

Just

 HandleException(() => someFunction(ds, ModeState));

should do it

To get the return value, you need a Func<> , not Action<> :

private TR HandleException<TR>(Func<TR> action)
{
    try
    {
        return action();
    }
    catch (ServiceException ex)
    {
        ModelState.Merge(ex.Errors);
    }
    catch (Exception e)
    {
        Trace.Write(e);
        ModelState.AddModelError("", "Database access error: " + e.Message);
    }

    return default(TR); // null for reference types
}

You then would use it, eg without an existing function:

bool result = HandleException(() =>
    {
         if (string.IsNullOrEmpty(ds))
             return false;

         // do interesting stuff that throws many kinds of exceptions :)
         // Note: freely use ds and ModelState from surrounding scope, 
         // no need to 'pass them'

         return true;
    });

You can define an action with up to 16 parameters (no discussion, if that number is useful, please). So, sour call could look like:

private void HandleException(Action<string, System.Web.Mvc.ModelState ModelState  > action) {

Edit

Here is an example with an action having parameter:

private void RunHandleException(Action<int> action) 
{
    action(someIntValue);
}

...

RunAndHandleExceptions((someInt) => 
    {    
        //Do some computing      
    });  

And here is an example with a function having a return value:

private void RunHandleException(Func<bool, int> action) 
{
    bool returnValue = action(someIntValue);
}

...

RunAndHandleExceptions((someInt) => 
    {    
        //Do some computing  
        return true;    
    });  

你看过RedirectToAction了吗?

return this.RedirectToAction(c => c.SomeAction(MyParam));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM