Open Source Series 2.: NVelocity

Formerly as part of the Apache Jakarta project, now part of the Apache project itself can be found the Velocity project. Since the beginning, it is the de facto tool for generating template driven applications – websites, documents, etc., the list of projects, websites and tools powered by or capable of using Velocity is endless. As for other great and successfull projects (like Hibernate, which also uses velocity for generating POJO files), this was met with the new wind of .NET – there were NVelocity born. So to make it short – NVelocity is a .Net-based template engine – it permits anyone to use the simple yet powerful template language called VTL to reference objects, execute methods, etc defined in .Net code. Speaking of a web application, this leads to the MVC pattern – this simple to use and set up tool enables you to program against modells in your views, and put the controller in charge of binding the two – this (and NHibernate, and ActiveRecord, and Windsor…) is what lead to the born of the Castle and the Rhino projects – but these are in later post :).

So no whistles nor bells, but the main part of NVelocity is:

  • You create the engine – new NVelocity.App.VelocityEngine(ep);
  • You get a template – engine.GetTemplate(“template.vm”);
  • You create a context – new NVelocity.VelocityContext();
  • You fill the context – context.Put(“key”, “value”);
  • Than you merge the context with the template using the engine – template.Merge(context, outputStream);

The extras are coming mostly in the template file, and by the fact, that there are plenty of way to provide plugins into the system – ResourceLoaders for determining where to get the file (from the filesystem? database? embedded resource?), caches of various parts, etc. So, back to template file – what can I do?

  • I can print the value for a key – ${key}
  • I can use property getters and setters – #set ${object}.prop = “value”
  • I can use loops, if-then-else, define macros, etc
  • Call and return any method with parameterlists which may contain values, arrays, dictionaries, etc – ${object}.Method(${key}, “%{key = ‘value’, key2 = ${key}}”);
  • Meanwhile the whole thing is typesafe and – the template gets compiled into an AST tree the first time it met!

So – I think this is a very handy tool for generating source files, for generating documents and for driving data driven websites.

PS: No, it has nothing to do with Microsoft Velocity – that goes into the cache providers series 🙂

This post is part of the open source series

A short overview of cache providers, part 1

In the following few paragraphs I'll try to a give a less-or-more comprehensive view over the world of cache providers. What we call a cache provider? The first thing people generally do is cache things within their web processes. But this means your cache is duplicated multiple times, once for each thread. This is a waste of memory and you'll get low cache hit rates. If you're using a multi-threaded language or a shared memory API like the built-in System.Web.Cache with in-memory settings, you can have a global cache for all threads, but it's per-machine. It doesn't scale to multiple machines. Once you have 20 webservers, those 20 independent caches start to look just as silly as when you had 20 threads with their own caches on a single box. Cache providers' servers and clients work together to implement one global cache across as many machines as you have – like the state server or sql server options for System.Web.Cache. But is the default implementation of System.Web.Cache flawless and perfect? Some says no – there may be improvement both the server and client sides.

Memcached

Why bother with a linux damon cache implementation made by danga? Answer is easy: first of all – there is a win32 implementation for it. Secondly – there are thons of different APIs to access it from .NET. Three of those APIs am I going to further investigate – the .NET memcached client library, the enyim.com Memcached Client, and BeIT Memcached. (interesting thing to mark – 3 projects, 3 different forge – sourceforge, codeplex and google code 🙂 )

The .NET memcached client library is the one, that mostly follows the schema and technology of the java client – it's a 1:1 port of that one.

The BeIT client comes with many bells and whistles, like socket pooling, uses the ketama algorithm for sticking keys to servers (key – server assigment stays the same even if there are new servers added or removed between), most of the simple value types have their own optimized serialization, have compression schemas, automatic key prefixing, dead server detection, etc.

The enyim.com Memcached client comes with nearly the same set of features, except for an additional one – a provider project to wire it up into System.Web.Cache without any additional steps.

As a special experiment, there are .NET implementations of the memcached server as well, and compared to the native C implementation, they even doesn't work that bad. One of the implementations I tried was part of the experimental hive of the Rhino framework – I'll try to cover that framework in a later post 🙂

33 easy steps to dive into WCF

¾  Step 1 – Read Principles of Service Design: Patterns and Anti-Patterns for an overview of SOA basics (~20 -30 min.)

http://msdn2.microsoft.com/en-us/library/ms954638.aspx

¾  Step 2 – Read Windows Communication Foundation Road Map to get an understanding of WCF goals, and its role in SOA enterprise architecture (~20 -30 min.)

http://msdn2.microsoft.com/en-us/library/aa480211.aspx

¾  Step 3 – Read On the road to Indigo: Prescriptive Guidance for Today’s Technologies by Richard Turner to get an understanding of current state of all Microsoft distributed application technologies (20-30 min)

http://blogs.msdn.com/richardt/archive/2004/03/05/84834.aspx 

¾  Step 4 – Read Windows Communication Foundation Architecture Overview by Yasser Shohoud to learn WCF basics: Addresses, Bindings, and Contracts (~45 min.)

