Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Code is executed second time with the wrong parameter
Message
 
 
À
24/07/2015 05:09:21
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01622436
Message ID:
01622470
Vues:
95
Unfortunately the second version produces

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject'

Wondering if I can still utilize it or should switch to the first version.

>As you found the solution to the original problem:
>I realize that fwks sometimes generate part of the code or at least form basic templates the code is expected to follow (so do not take this personal), but the parts left standing at the bottom have so much redundancy in them, it is no wonder more time than expected for covered functionality is spent debugging.
>
>Refactoring minimally (all from the hip)
>
>without changing Routing-convention
>
>  [Route("")]
>        public ActionResult Index()
>        {            
>           =  CleanIDs();
>            return View();
>        }
>
>        [Route("Department/{departmentId}")]
>        public ActionResult DepartmentById(int departmentId)
>        {
>           =  CleanIDs();
>            ViewBag.departmentId = departmentId;
>            return View("Index");
>        }
>
>        [Route("Category/{categoryId}")]
>        public ActionResult CategoryById(int categoryId)
>        {
>           =  CleanIDs();
>            ViewBag.categoryId = categoryId;
>            return View("Index");
>        }
>
>        [Route("Item/{itemId}")]
>        public ActionResult ItemById(int itemId)
>        {
>           =  CleanIDs();
>            ViewBag.itemId = itemId;
>            return View("Index");
>        }
>
>        [Route("SubCategory/{subCategoryId}")]
>        public ActionResult SubCategoryById(int subCategoryId)
>        {
>           =  CleanIDs();
>            ViewBag.subCategoryId = subCategoryId;
>            return View("Index");
>        }
>        public CleanIDs()
>        {            
>            ViewBag.departmentId = 0;
>            ViewBag.categoryId = 0;
>            ViewBag.itemId = 0;
>            ViewBag.subCategoryId = 0;
>            return ;
>        }
>
>results in minimally more CPU instructions (calling refactored func + setting 1 property to 0 needlessly), but is much easier to optimize for JIT. LOC is not that much less, but time figuring out the relevant differences is much lower.
>
>Switching over to Dict-like access to ViewBag (speaking in C# or Python concepts) results in even less programmer code even when keeping previous routing calls
>
>
>  [Route("")]
>        public ActionResult Index()
>        {            
>           =  CleanIDs()
>            return View();
>        }
>
>        [Route("Department/{departmentId}")]
>        public ActionResult DepartmentById(int departmentId)
>        {           
>          return ByID("departmentId", departmentId);
>        }
>
>        [Route("Category/{categoryId}")]
>        public ActionResult CategoryById(int categoryId)
>        {
>           return ByID("categoryId", categoryId);
>        }
>
>        [Route("Item/{itemId}")]
>        public ActionResult ItemById(int itemId)
>        {
>           return ByID("itemId", itemId);
>        }
>
>        [Route("SubCategory/{subCategoryId}")]
>        public ActionResult SubCategoryById(int subCategoryId)
>        {
>           return ByID("subCategoryId", subCategoryId);   
>        }
>
>        public ActionResult ByID(string key, int value)
>        {
>            = CleanIDs()
>            ViewBag[key] = value;
>            return View("Index");
>        }
>
>        public CleanIDs()
>        {            
>            ViewBag["departmentId"] = 0;
>            ViewBag["categoryId"] = 0;
>            ViewBag["itemId"] = 0;
>            ViewBag["subCategoryId"] = 0;
>            return ;
>        }
>
>but it is easy to see that most of the individual routing functions can be eliminated if the call is done directly to ByID with the correct parameters.
>
>Identical strings (state example) should be at least kept in variables, fitting here is IMO a constructor function setting the static properties in code via string literals and the url via parameter.
>
>Does your code review not check for a) programming to differences b) following the rule of three?
>
>
>>My MVC controller has the following code:
>>
>>
>>  [Route("")]
>>        public ActionResult Index()
>>        {            
>>            ViewBag.departmentId = 0;
>>            ViewBag.categoryId = 0;
>>            ViewBag.itemId = 0;
>>            ViewBag.subCategoryId = 0;
>>            return View();
>>        }
>>
>>        [Route("Department/{departmentId}")]
>>        public ActionResult DepartmentById(int departmentId)
>>        {
>>            ViewBag.departmentId = departmentId;
>>            ViewBag.categoryId = 0;
>>            ViewBag.itemId = 0;
>>            ViewBag.subCategoryId = 0;
>>            return View("Index");
>>        }
>>
>>        [Route("Category/{categoryId}")]
>>        public ActionResult CategoryById(int categoryId)
>>        {
>>            ViewBag.categoryId = categoryId;
>>            ViewBag.departmentId = 0;
>>            ViewBag.itemId = 0;
>>            ViewBag.subCategoryId = 0;
>>            return View("Index");
>>        }
>>
>>        [Route("Item/{itemId}")]
>>        public ActionResult ItemById(int itemId)
>>        {
>>            ViewBag.categoryId = 0;
>>            ViewBag.departmentId = 0;
>>            ViewBag.itemId = itemId;
>>            ViewBag.subCategoryId = 0;
>>            return View("Index");
>>        }
>>
>>        [Route("SubCategory/{subCategoryId}")]
>>        public ActionResult SubCategoryById(int subCategoryId)
>>        {
>>            ViewBag.categoryId = 0;
>>            ViewBag.departmentId = 0;
>>            ViewBag.itemId = 0;
>>            ViewBag.subCategoryId = subCategoryId;
>>            return View("Index");
>>        }
>>
>>
>>and I invoke the page using max4Sales/SubCategory/2 syntax so the last route fires.
>>
>...
>>and also I have the following states defined at the top:
>>
>>
>>app.config(['$stateProvider', function ($stateProvider) {
>>        $stateProvider.state('home', {
>>            url: '/',
>>            controller: 'max4salesController',
>>            template: ''
>>        }).state('edit', {
>>            url: '/edit/:id',
>>            controller: 'max4salesCrudController',
>>            templateUrl: '/CapacityControl/max4sales/editForm'
>>        }).state('new', {
>>            url: '/new',
>>            controller: 'max4salesCrudController',
>>            templateUrl: '/CapacityControl/max4sales/editForm'
>>        }).state('department', {
>>            url: '/new/:departmentId',
>>            controller: 'max4salesCrudController',
>>            templateUrl: '/CapacityControl/max4sales/editForm'
>>        }).state('category', {
>>            url: '/new/:categoryId',
>>            controller: 'max4salesCrudController',
>>            templateUrl: '/CapacityControl/max4sales/editForm'
>>        }).state('item', {
>>            url: '/new/:itemId',
>>            controller: 'max4salesCrudController',
>>            templateUrl: '/CapacityControl/max4sales/editForm'
>>        }).state('subCategory', {
>>            url: '/new/:subCategoryId',
>>            controller: 'max4salesCrudController',
>>            templateUrl: '/CapacityControl/max4sales/editForm'
>>        });
>>    }]);
>>
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform