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");
What happens if your client complains that your application is running very slow!!! or in your load/stress testing you found that some functionalities are very slow in executing than expected. This is the time where you go for profiling the execution, to analyse the root cause of these issues.
So how we could develop a profiler, where we don’t have to wrap our normal code in a profiling code.
With the help of AOP (Aspect-Oriented Programming) we could do the profiling task without interrupting the actual code.
AOP is a programming paradigm in which secondary or supporting functions are isolated from the main program’s business logic
So in order bring the AOP functionality into this application, I am going to use a third party library PostSharp which I believe this is one of the best that is available in the market.
So, now we have got the basic things to start with and now let’s start coding….
Start a new solution in visual studio and add a new console application project to it. Then add the below references to the newly created project
Add reference to the Log4Net.dll, PostSharp.Laos.dll and PostSharp.Public.dll (Please read https://www.postsharp.net/documentation to get the basic installation procedure). Next, create a new attribute class called ProfileMethodAttribute – this class is responsible for doing the profiling work. Make sure that you have decorated this class with Serializable attribute
This class actually derives from OnMethodBoundaryAspect, it has got two methods OnEntry and OnExit which we needed. These method will be called before the start of a method execution and at the end of method execution respectively, when this attribute is decorated against a method.
When a call comes to OnEntry method, we will first log the execution call using the LoggerHelper, then start a clock using another helper class Profiler
///<summary> /// Helper class that wraps the timer based functionalities. ///</summary> internalstaticclassProfiler { ///<summary> /// Lock object. ///</summary> privatestaticreadonlyobject SyncLock = newobject();
///<summary> /// Variable that tracks the time. ///</summary> privatestaticreadonly Dictionary<int, Stack<long>> ProfilePool;
///<summary> /// Initializes static members of the <see cref="Profiler"/> class. ///</summary> staticProfiler() { ProfilePool = new Dictionary<int, Stack<long>>(); }
///<summary> /// Starts this timer. ///</summary> publicstaticvoidStart() { lock (SyncLock) { int currentThreadId = Thread.CurrentThread.ManagedThreadId; if (ProfilePool.ContainsKey(currentThreadId)) { ProfilePool[currentThreadId].Push( Environment.TickCount ); } else { var timerStack = new Stack<long>(); timerStack.Push(DateTime.UtcNow.Ticks); ProfilePool.Add(currentThreadId, timerStack); } } }
///<summary> /// Stops timer and calculate the execution time. ///</summary> ///<returns>Execution time in milli seconds</returns> publicstaticintStop() { lock (SyncLock) { long currentTicks = DateTime.UtcNow.Ticks; int currentThreadId = Thread.CurrentThread.ManagedThreadId;
if (ProfilePool.ContainsKey(currentThreadId)) { long ticks = ProfilePool[currentThreadId].Pop(); if (ProfilePool[currentThreadId].Count == 0) { ProfilePool.Remove(currentThreadId); }
var timeSpan = new TimeSpan(currentTicks - ticks);
return (int)timeSpan.TotalMilliseconds; } }
return0; } }
which stores the starting tick and calculate the time taken to execute when Stop is called.
Next step is to use this and see whether it is logged properly. In order enable profiling for a method, you just needs to decorate it with the ProfileMethod attribute. Like below
What happens if your client complains that your application is running very slow!!! or in your load/stress testing you found that some functionalities are very slow in executing than expected. This is the time where you go for profiling the execution, to analyse the root cause of these issues.
So how we could develop a profiler, where we don’t have to wrap our normal code in a profiling code.
With the help of AOP (Aspect-Oriented Programming) we could do the profiling task without interrupting the actual code.
AOP is a programming paradigm in which secondary or supporting functions are isolated from the main program’s business logic
So in order bring the AOP functionality into this application, I am going to use a third party library PostSharp which I believe this is one of the best that is available in the market.
So, now we have got the basic things to start with and now let’s start coding….
Start a new solution in visual studio and add a new console application project to it. Then add the below references to the newly created project
Add reference to the Log4Net.dll, PostSharp.Laos.dll and PostSharp.Public.dll (Please read https://www.postsharp.net/documentation to get the basic installation procedure). Next, create a new attribute class called ProfileMethodAttribute – this class is responsible for doing the profiling work. Make sure that you have decorated this class with Serializable attribute
This class actually derives from OnMethodBoundaryAspect, it has got two methods OnEntry and OnExit which we needed. These method will be called before the start of a method execution and at the end of method execution respectively, when this attribute is decorated against a method.
When a call comes to OnEntry method, we will first log the execution call using the LoggerHelper, then start a clock using another helper class Profiler
///<summary> /// Helper class that wraps the timer based functionalities. ///</summary> internalstaticclassProfiler { ///<summary> /// Lock object. ///</summary> privatestaticreadonlyobject SyncLock = newobject();
///<summary> /// Variable that tracks the time. ///</summary> privatestaticreadonly Dictionary<int, Stack<long>> ProfilePool;
///<summary> /// Initializes static members of the <see cref="Profiler"/> class. ///</summary> staticProfiler() { ProfilePool = new Dictionary<int, Stack<long>>(); }
///<summary> /// Starts this timer. ///</summary> publicstaticvoidStart() { lock (SyncLock) { int currentThreadId = Thread.CurrentThread.ManagedThreadId; if (ProfilePool.ContainsKey(currentThreadId)) { ProfilePool[currentThreadId].Push( Environment.TickCount ); } else { var timerStack = new Stack<long>(); timerStack.Push(DateTime.UtcNow.Ticks); ProfilePool.Add(currentThreadId, timerStack); } } }
///<summary> /// Stops timer and calculate the execution time. ///</summary> ///<returns>Execution time in milli seconds</returns> publicstaticintStop() { lock (SyncLock) { long currentTicks = DateTime.UtcNow.Ticks; int currentThreadId = Thread.CurrentThread.ManagedThreadId;
if (ProfilePool.ContainsKey(currentThreadId)) { long ticks = ProfilePool[currentThreadId].Pop(); if (ProfilePool[currentThreadId].Count == 0) { ProfilePool.Remove(currentThreadId); }
var timeSpan = new TimeSpan(currentTicks - ticks);
return (int)timeSpan.TotalMilliseconds; } }
return0; } }
which stores the starting tick and calculate the time taken to execute when Stop is called.
Next step is to use this and see whether it is logged properly. In order enable profiling for a method, you just needs to decorate it with the ProfileMethod attribute. Like below
This is an update to my previous post regarding conditional rendering partial views, in that I used the internal implementation of the Html.RenderPartail(…) method to create the Html extension. Later I found a simple way to achieve the same using Action<T> delegate
<% Html.PartialIf(this.Model.Exists, html => html.RenderPartial("MyPartialView")); %>
If you look at the PartialIf implementation, it is simple, cleaner than the previous technique I have mentioned in my post.
This is an update to my previous post regarding conditional rendering partial views, in that I used the internal implementation of the Html.RenderPartail(…) method to create the Html extension. Later I found a simple way to achieve the same using Action<T> delegate
<% Html.PartialIf(this.Model.Exists, html => html.RenderPartial("MyPartialView")); %>
If you look at the PartialIf implementation, it is simple, cleaner than the previous technique I have mentioned in my post.
I am not sure about any other technique for rendering partial view conditionally other than this (correct me if I am wrong :) ).
Then I thought about copying the pattern I have used in my previous post and came up with this code which could conditionally render partial views and you could use the Html extension like below, which more clean than the previous
And here is the helper method I have used in the about extension ( most of the code snippet is taken from the MVC method Html.Renderpartial(…), thanks to open source )
I am not sure about any other technique for rendering partial view conditionally other than this (correct me if I am wrong :) ).
Then I thought about copying the pattern I have used in my previous post and came up with this code which could conditionally render partial views and you could use the Html extension like below, which more clean than the previous
And here is the helper method I have used in the about extension ( most of the code snippet is taken from the MVC method Html.Renderpartial(…), thanks to open source )
We come across situations like rendering elements based on the conditions in Model. For example, in the view if we want to show a TextBox if some property is true. A normal way of doing this is like below
This looks like old classic asp style, and when it comes to code maintenance this will be a pain. So a clean way is to use an Html helper to generate this
Ok, now we got a conditionally rendered element, sometimes we may have to put a wrapper around this element. So I have written another Html helper method which will help you to put any html element around a particular element.
We come across situations like rendering elements based on the conditions in Model. For example, in the view if we want to show a TextBox if some property is true. A normal way of doing this is like below
This looks like old classic asp style, and when it comes to code maintenance this will be a pain. So a clean way is to use an Html helper to generate this
Ok, now we got a conditionally rendered element, sometimes we may have to put a wrapper around this element. So I have written another Html helper method which will help you to put any html element around a particular element.
I got this exception when I tried to run my Repository tests and couldn’t find the exact reason why these tests failed.
All the unit tests where passing before I removed the default constructors from Domain Model, when I reverted the default constructors back, everything started working again….. !!!
Do you know the exact reason for this?
Update: In order to support lazy loading, NHibernate needs to create proxy objects. Those proxy classes derived from the actual domain model class itself, and they are instantiated using the default constructor. So the parent class also should have a default constructor.