How to not use an if else statement when I get the type of a model...

I have the following if statement that I'm returning a model if it match my current model. I'm trying to re write the example bellow without an if else statement. Any suggestions would be great...

        public ActionResult ReportWriter(ReportRequestViewModel viewModel)
        {
            bool hasAccess = true;
            if(!hasAccess)
            {
                return RedirectToAction("Index", "Reports", new { area = "Reports" });
            }
            ViewBag.IsAngularJS = false;
            var model = GetViewModel(viewModel.ReportType.ToString(), viewModel.ConfigurationId);
            var type = model.Data.GetType().Name;
            ReportViewModel vm = null;
            #region ViewModel Casting
            if (type.Equals("SalesReportViewModel"))
            {
                vm = model.Data as SalesReportViewModel;
            }
            else if (type.Equals("CustomerReportViewModel"))
            {
                vm = model.Data as CustomerReportViewModel;
            }

Open in new window

yguyon28Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
You could use a ternary; e.g. -
public ActionResult ReportWriter(ReportRequestViewModel viewModel)
{
    bool hasAccess = true;
    if (!hasAccess)
    {
        return RedirectToAction("Index", "Reports", new { area = "Reports" });
    }
    ViewBag.IsAngularJS = false;
    var model = GetViewModel(viewModel.ReportType.ToString(), viewModel.ConfigurationId);
    ReportViewModel vm = model.Data is SalesReportViewModel ? model.Data as SalesReportViewModel : model.Data as CustomerReportViewModel;
}

Open in new window

-saige-
gr8gonzoConsultantCommented:
Just cast it as a ReportViewModel, since that's what vm is (it won't lose the original type - it's simply using the parent class as a container). Also, use the "is" operator instead of running GetType().Name and comparing it to a string (which forces .NET to create an instance of that string just for the sake of comparison, which is inefficient).

var model = GetViewModel(viewModel.ReportType.ToString(), viewModel.ConfigurationId);
ReportViewModel vm = null;
if(model.Data is ReportViewModel)
{
  // This works since SalesReportViewModel and CustomerReportViewModel are both sub-classes of ReportViewModel
  vm = (ReportViewModel)model.Data;
}

Open in new window


The object doesn't actually change, so you can still check the exact type later if you need to. For example, later on if you want to run something specific if it's a SalesReportViewModel, you can still do:

if(vm is SalesReportViewModel)
{
   var salesReportViewModel = (SalesReportViewModel)vm;
   ...code specific to SalesReportViewModel...
}

Open in new window

Miguel OzSenior Software EngineerCommented:
I agree with gr8gonzo that you only need to cast it as a model, but my suggestion is to check how the model is build.
Some questions:
Q1. What is the model.Data type?
Q2. How vm variable is used in the code?
Q3. Are all targeted models (SalesReportViewModel, CustomerReportViewModel) derived from ReportViewModel?
If not why not.

If the base class of of your models is ReportViewModel and your vm variable code is use as ReportViewModel , then there is no need for casting at all just make sure model.Data  type is ReportViewModel and your code can be simplified.


Note: If more help needed please answer questions above and post code for GetViewModel
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.