Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to wrap functions in a standard try/catch function in c#

I know there are lots of delegate/func examples but I can't find any examples that will work for me, or I just don't understand them.

I'm using asp.net MVC for a website, and the website needs some web service calls for an outside application to interact with my app. These all need a function to execute (going to db and whatnot), and return a similar data model every time. I want to wrap each call in a try/catch and populate the model.

Here is the generic code that happens every call.

var model = new ResponseDataModel();
try
{
     //execute different code here
}
catch (Exception ex)
{
    model.Error = true;
    model.Message = ex.ToString();
}
return View(model); // will return JSON or XML depending on what the caller specifies

This is one of the controller methods/ functions that I am using

public ActionResult MillRequestCoil()
{
    var model = new ResponseDataModel();
    try
    {
        /* edit */
        //specific code
        string coilId = "CC12345";

        //additional code
        model.Data = dataRepository.doSomethingToCoil(coilId);

        //replaced code
        //model.Data = new { Coil = coilId, M3 = "m3 message", M5 = "m5 message" };
        model.Message = string.Format("Coil {0} sent successfully", coilId);
    }
    catch (Exception ex)
    {
         model.Error = true;
         model.Message = ex.ToString();
    }
    return View(model);
}

I would like to be able to somehow convert the specific function to a variable to pass into the generic code. I've looked at delegates and anonymous funcs but it's pretty confusing until you do it yourself.

like image 723
Brandon Avatar asked Oct 14 '25 07:10

Brandon


2 Answers

Put the following somewhere accessible:

public static ActionResult SafeViewFromModel(
    Action<ResponseDataModel> setUpModel)
{
    var model = new ResponseDataModel();
    try
    {
        setUpModel(model);
    }
    catch (Exception ex)
    {
        model.Error = true;
        model.Message = ex.ToString();
    }
    return View(model); 
}

and then call it like:

public ActionResult MillRequestCoil()
{
    return MyHelperClass.SafeViewFromModel(model =>
    {
        string coilId = "CC12345";
        model.Data = new {
            Coil = coilId,
            M3 = "m3 message",
            M5 = "m5 message" };
        model.Message = string.Format("Coil {0} sent successfully", coilId);
    });
}
like image 159
Rawling Avatar answered Oct 18 '25 04:10

Rawling


public interface IAction{
    public void doAction(ResponseDataModel model);
}

public class Action1 : IAction
{
    public void doAction(ResponseDataModel model)
    {
        string coilId = "CC12345";
        model.Data = new { Coil = coilId, M3 = "m3 message", M5 = "m5 message" };
        model.Message = string.Format("Coil {0} sent successfully", coilId);
    }
}

class Class1
{
    public ActionResult MillRequestCoil(IAction action)
    {
        var model = new ResponseDataModel();
        try
        {
            //specific code
            action.doAction(model);
        }
        catch (Exception ex)
        {
            model.Error = true;
            model.Message = ex.ToString();
        }
        return View(model);
    }
}

Use:

var result = MillRequestCoil(new Action1());

or execute other code

var result = MillRequestCoil(new ActionXY());
like image 24
user1519979 Avatar answered Oct 18 '25 03:10

user1519979



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!