Meaning of IController returned in ternary operator

0

Could someone tell me what this sentence is telling me?

                 return controllerType != null
                         ? _container.Resolve(controllerType) as IController
                           : base.GetControllerInstance(requestContext, controllerType);

Inside the code:

using Dominio.MesaEntrada.Expedientes;

namespace MVC.Client.Extensiones.ControllerFactories
{
    public class UnityControllerFactory : DefaultControllerFactory
    {
        private readonly IUnityContainer _container;

        public UnityControllerFactory(IUnityContainer container)
        {
            _container = container;
        }


        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            try
            {
                 return controllerType != null
                         ? _container.Resolve(controllerType) as IController
                           : base.GetControllerInstance(requestContext, controllerType);
            }
            catch (ResolutionFailedException)
            {
                //Log the exception if needed
                //Try to resolve using default factory
                return base.GetControllerInstance(requestContext, controllerType);
            }
         }
    }
}
    
asked by pj2016 26.12.2016 в 13:52
source

2 answers

0

Tells you that if controllerType != null then returns

_container.Resolve(controllerType) as IController

On the other hand, if controllerType == null returns

base.GetControllerInstance(requestContext, controllerType)

It is a ternary operator, more explained in: link

    
answered by 26.12.2016 в 14:25
0

It's actually a if but one line, example:

% traditional% co:

 string resultado = "";    
 if (bandera){
    resultado = "Bandera es verdadero.";
 }else{
    resultado = "Bandera es falso.";
 }

% simplified% co:

 string resultado = bandera ? "Bandera es verdadero." : "Bandera es falso.";    

The two previous examples do the same, add value to the result variable based on whether the flag variable is If or If .

Explained in another way the simplified true is divided into three sections:

  • Condition
  • True result
  • False result
  • The syntax would be the following:

    <condicion> ? <resultado true> : <resultado false>
    

    I hope I could have helped you, regards.

    Explanation of the code:

     protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
            {
                try
                {
                     return controllerType != null
                             ? _container.Resolve(controllerType) as IController
                               : base.GetControllerInstance(requestContext, controllerType);
                }
                catch (ResolutionFailedException)
                {
                    //Log the exception if needed
                    //Try to resolve using default factory
                    return base.GetControllerInstance(requestContext, controllerType);
                }
             }
    

    The false method receives a if object and receives the type of object that is: GetControllerInstance , which may have data or be System.Web.Routing.RequestContext requestContext

    What you do first is to ask if the type received is not Type controllerType :

    controllerType != null
    

    If this condition is met:

    _container.Resolve(controllerType) as IController
    

    null < --- is an object that you have defined above

    It seems to call the method null that receives as parameter the _container and converts it to the type .Resolve < - For you to return the controllerType that is required for that type of IController

    If the condition is not met or if the type of controller is controller :

    base.GetControllerInstance(requestContext, controllerType)
    

    An instance of the base is returning ( controller ), and is based on null received and DefaultControllerFactory

    In short, this code is using Interfaces and inheritance to obtain an object of type controllerType (it must be an interface), with requestContext all it does is validate so that you do not return an object IController or empty.

        
    answered by 27.12.2016 в 11:55