I'm currently using the ASP.NT MVC RC1 to implement a basic timesheet application. I'd like to follow the DRY principles but finding it difficult in one particular case:
One of my views, a partial view actually, has a number of textboxes that represent the number of hours spent on a particular task, one textbox per day of the week. When I initially load the page, I want a textbox in the view to display the total of all those hours. Additionally, I want that total to update as I change the values in the textboxes. The update to the textboxes does not cause a full postback, only an AJAX postback that does not do anything with the results (the postback updates the value on the DB but the return ActionResult is an EmptyResult as there is nothing I need to update on the UI).
Currently I have the controller create a view that is populated with the "total" for that view, so the logic that adds all the values is in the C# controller. On the UI side, I have javascript that updates the total on the UI level. But this isn't good because if I change the logic behind how that total is calculated, I have to change it in two places! AHH! NOT DRY!
So, how can I do this? The only answer that comes to me so far is to scrap the javascript code that calculates the total on the UI and instead have the AJAX postback return the new "total" for that view.
Are there other approaches?
You could try Nikhil Kothari's Script# project. It's a cross-compiler that compiles and translates from C# to browser independent javascript, which lets you share logic. Its used by many groups inside Microsoft for exactly that purpose.
More, from the website:
Script# brings productivity to Ajax and JavaScript development. Script# is a free tool that enables developers to author C# source code and subsequently compile it into regular script that works across all modern browsers, and in doing so, leverage the productivity and power of existing .NET tools as well as the Visual Studio IDE. Script# empowers you with a development methodology and approach that brings software engineering, long term maintainability and scalable development approaches for your Ajax applications, components and frameworks.
Script# is used extensively by developers within Microsoft building Ajax experiences in Windows Live, Office to name just a couple, as well as by a external developers and companies including Facebook. If you’re building Ajax-based RIA applications, you owe it to yourself to try Script# today and see if it can help improve your own Ajax development!
Having the logic for totalling the time in both C# and Javascript is not DRY in the strictest sense, that is true. One might split hairs over the fact that they are two different languages operating in two separate environments, but at the end of the day if you change one you have to change both.
I think it's a tradeoff between form and function. How important is it to have the javascript? If the AJAX call is just too slow (a real possibility) then this may be the time to realize that the DRY principle is a guiding principle, not a law.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With