http://www.yassers.com/content/soa/WCFArchOverview.aspx

¾  Step 5 – Read Windows Communication Foundation Architecture Overview to learn WCF basics: Endpoints, Bindings, Contracts, Behaviors (~45 min.)

(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnlong/html/wcfarch.asp)

¾  Step 6 – Complete virtual lab “Understanding Windows Communication Foundation” (90 min.)

http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032315324&EventCategory=3&culture=en-US&CountryCode=US

¾  Step 7 – Complete virtual lab “The Fundamentals of Programming the Windows Communication Foundation Virtual Lab” (90 min.)

http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032291422&EventCategory=3&culture=en-US&CountryCode=US

¾  Step 8 – Look through WCF latest news and announcements (~20 min.)

http://wcf.netfx3.com/blogs/news_and_announcements/default.aspx

¾  Step 9 – Download and build the Data Contract Samples

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/WCF_Samples_MSDNLive/html/7a6107b5-eda1-491a-94a9-8265ba0c698b.asp

¾  Step 10 – Read the article “Serialization in Windows Communication Foundation”

http://msdn.microsoft.com/msdnmag/issues/06/08/ServiceStation/default.aspx

¾  Step 11 – Read Craig McMurty’s Blog on “Versioning Windows Communication Foundation Services”

http://blogs.msdn.com/craigmcmurtry/archive/2006/07/23/676104.aspx

¾  Step 12 – Read the article and review the code for “Discover Mighty Instance Management Techniques For Developing WCF Apps”

http://msdn.microsoft.com/msdnmag/issues/06/06/WCFEssentials/

http://msdn.microsoft.com/msdnmag/code/?url=http://msdn.microsoft.com/msdnmag/issues/06/06/WCFEssentials/default.aspx (Code)

¾  Step 13 – Read the MSDN Documentation on “Synchronous and Asynchronous Operations”

http://msdn2.microsoft.com/en-us/library/ms734701.aspx

¾  Step 14 – Read Steven M. Cohn’s blogs on “WCF: Asynchronous Operations”

http://weblogs.asp.net/stevencohn/archive/2007/02/05/asynchronous-operations.aspx

http://weblogs.asp.net/stevencohn/archive/2007/03/22/wcf-asynchronous-operations-ii.aspx

¾  Step 15 – Read the MSDN Article “Build a Queued WCF Response Service

http://msdn.microsoft.com/msdnmag/issues/07/02/Foundations/default.aspx

¾  Step 16 – Read Designing and Implementing Services for a high level conceptual orientation to design and implement WCF services.

http://msdn2.microsoft.com/en-us/library/ms729746.aspx

¾  Step 17 – Read Hosting WCF Services

http://www.devx.com/codemag/Article/33655/1954?pf=true

¾  Step 18 – Read WCF Essentials – What You Need To Know About One-Way Calls, Callbacks, And Events by Juval Lowy (Source code included)

http://msdn2.microsoft.com/en-us/library/ms729746.aspx

¾  Step 19 – Read Build a Queued WCF Response Service by Juval Lowy (Source code included) for a brief introduction to WCF queued calls followed by an interesting problem-how to get results out of a queued call-and the solution via some cool WCF programming techniques.

http://msdn.microsoft.com/msdnmag/issues/07/02/Foundations/default.aspx

¾  Step 20 – Review Build a Queued WCF Response Service  on Dr Dobb’s Portal

http://www.ddj.com/dept/windows/196900749

¾  Step 21 – Read the MSDN article on Sessions, Instancing, and Concurrency

http://msdn2.microsoft.com/en-us/library/ms731193.aspx

¾  Step 22 – Read Understanding Windows Communication Foundation Extensibility for an overview of WCF’s extensibility points (~20-30 min.)

http://weblogs.asp.net/andresv/archive/2006/10/19/Understanding-WCF-Extensibility.aspx 

¾  Step 23 – Watch MSDN Architecture Webcast: Extending Windows Communication Foundation by Aaron Skonnard for a demonstration of several key extensibility points (~70 min.)

http://msevents.microsoft.com/cui/WebCastEventDetails.aspx?EventID=1032299314&EventCategory=5

¾  Step 24 – Review the following SDK extensibility samples (~180 min.)

¾  Custom Service Host 
http://msdn2.microsoft.com/en-us/library/aa395224.aspx

¾  Message Inspectors
http://msdn2.microsoft.com/en-us/library/aa717047.aspx

¾  Transport Extensibility Samples   
http://msdn2.microsoft.com/en-us/library/aa967733.aspx

¾  Channels Extensibility Samples
http://msdn2.microsoft.com/en-us/library/aa967568.aspx

¾  Message Encoder Extensibility Samples
http://msdn2.microsoft.com/en-us/library/aa967732.aspx

¾  Instancing Extensibility Samples
http://msdn2.microsoft.com/en-us/library/aa967565.aspx

