yysun's space

Posts Tagged ‘rabbit framework

Recently I have made some improvements to the Rabbit Framework. I figured out how to inject a base class between WebPage and the Razor page, so that to create a Web Form or a MVC controller is just to inherit from the base classes.

@inherits Rabbit.WebForm
@functions {
         void button_click(string name)


@inherits Rabbit.MvcController
@functions {  
    void Index() {

Another interesting thing I did is to sync data between C#, JavaScript and HTML inputs.

1. C# fields (both public and non-public) are populated with HTML inputs.

<input type=”text” name=”txtEmail” value=”@txtEmail”/>

@functions {
    string Email = “”; // it will have the value user typed after post.

This is similar to the model binding concept found in ASP.NET MVC. The difference is here we bind to fields, where ASP.NET MVC binds to action parameters (actually, we do that too).

2. C# public fields are synced with JavaScript through a JavaScript object, called webForm.

@functions {
      public int count = 20;

alert(webForm.count); // it shows 20

And vice versa, if changed the value in JavaScript, the C# field has the new value after post.

The C# data are serialized into JSON. On the client side, it can be rendered into HTML using jQuery template or jsrender.

It also could be a solution to view state to some degree I think.

Download the sample site to see how it works.




I found myself hitting F5 in IE like crazy. Every time after modified the web files, in order to preview the result, I need F5 to refresh IE.

So, I created a small Windows Forms application that monitors the web site folder (using FileSystemWatcher). When web file changed, it searches all IE browsers that are from the web site. and send key F5 (actually post message via p-invoke).

It works very well especially on two monitors. Saving files on one monitor, browser refreshes on the other.


It is very convenient. After saved the files, I will see the result immediately. It is not only useful for developing UI. It is also great for unit testing.

After modified and saved the tests in the Rabbit Framework, the test runner page refreshes automatically.

I didn’t make it a Visual Studio Plug-in, because I need it when using Visual Studio Express and WebMatrix.

I have built Web Forms on top of ASP.NET Web Pages.

The HTML is clean and unobtrusive. It uses HTML 5 data attributes. No messed up Ids. No view state. It supports multiple forms on a page.


The server side event handler has meaningful parameters. My Web Forms engine will feed the parameters with data comes in the HTML form posted.


Nice and simple. I will release it in the Rabbit Framework.

After Part I and Part II, here comes the part of making a dynamic site.

I am always impressed by PHP based CMS, like Joomla, Drupal, Wrodpress and etc. Nothing from .NET comes close to them.

With the WebMatrix, I want give it a try myself to build a dynamic and extensible site. It is going to have features of

  • Modules
  • Plugins
  • Templates
  • NuGet Package


Modules are a deployment packages of certain functionality. E.g. Pages, Blog, News, User Management … Modules are deployed as NuGet Packages. Modules can be enable and disable at runtime.

Many CMS have the concept of module (Drupal, XOOPS, DotNetNuke, Orchard…). It is called Component in Joomla.

Module functions are extensible. Modules publish extension points to allow other modules to extend functions.

The concept of Extension points is famous in Eclipse. MEF uses Import and Export. WordPress uses Hooks. Inspired by WordPress, I will use Hooks.

Hooks are a loosely coupled event publication and subscription. From anywhere of the site modules, you can request to run a hook (like fire a event). If any module has added (registered) the hook implementation, it will be invoked. The result of all hooks are chained.

E.g. In the _PageStart, there is a line.

Layout = Site.RunHook("get_layout") as string;

The template module responses to the hook get_layout and return the layout file name.

That’s it. The core mechanism of a dynamic site is that modules request hooks and as well as implement the hooks. Hooks glue all program pieces together.

Put it all together I will create a framework, the Rabbit Framework for WebMatrix.

Name it Rabbit, just because rabbit are small, light, fast, delicate … and this year is the Year of Rabbit.

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.


They made it very easy to create DTO in model.


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


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.



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.


It also has a test runner page.


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.


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.


  • None
  • Pk: The horizontal view is a pleasure to use! Good thinking
  • randyburden: A valiant and commendable effort. Your use of Tuple is a little weird but it offers a feature that most other microORMs don't. Your use of a static Gu
  • reav: great work done on Rabbit Framework. just started to learning it, and by now i think it will solve all my problems and questions, that i had in webpag