In my current project, I had to render elements in the view based on a setting provided by the model(basically it is a configurable thing). Few clients need view element to be rendered in a particular order and few others in a different way. What we did was, saved this elements order in a settings file which could be changed based on the clients. Then created an extension to render this based on the order.
This is what was I was trying to explain. for Client 1 the Login section to be displayed first followed by Password reminder section
For Client 2 , these sections needs be ordered differently
In order to achieve this, I came up with an HtmlHelper extension
///<summary> /// Renders the render items in the provided sequence order. ///</summary> ///<param name="htmlHelper">The HTML helper which is extended.</param> ///<param name="sequenceOrder">The order in which items to be rendered. Sequence starts at an index of 0.</param> ///<param name="renderItems">The items to be rendered in order.</param> ///<remarks> /// Values in the sequence order should match with the total number of render items. /// Invalid sequnce numbers are ignored. ///</remarks> publicstaticvoidOrderBy(this HtmlHelper htmlHelper, int[] sequenceOrder, params Action<HtmlHelper>[] renderItems) { if (sequenceOrder != null && renderItems != null) { foreach (var sequnce in sequenceOrder) { // CHeck whether the sequence is with inthe bounds if (sequnce < renderItems.Length && sequnce >= 0) { renderItems[sequnce].Invoke(htmlHelper); } } } elseif (renderItems != null) { // If the sequence order is not provided, render it in normal order in which items are declared. foreach (var renderItem in renderItems) { renderItem.Invoke(htmlHelper); } } else { // Do Nothing } }
Few weeks ago I started learning Android programming, so this article is an outcome of that out-side office study :). Here I will be explaining – how to create a live wallpaper which looks like an aquarium with fishes swimming across the screen. The fish animation is done using sprite technique.
Creating a new Android project in eclipse (I am not familiar with any other IDEs for Android development :) ). Now create a class for your live wallpaper service, I called it as AquariumWallpaperService, then instantiate the AquariumWallpaperEngine which is responsible for creating the actual Aquarium class which does all the rendering logic. It also controls the flow of Aquarium based Surface callbacks Below is the code for AquariumWallpaperService
@Override publicvoidonSurfaceChanged(SurfaceHolder holder, int format, int width, int height){ super.onSurfaceChanged(holder, format, width, height); }
This interface helps to render an object other than a fish, like plants etc… in future. I have created another abstract class which has common functionalities like changing the position and direction of a fish after a particular interval and called it as AquaticAnimal, this is because I could create specific fishes which just differ by it’s look by extending from this class.
Now to the final bit, that is creating a fish. you might have noticed that I have created an object of ClownFishin the Aquarium class. ClownFish is just a derived class from AquaticAnimal, with a specific sprite image. So if you have sprite images for a shark, you could simple extend a new class for a shark with that image.
Few weeks ago I started learning Android programming, so this article is an outcome of that out-side office study :). Here I will be explaining – how to create a live wallpaper which looks like an aquarium with fishes swimming across the screen. The fish animation is done using sprite technique.
Creating a new Android project in eclipse (I am not familiar with any other IDEs for Android development :) ). Now create a class for your live wallpaper service, I called it as AquariumWallpaperService, then instantiate the AquariumWallpaperEngine which is responsible for creating the actual Aquarium class which does all the rendering logic. It also controls the flow of Aquarium based Surface callbacks Below is the code for AquariumWallpaperService
@Override publicvoidonSurfaceChanged(SurfaceHolder holder, int format, int width, int height){ super.onSurfaceChanged(holder, format, width, height); }
This interface helps to render an object other than a fish, like plants etc… in future. I have created another abstract class which has common functionalities like changing the position and direction of a fish after a particular interval and called it as AquaticAnimal, this is because I could create specific fishes which just differ by it’s look by extending from this class.
Now to the final bit, that is creating a fish. you might have noticed that I have created an object of ClownFishin the Aquarium class. ClownFish is just a derived class from AquaticAnimal, with a specific sprite image. So if you have sprite images for a shark, you could simple extend a new class for a shark with that image.
All these are good because it helps to reduce failures but does everyone follow these all the time?. Sometimes I (or any developer) forgot to go through the checklist due to many reasons like time constraints, lack of concentration etc… and I don’t think we should blame anyone for missing this because - We all are humans and we tends to forget.
Only way we could reduce these mistakes is to automate!!! wherever possible. In my current project, all the aspx page should have direction(dir) attribute in the html tag as part of the localization work. As usual an email with checklist for localizing an aspx page was sent to all the developers, out of that one item was to include dir attribute whenever they add new aspx file.
Everybody followed this in the initial stages but later we started forgetting about this requirement, which caused extra hours of effort to fix it in all the pages.
using System.Linq; using HtmlAgilityPack; using Microsoft.Build.Framework; using Microsoft.Build.Utilities;
namespaceStaticFileAnalysisTask { ///<summary> /// Custom MSBuild task which analyse the HTML code validation. ///</summary> publicclassHTMLCodingStandard : Task { #region Private fields
///<summary> /// Variable that holds the status of this task execution. ///</summary> privatebool _success;
#endregion
#region Public properties
///<summary> /// Gets or sets the source file list. ///</summary> ///<value>The source file list.</value> [Required] public ITaskItem[] SourceFiles { get; set; }
///<summary> /// Gets or sets the list of files to be excluded. ///</summary> ///<value>The excluded files.</value> public ITaskItem[] ExcludeFiles { get; set; }
#endregion
///<summary> /// Executes this task. ///</summary> ///<returns><c>true</c> if task executed successfully; Otherwise, <c>false</c>.</returns> publicoverrideboolExecute() { this._success = true;
foreach (ITaskItem current in SourceFiles) { // If the items is in the exluded list, then skip if(this.IsInExlcudedList(current)) { continue; }
///<summary> /// Method that is responsible for validating the file. ///</summary> ///<param name="path">The full path to the file.</param> privatevoidValidateFile(string path) { HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.Load(path); HtmlNode node = htmlDocument.DocumentNode.SelectSingleNode("//html"); if(node != null) { if(!node.Attributes.Contains("dir")) { this.BuildEngine.LogErrorEvent(new BuildErrorEventArgs( "Invalid HTML coding standard", "SFAT-HTML-1", path, node.Line, node.LinePosition, node.LinePosition, node.LinePosition + node.OuterHtml.Length, "SFAT-HTML-1: Direction(dir) tag is missing", null, "HTMLCodingStandardTask" )); this._success = false; }
} }
///<summary> /// Determines whether an item is in the excluded list. ///</summary> ///<param name="taskItem">The task item which needs to checked.</param> ///<returns> ///<c>true</c> if item is in exlcuded list; otherwise, <c>false</c>. ///</returns> privateboolIsInExlcudedList(ITaskItem taskItem) { if(this.ExcludeFiles == null) { returnfalse; }
For including this in the your web project, open the project file in a text editor and add the below lines
1 2 3 4 5 6 7 8 9 10 11
<UsingTaskAssemblyFile="..\output\StaticFileAnalysisTask.dll"TaskName="HTMLCodingStandard" /> <TargetName="BeforeBuild"> <HTMLCodingStandardSourceFiles="@(Content)" /> </Target> If you want to exclude any files from this verification process, then define an ItemGroup <ItemGroup> <ExcludedContentsInclude="WebForm2.aspx" /> </ItemGroup> and add that to the build action like below <UsingTaskAssemblyFile="..\output\StaticFileAnalysisTask.dll"TaskName="HTMLCodingStandard" /> <TargetName="BeforeBuild"> <HTMLCodingStandardSourceFiles="@(Content)"ExcludeFiles="@(ExcludedContents)" /> </Target>
All these are good because it helps to reduce failures but does everyone follow these all the time?. Sometimes I (or any developer) forgot to go through the checklist due to many reasons like time constraints, lack of concentration etc… and I don’t think we should blame anyone for missing this because - We all are humans and we tends to forget.
Only way we could reduce these mistakes is to automate!!! wherever possible. In my current project, all the aspx page should have direction(dir) attribute in the html tag as part of the localization work. As usual an email with checklist for localizing an aspx page was sent to all the developers, out of that one item was to include dir attribute whenever they add new aspx file.
Everybody followed this in the initial stages but later we started forgetting about this requirement, which caused extra hours of effort to fix it in all the pages.
using System.Linq; using HtmlAgilityPack; using Microsoft.Build.Framework; using Microsoft.Build.Utilities;
namespaceStaticFileAnalysisTask { ///<summary> /// Custom MSBuild task which analyse the HTML code validation. ///</summary> publicclassHTMLCodingStandard : Task { #region Private fields
///<summary> /// Variable that holds the status of this task execution. ///</summary> privatebool _success;
#endregion
#region Public properties
///<summary> /// Gets or sets the source file list. ///</summary> ///<value>The source file list.</value> [Required] public ITaskItem[] SourceFiles { get; set; }
///<summary> /// Gets or sets the list of files to be excluded. ///</summary> ///<value>The excluded files.</value> public ITaskItem[] ExcludeFiles { get; set; }
#endregion
///<summary> /// Executes this task. ///</summary> ///<returns><c>true</c> if task executed successfully; Otherwise, <c>false</c>.</returns> publicoverrideboolExecute() { this._success = true;
foreach (ITaskItem current in SourceFiles) { // If the items is in the exluded list, then skip if(this.IsInExlcudedList(current)) { continue; }
///<summary> /// Method that is responsible for validating the file. ///</summary> ///<param name="path">The full path to the file.</param> privatevoidValidateFile(string path) { HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.Load(path); HtmlNode node = htmlDocument.DocumentNode.SelectSingleNode("//html"); if(node != null) { if(!node.Attributes.Contains("dir")) { this.BuildEngine.LogErrorEvent(new BuildErrorEventArgs( "Invalid HTML coding standard", "SFAT-HTML-1", path, node.Line, node.LinePosition, node.LinePosition, node.LinePosition + node.OuterHtml.Length, "SFAT-HTML-1: Direction(dir) tag is missing", null, "HTMLCodingStandardTask" )); this._success = false; }
} }
///<summary> /// Determines whether an item is in the excluded list. ///</summary> ///<param name="taskItem">The task item which needs to checked.</param> ///<returns> ///<c>true</c> if item is in exlcuded list; otherwise, <c>false</c>. ///</returns> privateboolIsInExlcudedList(ITaskItem taskItem) { if(this.ExcludeFiles == null) { returnfalse; }
For including this in the your web project, open the project file in a text editor and add the below lines
1 2 3 4 5 6 7 8 9 10 11
<UsingTaskAssemblyFile="..\output\StaticFileAnalysisTask.dll"TaskName="HTMLCodingStandard" /> <TargetName="BeforeBuild"> <HTMLCodingStandardSourceFiles="@(Content)" /> </Target> If you want to exclude any files from this verification process, then define an ItemGroup <ItemGroup> <ExcludedContentsInclude="WebForm2.aspx" /> </ItemGroup> and add that to the build action like below <UsingTaskAssemblyFile="..\output\StaticFileAnalysisTask.dll"TaskName="HTMLCodingStandard" /> <TargetName="BeforeBuild"> <HTMLCodingStandardSourceFiles="@(Content)"ExcludeFiles="@(ExcludedContents)" /> </Target>
What makes your application different from others? I strongly feel the user experience that you provides plays an important role to be successful. Some of us might have felt that(at least myself), you woke up one day with a brand new idea but later you realize that somebody had implemented that an year back. Which is a frustrated feeling, I have been to that situation so many times. So even if that idea exists already, how to make that idea a successful one.
Say if you are going to enter in to a world where there are n number of similar applications, how will you attract the users? A great example may be GMail, IMHO they entered to the party when Yahoo and Microsoft where ruling email market. But now GMail is much popular than other email service providers. One reason I could think of for this success is the experience that you get as a user.
Not sure whether anybody has noted this or not but today when was about to send an email to my friend, I got a message box saying Did you mean to attach files?
Yes, GMail reminded me to attach the file. I was surprised to see this, GMail has intelligently scanned what I have typed in the email message and gave me suggestion before sending…. WOW!!!!!. I checked whether Yahoo mail has got this feature, not yet. That makes GMail stand-out from others.
What makes your application different from others? I strongly feel the user experience that you provides plays an important role to be successful. Some of us might have felt that(at least myself), you woke up one day with a brand new idea but later you realize that somebody had implemented that an year back. Which is a frustrated feeling, I have been to that situation so many times. So even if that idea exists already, how to make that idea a successful one.
Say if you are going to enter in to a world where there are n number of similar applications, how will you attract the users? A great example may be GMail, IMHO they entered to the party when Yahoo and Microsoft where ruling email market. But now GMail is much popular than other email service providers. One reason I could think of for this success is the experience that you get as a user.
Not sure whether anybody has noted this or not but today when was about to send an email to my friend, I got a message box saying Did you mean to attach files?
Yes, GMail reminded me to attach the file. I was surprised to see this, GMail has intelligently scanned what I have typed in the email message and gave me suggestion before sending…. WOW!!!!!. I checked whether Yahoo mail has got this feature, not yet. That makes GMail stand-out from others.
There are so many free JQuery Grid plugins out there, in that I liked FlexiGrid just because of it’s look and style. In order to use it in your MVC application you may have to put the Javascript code into your view, which requires the property names of your model in order to generates the Grid columns as well the search options etc… as everybody knows when you deal with hard coded string as the property names in any code, it is error prone.
In order to avoid this problem I thought of creating a html extension which is tightly coupled with your data that is going to bound to the Grid. Which helps the developer from writing any javascript codes.
There are so many free JQuery Grid plugins out there, in that I liked FlexiGrid just because of it’s look and style. In order to use it in your MVC application you may have to put the Javascript code into your view, which requires the property names of your model in order to generates the Grid columns as well the search options etc… as everybody knows when you deal with hard coded string as the property names in any code, it is error prone.
In order to avoid this problem I thought of creating a html extension which is tightly coupled with your data that is going to bound to the Grid. Which helps the developer from writing any javascript codes.
Datepicker is nice and cool plugin for displaying the calendar with ease. It is very easy to use JQuery plugin, it comes as part of JQueryUI library, so if you want to use this – first download JQueryUI from http://jqueryui.com/download and also download JQuery(http://jquery.com/download/) if you haven’t done yet.
and you want to attach datepicker to StartDate and EndDate input fields, what you needs to do is call the datepicker function on the these input field selector like below.
Consider a scenario where your MVC application supports localization, then the selected date displayed in the input fields also should display the in the same date format of the current culture(This format could be custom one or default one).
This leads to you another issue – Datepicker plugin given by the JQueryUI supports different date formats, but it is different from one that is available in .NET. For e.g. in order to display a long day name (“Thursday”) .NET uses dddd* its equivalent in Datepicker is DD.
In order to solve this disparity between the .NET world and Datepicker world, I have created a html helper function, which could generate the Datepicker format from a .Net date format.
///<summary> /// JQuery UI DatePicker helper. ///</summary> publicstaticclassJQueryUIDatePickerHelper { ///<summary> /// Converts the .net supported date format current culture format into JQuery Datepicker format. ///</summary> ///<param name="html">HtmlHelper object.</param> ///<returns>Format string that supported in JQuery Datepicker.</returns> publicstaticstringConvertDateFormat(this HtmlHelper html) { return ConvertDateFormat(html, Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern); }
///<summary> /// Converts the .net supported date format current culture format into JQuery Datepicker format. ///</summary> ///<param name="html">HtmlHelper object.</param> ///<param name="format">Date format supported by .NET.</param> ///<returns>Format string that supported in JQuery Datepicker.</returns> publicstaticstringConvertDateFormat(this HtmlHelper html, string format) { /* * Date used in this comment : 5th - Nov - 2009 (Thursday) * * .NET JQueryUI Output Comment * -------------------------------------------------------------- * d d 5 day of month(No leading zero) * dd dd 05 day of month(two digit) * ddd D Thu day short name * dddd DD Thursday day long name * M m 11 month of year(No leading zero) * MM mm 11 month of year(two digit) * MMM M Nov month name short * MMMM MM November month name long. * yy y 09 Year(two digit) * yyyy yy 2009 Year(four digit) * */
string currentFormat = format;
// Convert the date currentFormat = currentFormat.Replace("dddd", "DD"); currentFormat = currentFormat.Replace("ddd", "D");