tags:

views:

302

answers:

3

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?

A: 

If you want code to execute on both, your server and the client, you could use/write an expression tree to JavaScript compiler.

On the serverside, you evaluate the expression and send the same expression translated to JavaScript to the client.

SealedSun
Are you suggesting to use a C#->Javascript translator to create the javascript client side? Or perhaps you mean to use javascript on the server side as well as client side?
MBonig
No i think he meant have an "expression" that can be executed by both C# AND Javascript
Neil N
Yes and no. Expression trees are in-memory representations of a subset of C#/VB.NET expressions. They can easily be evaluated on the server. You'd however have to write a component that translates such expression trees to JavaScript.
SealedSun
+1  A: 

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.

Dave Swersky
It *is* a guiding principle, not a law. If you need the logic both places, and it is not technically feasible to share the same implementation, then you'll have to implement it twice. Tough luck, that's life. ;)
jalf
@jalf: exactly. A (any) guideline is not an unbreakable law of existence.
Richard
OK, noted and changed ;)
Dave Swersky
+2  A: 

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!"

Chris Hynes