Archive for February, 2011

I Like ASP.NET Web Pages–Part II

Continuing from Part I, here are more fun – using .NET 4 dynamic.

  • Extension methods for creating dynamic objects
  • Validation of dynamic objects (declarative / DSL style)
  • Unit Testing within web site
  • Mock, stub, fake all in one

Extension methods for creating dynamic objects

To create DTO from database and/or screen (html form), I developed two extension methods.

image

They made it very easy to create DTO in model.

image

From the controller, this is how to let the model create a new DTO from screen and save it to database.

image

The convention here is, table column name = object property name = html input name. And they are case sensitive.

Validation of dynamic objects

Since the DTO is dynamic object, there are no re-defined class to use metadata attributes (data annotations). I think it’s a good thing. It gives me a chance to do validation declaratively. The example below is self-explanatory enough because of its declarative style.  It looks like I invented a validation DSL.

image

Unit Testing

One limitation of web sites is that you cannot create a unit test project and reference to the web site to test classes in the web site. I created in the Rabbit Framework a Testing Module. It provides a familiar way of writing unit tests using TestClass, TestMethod attributes, and an Assert class.

image

It also has a test runner page.

image

The test runner page is REST/MVC style. It has URL patterns below.

~/Test                                                 the entry page to unit testing
~/Test/All                                           lists all test classes and test methods
~/Test/TestClass                            runs test methods within the test class
~/Test/TestClass/TestMethod      runs a test method within the test class

Mock, stub, fake all in one

Mocking an interface is difficult. It requires emitting classes dynamically, like what Moq does. Mocking a concrete class is more difficult.

Mocking using dynamic object is very easy. The Mock class in Rabbit Framework probably is the world’s simplest mock implementation. 160 Lines in total. No dependency to any 3rd party library.

image

The Mock class is derived from the DynamicObject.  It allows to,

  • Verify the sequence of methods to be called
  • Verify the times of methods to be called
  • Verify the parameter count being passed to methods
  • Verify each parameter’s type being passed to methods
  • Verify each parameter’s value being passed to methods

I really enjoyed running unit tests with mocking feature on a web page especially with the auto refresh tool.

Add comment February 22nd, 2011

I Like ASP.NET Web Pages–Part I

ASP.NET Web Pages is a new Microsoft technology for web development. But the built-in template is misleading. It lets new / first time user think it is just an ASP with the Razor syntax. But it is much powerful than that. After all, I created a few fun stuffs with it.

Simple MVC

ASP.NET Web Pages’ Razor engine is the foundation of ASP.NET MVC 3. I found it is possible to do a kind of simple MVC w/o using MVC 3, thanks to ASP.NET Web Pages’ built-in magic routing. For a request like http://…/a/b/c/d/e, it routes to /a.cshtml. a.cshtml then can be used as controller. I created the Get and Post attributes for cshtml page methods to make it Sinatra style.

image

The RenderPage function invokes a view and pushes data to the view at the same time.

Fluent Service API (Monad)

Unlike traditional models / services, where the functions usually return object(s) of some class. My service here returns itself instead, so that the operations can be chained. Exception handling, unit of work and transaction are all easier by chaining. This is inspired by the concept of Monad. Does it looks similar to jQuery.

var service = new Service(selector).Op1().Op2().Op3()…..

The only question is how to spit out the object from the operation chain at the end. It could be a get function, a property and etc. I chose to have an index property just for fun (looks like jQuery).

service[0] // this is the object (DTO, data transfer object) to be displayed in the view.

Dynamic DTO

Back to the age of C/C++, the functions and classes require to be defined in separated .h files. In Java/C#, this is not required. Things get simplified. But the compilers still require the classes to be bound to some interfaces in order to link them. Now in the era of .NET 4 dynamic, world is further simplified.

DTO, the objects that services operate on, can be dynamic. My service class has a dynamic object as DTO. When the object was created from the Request Form, all the properties are created on the fly w/o pre-defining classes and interfaces.

image

If passed the dynamic object to the view via then RenderPage function, we can retrieve the data within the view via the Page property.

image

BTW, the Page property of an ASP.NET Web Page is also a dynamic property bag. We can push data and retrieve data like regular properties.e.g. Page.hasError.

After used above tricks in my GitWeb project on Github. I am really happy with ASP.NET Web Pages so far.

Add comment February 17th, 2011

Visualizing Git Repository

Git Web Access exposed Git repositories through OData. I can use jQuery and HTML 5 Canvas to  visualize the commit histories and branches.

image

image

Add comment February 8th, 2011


Calendar

February 2011
M T W T F S S
« May    
 123456
78910111213
14151617181920
21222324252627
28  

Posts by Month

Posts by Category


1