¾  Step 25 – Review “Administration and Diagnostics” branch in MSDN documentation (~30 min.)

http://msdn2.microsoft.com/en-us/library/ms731055.aspx

¾  Step 26 – Review the following integration and interop scenario guides (~120 min.)

¾  Integrating with COM Applications
http://msdn2.microsoft.com/en-us/library/ms733928.aspx
(especially http://msdn2.microsoft.com/en-us/library/ms730294.aspx)

¾  Integrating with COM+ Applications
http://msdn2.microsoft.com/en-us/library/ms733094.aspx

¾  Integration with .NET Remoting
http://msdn2.microsoft.com/en-us/library/ms730332.aspx

¾  Interoperability with WSE 3.0
http://msdn2.microsoft.com/en-us/library/ms734745.aspx

¾  Interoperability with ASP.NET Web Services
http://msdn2.microsoft.com/en-us/library/ms731779.aspx

¾  Step 27 – Review the following migration related articles (~120 min.)

¾  All About ASMX 2.0, WSE 3.0, and WCF (Aaron Skonnard)
http://msdn.microsoft.com/msdnmag/issues/06/01/servicestation/default.aspx

¾  Anticipating Adopting the Windows Communication Foundation: Easing Future Migration  (From ASP.NET)
http://msdn2.microsoft.com/en-us/library/aa738742.aspx

¾  WCF and ASP.NET Compatibility Mode (Wenlong Dong’s Blog)
http://blogs.msdn.com/wenlong/archive/2006/01/23/516041.aspx

¾  Migrating ASP.NET Web Services to WCF
http://msdn2.microsoft.com/en-us/library/ms730214.aspx

¾  Migrating WSE 3.0 Web Services to WCF
http://msdn2.microsoft.com/en-us/library/ms732008.aspx

¾  Step 28 – Read Windows Communication Foundation Transactions Overview to get an understanding of WCF transaction management functionality (~30 min.)

http://msdn2.microsoft.com/en-us/library/ms733904.aspx

Follow up by going through Writing a Transactional Application

http://msdn2.microsoft.com/en-us/library/ms229973(vs.80).aspx

¾  Step 29 – Go through Reliable Messaging demystified blog entry by Shy Cohen (~10 min.)

http://blogs.msdn.com/shycohen/archive/2006/02/20/535717.aspx 

¾  Step 30 – Listen to Secure, Reliable Transacted Messaging with WCF (Part 1)  podcast on Channel 9 (~30 min.)

 http://channel9.msdn.com/Showpost.aspx?postid=173405

¾  Step 31 – Listen to Secure, Reliable Transacted Messaging with WCF (Part 2)  podcast on Channel 9 (~30 min.)

http://channel9.msdn.com/Showpost.aspx?postid=173830

¾  Step 32 – View MSDN TV episode Reliable Messaging in Windows Communication Foundation (~15 min.)

http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20050825IndigoSC/manifest.xml

¾  Step 33 – Complete “Reliable and Transacted Messaging with the Windows Communication Foundation Virtual Lab” (~90 min.)

http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032291421&EventCategory=3&culture=en-US&CountryCode=US

 

Technorati tags: , , ,

Open Source Series 1.: Introduce to the series

In this series (probably) lesser known but very valuable opens source projects will be searched for, found, and most important – write about 🙂

In the following parts I try to cover some of my favourites first, but beside having open eyes I have ears as well – if you know some OS project gems, that are useful, don’t hesitate to comment/contact.

Technorati tags:

How to put ControlState into ViewState and how to put ViewState into session

It’s a common problem, that your viewstate is bloating up under heavy usage (complex data editing scenarios with multiple grids enabling editing, etc). ASP.Net 2.0 comes up with a handy solution for such cases by letting you easily redefine your PageStatePersister in your page with (under .NET 1.0/1.1 this could be done using LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium):

    protected override PageStatePersister PageStatePersister
    {
        get
        {
            return new SessionPageStatePersister(this);
        }
    }

After changing this (and applying a custom base page for all my pages with:

 
<pages pageBaseType=”PageBase” />
 

for this to work it is needed to derive all your aspx pages from that page!)

I got the result from ~3500 viewstate bytes to ~1500 viewstate bytes. But what can I do with that 1500 bytes? What is in it? I started ViewStateDecoder 2.1, and checked into it, what I saw was surprising: all usual viewstate data was wiped out, but there were still the controlstate! So it’s not the same as the PersistanceMedium calls as they were formerly… How to get rid of that 1500 byte? After using the Reflector a bit I found out, that all depends on a browser capatibility called RequiresControlStateInSession, when it’s set to true, than the controlstate is persisted into the session as well. Last question was: how to let the system know, that my browser has the RequiresControlStateInSession capatibility? It’s easy with the new syntax for browserCaps:

  <system.web>
    <browserCaps>
      <case>
        RequiresControlStateInSession=true
      </case>
    </browserCaps>
  </system.web>

And the day is saved, now I have a viewstate of a 57 bytes, which contains a pair of a boolean and an ID 🙂