Thursday, 26 March 2015

Overriding default HTML encoding in jQuery Template

${template-variable} syntax of jQuery Template encodes the HTML if present in the value passed to the template. You can override the default encoding behaviour using {{html template-variable-containing-html}} instead of the ${template-variable} syntax. Here is an example.


<div id="container">
    <script id="sampleTemplate" type="text/x-jquery-tmpl">
        <div id="cust_${id}">
        <span>${name}</span>:
      
       {{html phone}}

    </script>
</div>

<script type="text/javascript">

$(document).ready(function() {
 
var customer = {
            id: 101, 
            name: 'Test',
            phone: '<b>123-456-777</b><br/><b>111-223-455</b>'
        };

$('#sampleTemplate').tmpl(customer).appendTo('#container');

});

</script>

Monday, 9 March 2015

How to iterate through Microsoft Enterprise Library CacheManager?

Microsoft.Practices.EnterpriseLibrary.Caching.ICacheManager doesn't have a method / property that allow you retrieve the entire Cache items together. I had such a requirement and found a work around. This is using reflection to get "realCache" field in CacheManager and then looping through Cache.CurrentCacheState.

GetData() retrieves a single item from the Cache.

ICacheManager cacheManager = CacheFactory.GetCacheManager("cacheName");
object cacheItem = cacheManager.GetData("cacheKey");

Retrieving and iterating the entire Cache


using Microsoft.Practices.EnterpriseLibrary.Caching;

... 

string cacheName = "your-cache-name";
Cache cache;

if (TryGetCache(cacheName, out cache))
{
  foreach (DictionaryEntry cacheEntry in cache.CurrentCacheState)
  {
    object key = cacheEntry.Key;
    CacheItem cacheItem  = (CacheItem)cacheEntry.Value;
    object value = cacheItem.Value;
    Console.WriteLine("{0}: {1}", key, value)
  }
}

private static bool TryGetCache(string cacheName, out Cache cache)
{
  try
  {
    ICacheManager _cacheManager = CacheFactory.GetCacheManager(cacheName);
    cache = (Cache)_cacheManager.GetType().GetField("realCache", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(_cacheManager);
  }
  catch (Exception)
  {
    cache = null;
    return false;
  }
  return true;
}

Thursday, 5 March 2015

DateTime format strings in .NET

A date and time format string defines the text representation of a DateTime that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time. A custom format string consists of one or more custom date and time format specifiers.

Usually a format string is used with DateTime.ToString() or string.Format()

Examples

// DateTime to String

DateTime.Today.ToString("MMM dd, yyyy") // Mar 05, 2015

string.Format("{0:dd/MM/yy HH:mm:ss}", DateTime.Now) // 05/03/2015 14:05:33

string.Format("{0:hh:mm:ss tt}" , DateTime.Now) // 02:05:33 PM

// String to DateTime

string dateString = "Mar 05, 2015";
DateTime dateTime = DateTime.ParseExact(dateString , "MMM dd, yyyy", null);

Format Specifiers cheat sheet
Day of the month - "d", "dd"

Name of the day of the week - "ddd", "dddd" 

Month - "M", "MM", "MMM", "MMMM"

Year - "y", "yy", "yyy", "yyyy"

Hour (12 hour clock) - "h", "hh"

Hour (24 hour clock) - "H", "HH"

AM/PM designator - "t", "tt"

Minute - "m", "mm"

Second - "s", "ss"

MSDN >>

Download DateTime Formatting Utility from Microsoft : The Format Utility (Formatter.exe) is a Windows Forms application that allows you to apply standard or custom format strings to either numeric values or date and time values and to determine how they affect the result string.