Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Code is executed second time with the wrong parameter
Message
De
24/07/2015 12:17:22
 
 
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01622436
Message ID:
01622475
Vues:
46
As long as you doc it along the lines "before adding too many switch lines, look for alternate coding" should be close enough for the role of 3 ;-)


>ViewBag is a dynamic object and properties are added to it in run-time using ViewBag.property.
>
>In any case, I solved the problem with just a switch. Not too elegant, but should be OK for my purpose. I don't want to make the code slower.
>
>>Well, it is statically typed C#, and if ViewBag is no dict... ;-)
>>
>>But my "Switching over to Dict-like access to ViewBag (speaking in C# or Python concepts)" is perhaps a bit too [typ..crypt]ical for my mental shorthand - sorry.
>>
>>As I have no idea how [and often] ViewBag is used, I did not want to hint at basing it on a dict or just enhancing the object with a method granting the typical dict access types via key and value. If you do NOT switch over to a dict based type you either enhance the object itself or peek at the extension method of something like the answers of
>>http://stackoverflow.com/questions/13292078/refer-to-a-property-name-by-variable
>>(did google myself but picked the first answer sounding not too far off)
>>(Rick has written quite a bit on Reflection, might be a good source to get a refresher, as he writes (at least for my taste) well balanced ;-) )
>>
>>as this allows you to use functionality equal to
>>
>>JS syntax of Object[Propertyname]= value
>>Vfp Object.AddProperty(tcPrperty, tuValue)
>>[and Python objects just ARE dicts ;-)]
>>
>>in C# as well, if you cannot base your object on a dict (even if only for the possibly major task of refactoring).
>>
>>It will give you a performance hit via reeflection, but AFAIR from Ricks perf measurements this is nothing to be worried about if not done in tight loops running oodles of circles. But coding with Dict-like acces you easily gain some of the brevity found in dynamic languages back, so sometimes it might be a good idea to take a mental step back and ponder if coding might be streamlined if some data structure previously coded with fields/properties is switched over to dict/hashtable. And yes, you will loose some Intellisense - but get code which is more succinct.
>>
>>
>>
>>>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'
>>>>>        });
>>>>>    }]);
>>>>>
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform