Is it possible to do this without using the if if method (asp.net mvc post action)

According to the anti-campaign , it is better not to use ifs in our code. Can someone tell me if I can get rid of if in this code snippet?

[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(OrganisationInput organisationInput) { if (!this.ModelState.IsValid) { return View(organisationInput.RebuildInput<Organisation, OrganisationInput>()); } var organisation = organisationInput.BuildEntity<Organisation, OrganisationInput>(); this.organisationService.Create(organisation); return this.RedirectToAction("Index"); } 
+4
source share
5 answers

As you ask: yes, it is possible.

Create an abstract class A with an abstract method that returns an ActionResult created using the factory method, which uses the Func dictionary, which creates subclasses of A based on the state of the controller computed from the model (in your case, from the Instance.Model.IsValid controller to the property value. )

Code example:

 public abstract class ModelStateValidator { private Controller controller; protected Controller Controller { get { return controller; } } public abstract ActionResult GetResult(); #region initialization static ModelStateValidator() { creators[ControllerState.InvalidModel] = () => new InvalidModelState(); creators[ControllerState.ValidModel] = () => new ValidModelState(); } #endregion #region Creation private static Dictionary<ControllerState, Func<ModelStateValidator>> creators = new Dictionary<ControllerState, Func<ModelStateValidator>>(); public static ModelStateValidator Create(Controller controller) { return creators[GetControllerState(controller)](); } private static ControllerState GetControllerState(Controller c) { return new ControllerState(c); } internal class ControllerState { private readonly Controller controller; private readonly bool isModelValid; public ControllerState(Controller controller) : this(controller.ModelState.IsValid) { this.controller = controller; } private ControllerState(bool isModelValid) { this.isModelValid = isModelValid; } public static ControllerState ValidModel { get { return new ControllerState(true); } } public static ControllerState InvalidModel { get { return new ControllerState(false); } } public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) //I can show you how to remove this one if you are interested ;) { return false; } return this.isModelValid == ((ControllerState)obj).isModelValid; } public override int GetHashCode() { return this.isModelValid.GetHashCode(); } } #endregion } class InvalidModelState : ModelStateValidator { public override ActionResult GetResult() { return Controller.View(organisationInput.RebuildInput<Organisation, OrganisationInput>()); } } class ValidModelState : ModelStateValidator { public override ActionResult GetResult() { return this.Controller.RedirectToAction("Index"); } } 

80 extra lines, 4 new classes to delete one if.

your use, not if, calls the method as follows:

 ActionResult res = ModelStateValidator.Create(this).GetResult(); 

NOTE. Of course, it should be configured to match the code that is between the ifs in the original question, this is just code example :)


Adds extra unnecessary complexity? YES.

Contains ifs? NOT.

Should you use it? Answer yourself :)

+4
source

Looks like a valid use for "if".

the anti-campaign seems to be against abuse of if statments (i.e., too many nested ifs), etc., and not to completely destroy them.

They are necessary.

+13
source

Use the ternary operator :-) This is worse, but it is not.

+1
source

This is permissible if in this case, since it can be only one of two states. The anti-if campaign is more focused on endlessly open if / else / switch statuses. Looks like a good campaign!

0
source

You can just use the While loop ... but this is like an illustrious if statement.

  private static bool Result(bool isValid) { while(!isValid) { return true; } return false; } 
0
source

All Articles