tag:blogger.com,1999:blog-28003900076104389542024-03-28T15:15:31.069+05:30ASP.NET, ASP.NET MVC, Core .NET, and SQL ServerNirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.comBlogger85125tag:blogger.com,1999:blog-2800390007610438954.post-57039660844293479722017-12-22T12:20:00.002+05:302017-12-22T12:20:46.050+05:30ApplicationPoolIdentity >> IIS 7 and Application Pool Shutting down behavior with Error 503 followed by HTTP Error 401.3<div dir="ltr" style="text-align: left;" trbidi="on">
Recently while deploying one of our website to IIS of a new production server (Windows 2012 Server), I was facing a weird issue. Whenever a user tries to launch the website, the application pool was getting crashed with Error - 503. <div>
<br /></div>
<div>
The application pool was running under "ApplicationPoolIdentity" account.<br /><div>
<br /></div>
<div>
After investigating the event logs, I found the root cause of the issue as below: </div>
<div>
<br /></div>
<blockquote class="tr_bq">
Windows cannot copy file \\?\C:\Users\Default\AppData\Local\Microsoft\VSCommon\14.0\SQM\sqmdata00.sqm to location \\?\C:\Users\XXXXX.IIS APPPOOL.008\AppData\Local\Microsoft\VSCommon\14.0\SQM\sqmdata00.sqm. This error may be caused by network problems or insufficient security rights.<br /> DETAIL - Access is denied.</blockquote>
<div>
The issue was related to security which is quite apparent from the error message, but it was something new for me, especially after successfully deploying more than 100 ASP.NET websites.</div>
<div>
<br /></div>
<div>
After a lot of research, I found following had to be done to resolve this issue -</div>
</div>
<div>
<br /></div>
<div>
1. Open the properties of "Default" user (i.e., "C:\Users\Default". Location may defer based on the installation).</div>
<div>
2. Under Security, click "Advanced" followed to that click "Change Permissions". This should open "Advanced Security Settings for Default" dialog box.</div>
<div>
3. Tick "Replace all child object permissions with inheritable permissions for this object" check box.</div>
<div>
4. Click Apply and OK.</div>
<div>
<br /></div>
<div>
Now restart the application pool, and try accessing the website. This should fix the issue.</div>
<div>
<br /></div>
<div>
However, for me, it didn't end there. This resolved the 503 error, I was able to see the website getting loaded in the browser, and application pool was not shutting down which was perfect solution. But now, it was not loading any unmanaged resources (i.e., css, js, images, etc). </div>
<div>
Further more investigations suggested that all these resources were failing to load due to "401.3 Unauthorized" error.</div>
<div>
<br /></div>
<div>
After another round of research and googling, I found the root cause which was even more strange.</div>
<div>
<br /></div>
<div>
1. Open "Authentication" settings of the website under IIS.</div>
<div>
2. Select "Anonymous Authentication", and click "Edit" button.</div>
<div>
3. Check which option is selected under "Anonymous user identity". If it is "Specific user", then congratulations, your problem will be resolved in a minute.</div>
<div>
4. Change the option to "Application pool identity", and click "OK"</div>
<div>
<br /></div>
<div>
Restart Application Pool, and relaunch the website.</div>
<div>
<br /></div>
<div>
This time, the website should load properly and all expected resources.</div>
<div>
<br /></div>
<div>
Hope this helps</div>
</div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com9tag:blogger.com,1999:blog-2800390007610438954.post-24085216381241009252017-03-07T15:53:00.000+05:302017-03-07T15:58:11.323+05:30Dynamics CRM 2016: Updating Lookup field value using CRM REST API<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "arial" , "helvetica" , sans-serif;">This article aims to guide you about the things to consider when making a REST API call to insert/ update a value in the lookup field in CRM.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Consider your CRM instance is having following two entities:</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">1) account >> This entity comes default with CRM instance</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;">2) new_customer >> Custom entity having various fields, but we are typically interested in "new_account" which is a lookup to the account entity.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"> <span style="color: blue;">public class Account</span></span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> {</span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> public string name { get; set; }</span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> public Guid accountid { get; set; }</span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"></span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> }</span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;">using Newtonsoft.Json;</span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;">public class Customer</span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> {</span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> [JsonProperty(PropertyName = "new_accountid@odata.bind")]</span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> public string new_accountid { get; set; }</span><span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> </span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> public string new_customerRefNo { get; set; }</span><span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> </span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"> }</span><br />
<span style="color: blue; font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Notice, the JsonProperty in Customer class. This property needs to be set for all lookup fields in case if they are intended to be updated/ inserted using REST API. Name of this property should be set to - "<u><i>[lookupfieldname]</i>@odata.bind</u>". In above case, [lookupfieldname] is "new_accountid"</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Now you can populate values in Customer instance, and can send them in your REST API calls which will update lookup fields during PUT/ POST calls.</span></div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com1tag:blogger.com,1999:blog-2800390007610438954.post-85568687324816393382015-11-06T16:34:00.002+05:302015-11-06T16:34:56.286+05:30ASP.NET MVC - Customizing RequiredAttribute to display required field validation errors in a different format than the default one<div dir="ltr" style="text-align: left;" trbidi="on">
Consider a scenario where you want all Required validators of your MVC application to display error messages in a different format than the default one.<br />
As of ASP.NET MVC 5, the default error message format for required field validator is "The <<field display name>> field is required." For example, "The User name field is required." or "The Password field is required."<br />
<br />
But instead, you want them to display in a different format, like - "Please enter value in <<field display name>>." Examples - "Please enter value in User name input box", or "Please enter value in Password input box".<br />
<br />
One option is to set "ErrorMessage" property of Required data annotation for each and every fields where this validator is used. However, this can solve the purpose, but it will be tedious and importantly, maintenance nightmare to do so.<br />
<br />
Perform following steps to customize default error message of Required validators, and apply them across the application.<br />
<br />
<b><u>Step - 1 Create following custom attribute (for instance, "CustomizedRequiredValidatorAdapter") inheriting from RequiredAttributeAdapter:</u></b><br />
<b><u><br /></u></b>
<span style="color: blue;"> public class CustomizedRequiredValidatorAdapter: RequiredAttributeAdapter</span><br />
<span style="color: blue;"> {</span><br />
<span style="color: blue;"> public </span><span style="color: blue;">CustomizedRequiredValidatorAdapter</span><span style="color: blue;">(ModelMetadata metadata,</span><br />
<span style="color: blue;"> ControllerContext context,</span><br />
<span style="color: blue;"> RequiredAttribute attribute)</span><br />
<span style="color: blue;"> : base(metadata, context, attribute)</span><br />
<span style="color: blue;"> {</span><br />
<span style="color: blue;"> attribute.ErrorMessage = "Please enter value in {0}.";</span><br />
<span style="color: blue;"> }</span><br />
<span style="color: blue;"> }</span><br />
<br />
This requires "System.Web.Mvc", and "System.ComponentModel.DataAnnotations" namespaces<br />
<span style="color: blue;"><br /></span>
<b><u>Step - 2 Replace default RequiredAttribute validator by the one created in Step - 1</u></b><br />
<br />
Add following in the Application_Start event of Global.asax.cs file:<br />
<br />
<span style="color: blue;">DataAnnotationsModelValidatorProvider.RegisterAdapter(</span><br />
<span style="color: blue;"> typeof(RequiredAttribute),</span><br />
<span style="color: blue;"> typeof(CustomizedRequiredValidatorAdapter));</span><br />
<span style="color: blue;"><br /></span>
<b>That's it! This is all you need to do. Now try running your MVC application and see the error messages on required validation failures and you should see the customized messages.</b><br />
<b><br /></b>
Further, if you have Globalization implemented, you can set attribute.ErrorMessageResourceType, and attribute.ErrorMessageResourceName in the custom attribute (step - 1) to get the string Error message string from resources file.</div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com8tag:blogger.com,1999:blog-2800390007610438954.post-29632158784022742282015-11-06T14:47:00.001+05:302015-11-06T14:47:33.094+05:30ASP.NET MVC >> Using enumerations in AuthorizeAttribute to allow the actions to be invoked by multiple roles<div dir="ltr" style="text-align: left;" trbidi="on">
Consider a scenario where you need all/ some of the actions of a controller to be invoked by users having specific roles. For example, a "AdminController" which can be accessed only be users having "Admin" or "SuperUser" roles.<br />
<br />
Now, its easy to pass multiple roles as a string in Authorize attribute as below:<br />
<span style="color: blue;">[Authorize(Roles="Roles,SuperUser")]</span><br />
<span style="color: blue;"><br /></span>
However, this is an absolute maintenance nightmare, because if any of the roles are renamed you will need to remember to update at multiple places.<br />
<br />
Imagine creating enum as below<br />
<span style="color: blue;"><br /></span>
<span style="color: blue;"> public enum Roles</span><br />
<span style="color: blue;"> {</span><br />
<span style="color: blue;"> User,</span><br />
<span style="color: blue;"> Admin,</span><br />
<span style="color: blue;"> SuperUser</span><br />
<span style="color: blue;"> }</span><br />
<br />
But as the Roles parameter in Authorize attribute is of string type, it wont allow you to pass multiple enum values there.<br />
<br />
In order to fix this issue, we need to to create a new attribute inheriting from MVC's AuthorizeAttribute:<br />
<br />
<span style="color: blue;"> [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]</span><br />
<span style="color: blue;"> public class AuthorizeEnum: AuthorizeAttribute</span><br />
<span style="color: blue;"> {</span><br />
<span style="color: blue;"> public AuthorizeEnum(params object[] roles)</span><br />
<span style="color: blue;"> {</span><br />
<span style="color: blue;"> if (roles.Any(r => r.GetType().BaseType != typeof(Enum)))</span><br />
<span style="color: blue;"> throw new ArgumentException("roles");</span><br />
<span style="color: blue;"><br /></span>
<span style="color: blue;"> this.Roles = string.Join(",", roles.Select(r => Enum.GetName(r.GetType(), r)));</span><br />
<span style="color: blue;"> }</span><br />
<span style="color: blue;"> }</span><br />
<span style="color: blue;"><br /></span>
Now, this attribute can be used to decorate the controllers and/ or actions to pass multiple roles as enum values instead of strings.<br />
<br />
<span style="color: blue;">[AuthorizeEnum(Roles.User, Roles.Admin)]</span><br />
<span style="color: blue;">public partial class AdminController </span><br />
<span style="color: blue;">{</span><br />
<span style="color: blue;">..........</span><br />
<span style="color: blue;">}</span><br />
<br /></div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-91389976957757849342015-08-26T11:26:00.002+05:302015-08-26T11:26:56.511+05:30CSS Tips and Tricks: Make an image appearing as disabled.<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
At times, in your HTML pages, you would want user to display images as disabled.<br />
For example, consider a Delete button in an HTML table where for a few rows you cannot allow delete operations (based on some criteria). In such cases, it would be a good (and self-explanatory) to display Delete image as disabled (instead of hiding the same). Following is the CSS for the same:<br />
<br />
<span style="color: blue;">.disabledImage {</span><br />
<span style="color: blue;"> opacity: 0.4;</span><br />
<span style="color: blue;"> filter: alpha(opacity=30); /* msie */</span><br />
<span style="color: blue;">}</span><br />
<span style="color: blue;"><br /></span>
Example usage:<br />
<br />
<span style="color: blue;"><img src="../content/images/delete.png" title="cannot delete this entry due to dependency" class="disabledImage"/></span></div>
<br />
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a></div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-2170617773998738022015-08-13T12:03:00.001+05:302015-08-13T15:02:17.464+05:30Strategy Design Pattern - Explained with a real world example (C#)<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Strategy Pattern is a Behavioral pattern.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In theory, the Strategy Pattern means - </span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><i>A family of algorithms, encapsulates each one, and make them interchangeable.</i></span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><i>Strategy lets the algorithm vary independently from the clients that use it</i>.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">How do we elaborate it? or what can be a practical example of this?</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Let us consider, we are creating a basic simulator for the mobile handset. A user (client application) selects a handset, and should be able to function whatever is supported in the handset.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKjCL7HAuKIFJWKcawA7MjCxG7foesdkDduw3wXgEIzIBJOk7R9lcp6xpyjEZ_x13g9oOoOqrn9MB1q4-lQSPCzI9CGg8Ob3yG1MTV1mJ3gRfE8UpPM9LmfhYWvwTlgHc1yqU8rgvy0_8/s1600/PhoneSimulator.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKjCL7HAuKIFJWKcawA7MjCxG7foesdkDduw3wXgEIzIBJOk7R9lcp6xpyjEZ_x13g9oOoOqrn9MB1q4-lQSPCzI9CGg8Ob3yG1MTV1mJ3gRfE8UpPM9LmfhYWvwTlgHc1yqU8rgvy0_8/s1600/PhoneSimulator.PNG" /></a><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">As we all know there can be different types of mobile devices. Old (and cheapest) devices just support Call and SMS functionalities, and there is a range of smart phones/ new handsets which supports cool hardware and features like, rear camera (i.e., main camera), or front camera (i.e., the camera to take your selfie!). There are number of features in the smartphones (much more than camera), but to make the article a bit simple, we will only focus on following four features:</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">- Phone Call</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">- SMS</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">- Front Camera</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">- Rear Camera</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">We are taking example of two different handsets:</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">1. Nokia 5510 - having only basic features (i.e., phone call and SMS)</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">2. Nokia Lumia 920 - having tons of cool features (but for our case, its only Front Camera and Rear Camera that matters!)</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">3. Nokia 6600 - having only rear camera (sorry, but there was no facility to take a selfie with this 10 year old handsets!)</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">In Object-oriented world these three classes are called as "concrete implementations". So in future we should be able to add a number of handset, and so a concrete class for each of them.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b><u>Step 1 </u></b></span><b style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><u>Identify the behavior(s) which are common across all the clients.</u></b><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This means, there is a need of an abstract class containing the behavior which is common across all devices. This will be a base class for all the handsets.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Remember, this abstract class is the heart of Strategy pattern. Means, it will contain (and capable to plug-in) everything to create different handsets and attach various behaviors to them. This class is referred to as "Strategy".</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public abstract class Handset</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public abstract string Display();</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public string MakeACall(string destinationNumber)</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return String.Format("Calling {0}", destinationNumber);</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public string SendSms(string destinationNumber)</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return String.Format("Sending SMS to {0}", destinationNumber);</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><span style="color: blue;"> }</span></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><i>Display()</i> is an abstract method because all different handsets will have their own implementation of how they should appear.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><i>MakeACall()</i> and <i>SendSms()</i> are concrete methods because all devices should make a call or send an SMS in the same manner.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<b style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><u>Step</u></b><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b><u> 2 Do something for what varies!</u></b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In our example, we have Camera features which are varying in different devices. That means, a few handsets supports both front and rear cameras, whereas a few supports only rear camera, and there are those old phones which doesn't have a camera at all.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Lets create an interface which any type of camera can inherit from:</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public interface IPhotographyDevice</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> string TakeSnap(string location);</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> string RecordVideo(string location);</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"></span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And, then the two classes implementing this interface, each class for each type of camera (i.e. rear camera, and front camera)</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">A class implementing the interface for RearCamera:</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public class RearCamera : IPhotographyDevice</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public int megaPixels { get; set; }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public </span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">RearCamera</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">(int supportedMegaPixels)</span></span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> megaPixels = supportedMegaPixels;</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public string TakeSnap(string location)</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return String.Format("Taking Photograph on {0} MP main camera", megaPixels.ToString());</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public string RecordVideo(string location)</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return String.Format("Recording Video on {0} MP main camera", megaPixels.ToString());</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"></span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">So you can see that we have encapsulated the photography features in their concrete implementations (i.e., FrontCamera and RearCamera).</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<b style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><u>Step</u></b><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b><u> 3 Integrating the behavior(s) which vary</u></b>.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">The abstract class should now delegate the photography related behavior to <i>IPhotography</i> interface.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">For that, we need to inform the <i>Handset</i> class about <i>IPhotography</i> interface, so all the concrete classes of handsets inheriting from <i>Handset</i> class c</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">an use photography features,if they need to support them. (i.e., taking photos, and record videos through Front and/ or Rear cameras).</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><i><span style="color: #741b47;">Handset class revisited to add an instance variable</span></i></span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public abstract class Handset</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">........</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public IPhotographyDevice photographyDevice;</span><br />
<span style="color: blue;"><br /></span>
<br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><span style="color: blue;"><br /></span></span>
<b style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><u>Step 4 Implement the methods for the new behavior</u></b><br />
<span style="color: #4c1130; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><i>Handset class revisited to add TakeSnap and RecordVideo methods which calls relevant methods from concrete photography classes, if supported by the handset.</i></span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public abstract class Handset</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><span style="color: blue;">....</span></span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><span style="color: blue;"> public string TakeSnap(string location)</span></span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> if (photographyDevice != null)</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return photographyDevice.TakeSnap(location);</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> else</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return @"Sorry! this phone doesn't support photography";</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public string RecordVideo(string location)</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> if (photographyDevice != null)</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return photographyDevice.RecordVideo(location);</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> else</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return @"Sorry! this phone doesn't support videography";</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<b style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><u>Step 5 Setting behavior dynamically</u></b><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Our application allow users to change the camera run-time. Notice the "Activate Front Camera" and "Activate Rear Camera" buttons in the screenshot.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Here is the beauty of Strategy pattern wherein you can change the behavior dynamically (run-time polymorphism, as they say!)</span><br />
<br />
<span style="color: #4c1130; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><i>Handset class revisited to add a setter method (i.e. a method that sets an instance variable - in our case, it is "photographyDevice"</i></span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">public abstract class Handset</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.......</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public void SetPhotographyDevice(IPhotographyDevice newPhotographyDevice)</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> photographyDevice = newPhotographyDevice;</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">........</span><br />
<span style="color: blue; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><b><u>Step 6 Implement concrete classes from the Strategy</u></b></span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Now we have our Strategy (i.e. <i>Handset</i> abstract class) is ready, its time to create concrete classes.</span><br />
<br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">public class Nokia5510: Handset</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public override string Display()</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return "This is a very basic model from Nokia with no camera whatsoever!!";</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This particular device doesn't support any sort of camera, and so we don't need to set the instance of "IPhotography" variable.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">when your application will try to execute any photography related methods (i.e. TakeSnap or RecordVideo) then it will say, the phone doesn't support these features)</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">public class NokiaLumia920 : Handset</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> private readonly int rearCameraMPs = 12;</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> private readonly int frontCameraMPs = 5; </span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public NokiaLumia920()</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> photographyDevice = new MainCamera(mainCameraMPs);</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public void LoadFrontCamera()</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> SetPhotographyDevice(new FrontCamera(frontCameraMPs));</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public void LoadRearCamera()</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> SetPhotographyDevice(new RearCamera(</span><span style="color: blue; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">rear</span><span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">CameraMPs));</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> </span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public override string Display()</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return "Nokia Lumia 920... A full-featured phone from Nokia";</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="color: blue;"> }</span> </span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">This device supports both front and rear cameras, and the client application can switch the cameras at run-time, by calling "LoadFrontCamera" and "LoadRearCamera" methods which internally calls a setter property to change the behavior. That means, we initially attach the behavior (through constructor), and then change it.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">public class Nokia6600 : Handset</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> private readonly int </span><span style="color: blue; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">rear</span><span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">CameraMPs = 12;</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public Nokia6600()</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> photographyDevice = new RearCamera(mainCameraMPs);</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public void LoadFrontCamera()</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> //Front-camera are not supported in this device</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> SetPhotographyDevice(null);</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public void LoadRearCamera()</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> SetPhotographyDevice(new RearCamera(mainCameraMPs));</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> public override string Display()</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> return "Nokia Lumia 6600... A Nokia phone with a lots of features and a high-resolution camera";</span><br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<br />
<span style="color: blue; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">This device supports only rear camera, and so when your application tries to attach front camera, the instance is assigned to a null value. A client application can again start the rear camera by invoking "LoadRearCamera".</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><b><u>Step 7 Code the simulator</u></b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">By now we have the "Strategy" (i.e. our </span><i style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Handset</i><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> abstract class), interfaces and their concrete implementations, and concrete handset classes are ready. Now its just a matter of instantiate the handset classes (based on the selected handset in drop-down) and invoking methods in various events of simulator.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><b><u><br /></u></b></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><b>Call me lazy :) but I am leaving this to the readers to implement this simulator. Feel free to comment in case if you face any difficulties.</b></span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><b><u>Summary</u></b></span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">To summarize and conclude, we have used "Strategy design pattern" in our Mobile Handset simulator application, and following are the benefits:</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">- Behaviors can be added run-time</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">- Behaviors can be switched run-time</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">- Any new implementation of a behavior will not require any change in the "Strategy" class.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">- There is a single class (called as Strategy) that contains everything which is required by the clients. (abstract methods, concrete methods, instance variables of Interface type for the behavior and setter methods to change the behavior at run-time)</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">- If there is a need to supporting any new device in the simulator, and if that device has possible features within call, sms, and camera, then its just a matter of adding a new concrete class for the handset as we have done.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">- If the new handsets are supporting a different type of camera (may be, a "spy camera"), then you will just need to create a new concrete type from "IPhotographyDevice", and you should be able to use the "Spy camera" in your simulator straight-away. And similarly, can switch between - front, rear and spy cameras similar to what we did in our example. </span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">- if the new handsets are supporting some new behaviors, then a new interface for the behavior will need to be added, and so its concrete application (as per step 2). After that the "Strategy" class will need to be integrate that behavior (steps - 3,4,5). And phew, you are ready to use this new behavior.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Hope this article helped you in understanding Strategy pattern. Feel free to ask any questions, or provide feedback!</span></div>
<br/>
<a href="http://www.codeproject.com" rel="tag" style="display:none">CodeProject</a>Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com1tag:blogger.com,1999:blog-2800390007610438954.post-88546509752658802452015-06-17T12:01:00.000+05:302015-08-17T23:08:17.597+05:30Minify CSS and JS files through PowerShell scripts<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif;">The below scripts can be used in scenarios where you need to minify CSS and JavaScript files of your website using PowerShell Scripts. (For example, during Post-build PowerShell scripts in TFS build).</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<b><u><span style="font-family: Verdana, sans-serif;">Pre-requisite:</span></u></b><br />
<span style="font-family: Verdana, sans-serif;">Microsoft Ajax Minifier should be installed on the machine (or TFS build server) where the PowerShell scripts would be executing.</span><br />
<span style="font-family: Verdana, sans-serif;">Ajax Minifier can be downloaded from <a href="http://ajaxmin.codeplex.com/" target="_blank">here</a>.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<b><u><span style="font-family: Verdana, sans-serif;">PowerShell scripts to minify CSS files in a directory:</span></u></b><br />
<b><u><span style="font-family: Verdana, sans-serif;"><br /></span></u></b>
<span style="color: blue; font-family: Georgia, Times New Roman, serif;">function applyCssMinification($dir)</span><br />
<span style="color: blue; font-family: Georgia, Times New Roman, serif;">{</span><br />
<span style="color: blue; font-family: Georgia, Times New Roman, serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>$Minifier = “C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\AjaxMin.exe”</span><br />
<span style="color: blue; font-family: Georgia, Times New Roman, serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>get-childitem $dir -recurse -force -include *.css -exclude *.min.css | foreach-object {&$Minifier $_.FullName -out $_.FullName -clobber}</span><br />
<br />
<span style="color: blue; font-family: Georgia, Times New Roman, serif;">}</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<b><u><span style="font-family: Verdana, sans-serif;">PowerShell scripts to minify JavaScript files in a directory:</span></u></b><br />
<b><u><span style="font-family: Verdana, sans-serif;"><br /></span></u></b>
<span style="color: blue; font-family: Georgia, Times New Roman, serif;">function applyJsMinification($dir)</span><br />
<span style="color: blue; font-family: Georgia, Times New Roman, serif;">{</span><br />
<span style="color: blue; font-family: Georgia, Times New Roman, serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>$Minifier = “C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\AjaxMin.exe”</span><br />
<span style="color: blue; font-family: Georgia, Times New Roman, serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>get-childitem $dir -recurse -force -include *.js -exclude *.min.js | foreach-object {&$Minifier $_.FullName -out $_.FullName -clobber}</span><br />
<span style="color: blue; font-family: Georgia, Times New Roman, serif;">}</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Once defined call these functions by providing CSS and JS directory path as a parameter:</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">For example,</span><br />
<br />
<span style="color: blue; font-family: Verdana, sans-serif;">applyCssMinification "$Env:TF_BUILD_SOURCESDIRECTORY\Website\Content\CSS"</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><br /></span>
<span style="color: blue; font-family: Verdana, sans-serif;"><br /></span>
<span style="color: blue; font-family: Verdana, sans-serif;">applyJsMinification </span><span style="color: blue; font-family: Verdana, sans-serif;">"$Env:TF_BUILD_SOURCESDIRECTORY</span><span style="color: blue; font-family: Verdana, sans-serif;">\Website\Content\Scripts"</span></div>
<a href="http://www.codeproject.com" rel="tag" style="display:none">CodeProject</a>Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-63409072568943690602015-06-03T15:01:00.001+05:302015-08-17T23:09:42.169+05:30PowerShell scripts to apply configuration transformations for App.Config or Web.Config files<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif;">By default, Visual Studio provides configuration transformation for Web.config file. As well as, App.Config files can be transformed using a "SlowCheetah" or similar add-ons available in Visual Studio Gallary.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">But there may be the cases where the configuration transformation is not supported by project template in Visual Studio, or in case, during TFS build, if you would want to create configuration transformation files for all of the release configurations, and not particular to a single release configuration. </span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Through this, the same deployment package created during TFS build can be deployed on different environments.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Following is the PowerShell function I have created to achieve this -</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="color: blue; font-family: Verdana, sans-serif;">#Apply config transformation</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;">function applyConfigTransformation($src,$xdt,$dst)</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;">{</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Add-Type -Path "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.XmlTransform.dll"</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><br /></span>
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>try </span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Write-Host 'applyConfigTransformation - Called'</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Write-Host $src</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>$doc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>$doc.PreserveWhiteSpace = $true</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Write-Host 'applyConfigTransformation - Load Called'</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>$doc.Load($src)</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Write-Host 'applyConfigTransformation - Load completed'</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><br /></span>
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>$trn = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdt)</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><br /></span>
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>if ($trn.Apply($doc))</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Write-Host 'applyConfigTransformation - $trn.Apply called'</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>$doc.Save($dst)</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Write-Output "Output file: $dst"</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Write-Host 'applyConfigTransformation - $trn.Apply completed'</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>else</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>throw "Transformation terminated with status False"</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>}</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>catch</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>{</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Write-Output $Error[0].Exception</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><span class="Apple-tab-span" style="white-space: pre;"> </span>} </span><br />
<span style="color: blue; font-family: Verdana, sans-serif;">}</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Following is how this function can be called -</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="color: blue; font-family: Verdana, sans-serif;">$src = "C:Projects\MyWebApp\web.config"</span><br />
<span style="color: blue;"><span style="font-family: Verdana, sans-serif;">$xdt = "</span><span style="font-family: Verdana, sans-serif;">C:Projects</span><span style="font-family: Verdana, sans-serif;">\</span><span style="font-family: Verdana, sans-serif;">MyWebApp\Configs\</span><span style="font-family: Verdana, sans-serif;">web.PreProduction.config"</span></span><br />
<span style="color: blue;"><span style="font-family: Verdana, sans-serif;">$dst = </span><span style="font-family: Verdana, sans-serif;">"</span><span style="font-family: Verdana, sans-serif;">C:Projects</span><span style="font-family: Verdana, sans-serif;">\</span><span style="font-family: Verdana, sans-serif;">MyWebApp\</span><span style="font-family: Verdana, sans-serif;">web.PreProduction.config"</span></span><br />
<span style="color: blue; font-family: Verdana, sans-serif;">applyConfigTransformation $src $xdt $dst</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<a href="http://www.codeproject.com" rel="tag" style="display:none">CodeProject</a>Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-29229807699023245562015-04-24T12:39:00.000+05:302015-08-17T23:10:02.315+05:30Powershell Scripts to replace "setvar" variable in SQL-CMD script file before running the SQL scripts<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif;">Consider having a following SQL file, to run it an SQL-CMD mode:</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span><span style="font-family: Verdana, sans-serif;">C:\PowershellTest\CreateDatabase.sql</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="color: blue;"><span style="font-family: Verdana, sans-serif;">:setvar ImagesLocation '</span><span style="font-family: Verdana, sans-serif;">C:\ImagesStore\</span><span style="font-family: Verdana, sans-serif;">'</span></span><br />
<span style="font-family: Verdana, sans-serif;"><span style="color: blue;">........</span></span><br />
<span style="font-family: Verdana, sans-serif;"><span style="color: blue;">Update ImagesStore</span></span><br />
<span style="color: blue;"><span style="font-family: Verdana, sans-serif;">SET ImagesLocation = '$(</span><span style="font-family: Verdana, sans-serif;">ImagesLocation</span><span style="font-family: Verdana, sans-serif;">)'</span></span><br />
<span style="font-family: Verdana, sans-serif;"><span style="color: blue;">.......</span></span><br />
<br />
<div>
<span style="color: blue; font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Now, when you run this sql-cmd scripts file during the deployments, you would also want to change the value of ImagesLocation variable, as the location may vary for different environment.</span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">This can be achieved through using regular expressions in Powershell scripts.</span><br />
<span style="font-family: Verdana, sans-serif;">In order to do that, you can create a following function in your Powershell deployment or pre-deployment scripts:</span></div>
<div>
<br /></div>
<div>
<span style="color: #274e13;">#function to replace cmdlet variable in SQL-CMD scripts (i.e., the ones assigned by :setvar). For strange reasons, command line variable assignments has lower precendence than the Sqlcmd scripts setvar. </span><br />
<span style="color: #274e13;">function replaceCmdletParameterValueInFile( $file, $key, $value ) {</span><br />
<span style="color: #274e13;"> $content = Get-Content $file</span><br />
<span style="color: #274e13;"> if ( $content -match ":setvar\s*$key\s*[\',\""][\w\d\.\:\\\-]*[\'\""_]" ) {</span><br />
<span style="color: #274e13;"> $content -replace ":setvar\s*$key\s*[\',\""][\w\d\.\:\\\-]*[\'\""_]", ":setvar $key $value" |</span><br />
<span style="color: #274e13;"> Set-Content $file </span><br />
<span style="color: #274e13;"> } else {</span><br />
<span style="color: #274e13;"> Add-Content $file "$key = $value"</span><br />
<span style="color: #274e13;"> }</span><br />
<span style="color: #274e13;">}</span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Call this function in a following manner:</span></div>
<div>
<span style="color: #741b47;"><br /></span></div>
<div>
<span style="color: #4c1130; font-family: inherit;">$scriptfile = "C:\PowershellTest\UpdateImagesLocation.sql"</span></div>
<div>
<span style="color: #4c1130; font-family: inherit;">replacePatternMatchingValueInFile $scriptfile"SET @ImagesLocation" "'\\datashare\appImages'"</span><br />
<span style="color: #4c1130; font-family: inherit;">replaceCmdParameterValueInFile "C:\PowershellTest\CreateDatabase.sql" "ImagesLocation" "'\\datashare\ImagesStore'"</span></div>
<div>
<span style="color: #4c1130; font-family: inherit;"><br /></span></div>
<br />
<div>
</div>
<br />
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: 'Times New Roman'; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;">
<div style="margin: 0px;">
<span style="font-family: Verdana, sans-serif;">As a result, the variable assignment for ImagesLocation would be changed to a different value in the sql file, and then that file can be used in Invoke-Sqlcmd to run it.</span></div>
</div>
</div>
<a href="http://www.codeproject.com" rel="tag" style="display:none">CodeProject</a>Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-65436515752133440132015-04-24T12:27:00.000+05:302015-08-17T23:12:57.198+05:30Powershell Scripts to replace Key value pair in SQL script file before running the SQL scripts<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif;">Consider having a following SQL file:</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">C:\PowershellTest\UpdateImagesLocation.sql</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="color: blue; font-family: Verdana, sans-serif;">DECLARE @ImagesLocation NVARCHAR(max)</span><br />
<span style="color: blue; font-family: Verdana, sans-serif;">SET @ImagesLocation = 'C:\ImagesStore\'</span><br />
<div>
<span style="color: blue;">.....</span></div>
<div>
<span style="color: blue; font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Now, when you run this sql scripts file during the deployments, you would also want to change the value of @ImagesLocation, as the location may vary for different environment.</span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">This can be achieved through using regular expressions in Powershell scripts.</span><br />
<span style="font-family: Verdana, sans-serif;">In order to do that, you can create a following function in your Powershell deployment or pre-deployment scripts:</span></div>
<div>
<br /></div>
<div>
<div>
<span style="color: #274e13;">#if an SQL file contains 'SET @variable_name=value', then this function can be called to replace value by actual value.</span></div>
<div>
<span style="color: #741b47;">function replacePatternMatchingValueInFile( $file, $key, $value ) {</span></div>
<div>
<span style="color: #741b47;"> $content = Get-Content $file</span></div>
<div>
<span style="color: #741b47;"> if ( $content -match "^$key\s*=" ) {</span></div>
<div>
<span style="color: #741b47;"> $content -replace "^$key\s*=.*", "$key = $value" |</span></div>
<div>
<span style="color: #741b47;"> Set-Content $file </span></div>
<div>
<span style="color: #741b47;"> } else {</span></div>
<div>
<span style="color: #741b47;"> Add-Content $file "$key = $value"</span></div>
<div>
<span style="color: #741b47;"> }</span></div>
<div>
<span style="color: #741b47;">}</span></div>
</div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Call this function in a following manner:</span></div>
<div>
<span style="color: #741b47;"><br /></span></div>
<div>
<span style="color: #4c1130; font-family: inherit;">$scriptfile = "C:\PowershellTest\UpdateImagesLocation.sql"</span></div>
<div>
<span style="color: #4c1130; font-family: inherit;">replacePatternMatchingValueInFile $scriptfile"SET @ImagesLocation" "'\\datashare\appImages'"</span></div>
<div>
<span style="color: #4c1130; font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">As a result, the variable assignment for @ImagesLocation would be changed to a different value in the sql file.</span></div>
<div>
<span style="color: #4c1130; font-family: inherit;"><br /></span></div>
</div>
<a href="http://www.codeproject.com" rel="tag" style="display:none">CodeProject</a>Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-76507328883785383522014-09-04T00:59:00.003+05:302015-08-17T23:13:11.177+05:30How to get name of the member (property name, etc) of a .NET class without using hard-coded/ magic strings<div dir="ltr" style="text-align: left;" trbidi="on">
Consider following sample class:<br />
<br />
<span style="color: blue;">public class City()</span><br />
<span style="color: blue;">{</span><br />
<span style="color: blue;">public int CityId { get; set; }</span><br />
<span style="color: blue;">public string CityName { get; set; }</span><br />
<span style="color: blue;">}</span><br />
<br />
For various reasons, we need name of the properties/ field of the class in a string variable in our code. (For example, while binding ID and Text fields in DropDown controls, etc).<br />
<br />
One option which is quite easy is to use the hard-coded strings directly.<br />
For exmaple,<br />
<span style="color: blue;">string idField = "CityId";</span><br />
<br />
But the biggest disadvantage with the above approach is that whenever a field name or property name is changed, you will need to manually search and edit such string values. If you forget, it will not give you any compile error and you will only come to know when some exception will be thrown from your code.<br />
<br />
In order to get rid of such issues, we should avoid using hard-coded values.<br />
<br />
.NET 3.5 or higher provides support of encapsulating methods using Func helper which we can use to find a name of the property/ field.<br />
<br />
<span style="color: blue;">using System;</span><br />
<div>
<span style="color: blue;">using System.Linq.Expressions;</span></div>
<div>
<span style="color: blue;"><br /></span></div>
<span style="color: blue;">string idField =</span><br />
<span style="color: blue;"> ((MemberExpression)((Expression<Func<City, int>>)(c => c.CityId)).Body).Member.Name;</span><br />
<span style="color: blue;"><br /></span>
<span style="color: blue;">string textField =</span><br />
<span style="color: blue;"> ((MemberExpression)((Expression<Func<City, string>>)(c => c.CityName)).Body).Member.Name;</span><br />
<span style="color: blue;"><br /></span>
That means, now whenever some changes property "CityId" to "City_Id" or whatever, s/he must will need to change the above code otherwise it will always give a compile error.<br />
<br />
Hope this helps</div>
<a href="http://www.codeproject.com" rel="tag" style="display:none">CodeProject</a>Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-83558309205367125322014-07-28T10:22:00.001+05:302014-07-28T10:22:47.036+05:30Exploring Abstract class (OOP concepts) in .NET - My Observations<div dir="ltr" style="text-align: left;" trbidi="on">
<div>
<span style="font-family: Verdana, sans-serif;">Following are my observations with Abstract classes in .NET.</span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Feel free to write in comments if you think of more points to be added in this list, or if you want know more details about one or more points :)</span></div>
<div>
<br /></div>
<ol style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">A class must be declared with "abstract" keyword</span></li>
<li><span style="font-family: Verdana, sans-serif;">It can contain complete implantation of methods</span></li>
<li><span style="font-family: Verdana, sans-serif;">Methods declared with "abstract" keyword can contain only declarations in Abstract class</span></li>
<li><span style="font-family: Verdana, sans-serif;">We cannot create an instance of abstract class</span></li>
<li><span style="font-family: Verdana, sans-serif;">Abstract class must be inherited</span></li>
<li><span style="font-family: Verdana, sans-serif;">A class derived from an abstract class must contain complete implementation of all abstract methods</span></li>
<li><span style="font-family: Verdana, sans-serif;">An abstract class can be derived from another abstract class</span></li>
<li><span style="font-family: Verdana, sans-serif;">Abstract class can be used as a parameter in methods</span></li>
<li><span style="font-family: Verdana, sans-serif;">If a class contains an abstract method, then that class must be marked as "abstract"</span></li>
<li><span style="font-family: Verdana, sans-serif;">An abstract class can have a constructor</span></li>
<li><span style="font-family: Verdana, sans-serif;">An abstract class can be inherited from a non-abstract class</span></li>
<li><span style="font-family: Verdana, sans-serif;"><span class="li-clickable-span"><a href="http://dotnetnsqlcorner.blogspot.in/2014/07/exploring-object-oriented-programming.html" target="_blank">Constructor rules</a></span> are also applied in case of abstract classes inheritance</span></li>
</ol>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">P.S. - Please note all keyword and code snippets (if demonstrated above) are in C#.NET </span></div>
</div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-90067357505544523712014-07-24T17:36:00.000+05:302014-07-24T17:36:11.138+05:30HTML/ CSS: FIX >> Contents do not appear on new line after floated DIVs.<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif;">When we use "float:left", or "float:right" styles in DIVs, we come across an issue due to which the contents after those floated DIVs is not appearing in new line, rather they start appearing immediately besides the completion of last floated DIV.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">In order to make further contents appearing in new line, add following DIV after floated DIVs:</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<blockquote class="tr_bq">
<span style="color: blue; font-family: Verdana, sans-serif;"><div style="clear: both;"></div></span></blockquote>
</div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-24344373832652498442014-07-24T17:31:00.001+05:302014-07-24T17:36:34.477+05:30How constructors are called while the classes are inherited: Exploring Object Oriented Programming and Concepts <div dir="ltr" style="text-align: left;" trbidi="on">
<h2>
<span style="font-family: Verdana, sans-serif;">Constructor Calls in Inheritance</span></h2>
<h3 style="text-align: left;">
<b style="font-family: Verdana, sans-serif;">Case study 1:</b></h3>
<span style="font-family: Verdana, sans-serif;">ClassA is a base class of ClassB.</span><br />
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif;">Both classes have default constructor.</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif;"><u><br /></u></span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif;"><u>Observations:</u><br />While creating a new instance of ClassB (without parameters):<br />- it will first call default constructor of ClassB which will call constructor of ClassA.<br />- it will complete executing code of default constructor of ClassA.<br />- after that, it will complete executing code of default constructor of ClassB.</span></div>
<div style="text-align: justify;">
<b style="font-family: Verdana, sans-serif; text-align: left;"><br /></b></div>
<h3 style="text-align: justify;">
<b style="font-family: Verdana, sans-serif; text-align: left;">Case study 2:</b></h3>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif;">ClassA is a base class of ClassB.</span></div>
<span style="font-family: Verdana, sans-serif;"> ClassA has no default constructor, but only parameterized constructor.<br /> ClassB has a default constructor.</span><br />
<div>
<span style="font-family: Verdana, sans-serif;"><b><br /></b></span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><u>Observations:</u></span><span style="font-family: Verdana, sans-serif;"> </span><br />
<span style="font-family: Verdana, sans-serif;">- it will not allow creating ClassA without a default constructor </span><br />
<div>
<span style="font-family: Verdana, sans-serif;">- if you want to do so, you need to call a base class constructor from ClassB's constructor: </span></div>
<div>
<span style="font-family: Verdana, sans-serif;">For example, following is the constructor of class A </span></div>
<blockquote class="tr_bq">
<span style="color: blue;"><span style="font-family: Verdana, sans-serif;">public ClassA(string someVariable) </span><span style="font-family: Verdana, sans-serif;">{ </span><span style="font-family: Verdana, sans-serif;">..... some code .... </span><span style="font-family: Verdana, sans-serif;">} </span></span></blockquote>
<div>
<span style="font-family: Verdana, sans-serif;">To allow ClassA without a default constructor, its all child classes should call base class constructor as below: </span></div>
<blockquote class="tr_bq">
<span style="color: blue; font-family: Verdana, sans-serif;">public ClassB(): base("some value") </span></blockquote>
<div>
<span style="font-family: Verdana, sans-serif;">or</span><br />
<blockquote class="tr_bq">
<span style="font-family: Verdana, sans-serif;"><span style="color: blue;"> public ClassB(string fullName): base(fullName) </span></span></blockquote>
</div>
</div>
</div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-27045350416495462872014-07-17T17:31:00.001+05:302015-08-17T23:15:47.416+05:30MVC Attribute to restrict users to enter any kind of HTML tags in the input fields<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Arial, Helvetica, sans-serif;">By default, if a user tries to enter any HTML tags in any of the input boxes in an MVC website, it will throw an error saying "Potentially dangerous request".</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">This is OK, as it doesn't allow user to enter HTML tags and prevents the site from security threat.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">However, the error page is not user friendly, and it is not giving a clue to the user as what was wrong there. So you may need to avoid default error page of browser, and instead your custom validation message should be displayed.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Also, there may be circumstances when you want to allow some fields to accept HTML tags (such as rich text boxes), and rest of the input fields shouldn't allow HTML. In that case, this approach will help.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">In order to achieve this we need to create an attribute in MVC and then can decorate Model properties with that attribute.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<b><span style="font-family: Arial, Helvetica, sans-serif;">Step 1</span></b><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><b>- </b>Decorate HttpPost method with <span style="color: blue;">[ValidateInput(false)]</span> </span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Validate input is true by default, and that is why it prevents execution of any code in case if there is any HTML input values are detected in posted data.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Here, we need to set it to false" as we would like to execute code of our custom attribute </span><br />
<br />
<blockquote class="tr_bq">
<span style="color: blue; font-family: Arial, Helvetica, sans-serif;"> [HttpPost]<br /> [ValidateAntiForgeryToken]<br /> [ValidateInput(false)]<br /> public virtual ActionResult CreateCustomer(Customer viewModel)<br /> {<br />......<br />}</span></blockquote>
<br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<b><span style="font-family: Arial, Helvetica, sans-serif;">Step 2</span></b><br />
<span style="font-family: Arial, Helvetica, sans-serif;">- Create custom attribute as below:</span><br />
<blockquote class="tr_bq">
<blockquote class="tr_bq">
<span style="color: blue; font-family: Arial, Helvetica, sans-serif;"><br />using System.ComponentModel.DataAnnotations;<br />using System.Web.Mvc;<br />using System.Text.RegularExpressions;</span></blockquote>
<blockquote>
<span style="color: blue; font-family: Arial, Helvetica, sans-serif;"> public class DenyHtmlInputAttribute: ValidationAttribute<br /> {<br /><br /><br /> protected override ValidationResult IsValid(object value, ValidationContext validationContext)<br /> {<br /> if (value == null)<br /> return ValidationResult.Success;<br /><br /><br /> var tagWithoutClosingRegex = new Regex(@"<[^>]+>");<br /><br /><br /> var hasTags = tagWithoutClosingRegex.IsMatch(value.ToString());<br /><br /><br /> if (!hasTags)<br /> return ValidationResult.Success;<br /><br /> return new ValidationResult(String.Format("{0} cannot contain html tags", validationContext.DisplayName));<br /> }<br /><br /><br /> }</span></blockquote>
</blockquote>
<span style="font-family: Arial, Helvetica, sans-serif;"><b>Step 3</b> </span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">- Decorate all input fields of your Model with the attribute created above:</span><br />
<blockquote class="tr_bq">
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue;">[</span><span style="color: blue;">DenyHtmlInput</span><span style="color: blue;">]<br /> public string Comments { get; set; }</span></span></blockquote>
<span style="font-family: Arial, Helvetica, sans-serif;">That's it.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">This is how you can restrict Html inputs on specific fields of your MVC view.</span></div>
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a></div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-84124993377120610672014-07-15T10:26:00.000+05:302015-08-17T23:16:06.660+05:30FIX: CKEditor not showing up when site is deployed on IIS server (ckeditor-full version 4.4.2) <div dir="ltr" style="text-align: left;" trbidi="on">
<b><span style="font-family: Arial, Helvetica, sans-serif;">Scenario:</span></b><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Recently, I came across an issue due to which CKEditor was not showing up after deploying website in IIS. It was showing an empty placeholder instead. It was showing up completely fine in development environment, and while running site through Visual Studio.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">I had "<span style="background-color: white; font-size: 14px; line-height: 17.804800033569336px;">ckeditor-full" (Version 4.4.2)" package added in my MVC project, and also bundles to load "\</span><span style="background-color: white; font-size: 14px; line-height: 17.804800033569336px;">ckeditor\adapters\jquery.js" and "</span><span style="background-color: white; font-size: 14px; line-height: 17.804800033569336px;">ckeditor\ckeditor.js" javascript files in the project.</span></span><br />
<span style="background-color: white; font-size: 14px; line-height: 17.804800033569336px;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="background-color: white; font-size: 14px; line-height: 17.804800033569336px;"><span style="font-family: Arial, Helvetica, sans-serif;">I checked in the code but it all looked fine.</span></span><br />
<span style="background-color: white; font-size: 14px; line-height: 17.804800033569336px;"><span style="font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="background-color: white; font-size: 14px; line-height: 17.804800033569336px;"><span style="font-family: Arial, Helvetica, sans-serif;">As a resolution, it turned out that, I had to include following line in my view before loading the bundles -</span></span><br />
<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px;"><br /></span>
<pre class="lang-cs prettyprint prettyprinted" style="background: rgb(238, 238, 238); border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 17.804800033569336px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;"><code style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="pun" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;"><</span><span class="pln" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">script type</span><span class="pun" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background: transparent; border: 0px; color: maroon; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">"text/javascript"</span><span class="pun" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">></span><span class="pln" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">
CKEDITOR_BASEPATH </span><span class="pun" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: maroon; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">"@Url.Content("</span><span class="pun" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">~/</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">Scripts</span><span class="pun" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">/</span><span class="pln" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">ckeditor</span><span class="pun" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">/</span><span class="str" style="background: transparent; border: 0px; color: maroon; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">")"</span><span class="pun" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;"></</span><span class="pln" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">script</span><span class="pun" style="background: transparent; border: 0px; font-size: 14px; margin: 0px; padding: 0px; vertical-align: baseline;">></span></code></pre>
<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px;"><br /></span>
<span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17.804800033569336px;">Hope this will help someone.</span></div>
<a href="http://www.codeproject.com" rel="tag" style="display:none">CodeProject</a>Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-32743265283115554742014-04-18T14:47:00.002+05:302015-08-17T23:16:23.845+05:30How to disable generating .PDB files while compiling .NET assembly/ website in Release mode<div dir="ltr" style="text-align: left;" trbidi="on">
While compiling your .NET assembly or publishing ASP.NET or MVC project in Release mode, you may observe .pdb file also gets generated along with necessary .dll or supporting files.<br />
PDB file contains debugging information and symbol which does not make any sense during deployment or release of a product.<br />
In order to get rid of generating PDB files during publish/ build -<br />
1. Open the project properties of a project in Visual Studio<br />
2. Click on "Build" tab<br />
3. Make sure the configuration selected is "Release"<br />
4. Click on "Advanced" button<br />
5. Select "none" in "Debug Info" selection box.<br />
6. Click OK, and save the project<br />
Try building the project, and you will not see PDB files.<br />
<br />
Screenshots:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiqlYHLA0-t1NvGZe3koZKH8dOd7fLlDzwB1XMIhAuGtZzO3PF06bPkrAeEocl2wSlnahUGqYIOdTBuYw8zB4SZJtuDQrrjGhDNo5pkOQTZ2VthxKKbj16onx7fR85Aqw29bALZZmV4Qg/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiqlYHLA0-t1NvGZe3koZKH8dOd7fLlDzwB1XMIhAuGtZzO3PF06bPkrAeEocl2wSlnahUGqYIOdTBuYw8zB4SZJtuDQrrjGhDNo5pkOQTZ2VthxKKbj16onx7fR85Aqw29bALZZmV4Qg/s1600/1.png" height="578" width="640" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0DDIrTuDQ0rq6RmhyLVmAuN3QzbF1wOlYeRE5wcwGuvZFToqJJioEqwkswZc0goblj-BqEtaNSw8_lP6-FaWRUaYEBHcYgF1bYhQk-WQhqCzfoDYqaK9XEDFPdwbY84pbivUbP5xfdco/s1600/2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0DDIrTuDQ0rq6RmhyLVmAuN3QzbF1wOlYeRE5wcwGuvZFToqJJioEqwkswZc0goblj-BqEtaNSw8_lP6-FaWRUaYEBHcYgF1bYhQk-WQhqCzfoDYqaK9XEDFPdwbY84pbivUbP5xfdco/s1600/2.png" height="480" width="640" /></a></div>
<br /></div>
<a href="http://www.codeproject.com" rel="tag" style="display:none">CodeProject</a>Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com2tag:blogger.com,1999:blog-2800390007610438954.post-72219429671842987372014-04-01T14:26:00.002+05:302014-04-01T14:26:58.889+05:30Function Evaluation Timed out error in Entity Framework<div dir="ltr" style="text-align: left;" trbidi="on">
While running a time-consuming queries in Entity Framework, you may face the error saying "Function evaluation timed out".<br />
In order to fix this error, you will need to increase the timeout of underlying DbContext.<br />
<br />
<h3 style="text-align: left;">
<b>Steps</b></h3>
<br />
<ul style="text-align: left;">
<li>Open "<i>EntityName</i>.Context.cs" file.</li>
<li>This file must be having an Entity class inherited from "DbContext"</li>
<li>Copy following code in the default constructor of this class:</li>
</ul>
<span style="color: blue;"> var adapter = (IObjectContextAdapter)this;</span><br /><span style="color: blue;"> var objectContext = adapter.ObjectContext;</span><br /><span style="color: blue;"> objectContext.CommandTimeout = 120; //2 minutes</span><br />
<span style="color: blue;"><br /></span>
Please note that CommandTimeout is in seconds.</div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-86177143205217115882014-03-31T23:46:00.002+05:302014-03-31T23:48:21.109+05:30Error 403, 403.14, 404 - After deploying ASP.NET MVC website on IIS 7, IIS 7.5<div dir="ltr" style="text-align: left;" trbidi="on">
After deploying your ASP.NET MVC website on IIS 7 or IIS 7.5, you might face following errors while browsing your website:<br />
<div>
<br />
<div>
<ul style="text-align: left;">
<li><b>403.14 - Forbidden </b>(The Web server is configured to not list the contents of this directory.)</li>
<li><b>404 - Not Found </b>(The resource you are looking for has been removed, had its name changed, or is temporarily unavailable)</li>
</ul>
</div>
<div>
<br /></div>
</div>
<div>
Following are the possible causes/ fixes for this -</div>
<div>
<ul style="text-align: left;">
<li>Make sure the Application pool targets correct version of .NET framework (i.e .NET Framework v4.0.30319 for .NET 4 and 4.5)</li>
<li>Make sure the Pipeline mode of IIS Application pool is "<b>Integrated</b>"</li>
<li>Check <b>UrlRoutingModule-4.0</b> is added in the modules of that website.</li>
<li>(To do that, open list of added modules by clicking "<b>Modules</b>" against your website, and see if the module "UrlRoutingModule-4.0" is added or not). If not, then add a module by clicking "<b>Add Managed Module</b>" button, where select <b>System.Web.Routing.UrlRoutingModule</b> as a Module type, and give "UrlRoutingModule-4.0" as its name)</li>
<li>Make sure you have following element added in <b>system.webServer</b> section of website's web.config</li>
</ul>
<blockquote class="tr_bq">
<span style="color: blue;"><system.webServer></span></blockquote>
<blockquote>
<span style="color: blue;"><modules runAllManagedModulesForAllRequests="true"></modules> </span></blockquote>
<blockquote class="tr_bq">
<span style="color: blue;"></system.webServer></span></blockquote>
<br />
In most cases of 403.14, or 404 error, above are the possible causes and fixes. </div>
</div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com258tag:blogger.com,1999:blog-2800390007610438954.post-75136224409696504652014-03-25T11:18:00.000+05:302015-08-17T23:17:26.232+05:30FIX: 'Model' conflicts with the declaration 'System.Web.Mvc.WebViewPage.Model'<div dir="ltr" style="text-align: left;" trbidi="on">
I suddenly started receiving this error in one of my razor view. The error message was -<br />
<i><span style="color: blue;">'Model' conflicts with the declaration 'System.Web.Mvc.WebViewPage<TModel>.Model'</span></i><br />
<i><span style="color: blue;"><br /></span></i>
This error is caused if you use "Model" instead of "model" in one or more lambda expressions (predicates) in your view.<br />
<br />
Perform a case-sensitive search for "Model =>", and its very much possible that you may notice this in one or more lambda expressions,<br />
For example,<br />
<blockquote class="tr_bq">
<b><span style="color: #0c343d;">@Html.EditorFor(Model => Model.BirthDate)</span></b></blockquote>
<blockquote class="tr_bq">
This should be actually, <b><span style="color: #0c343d;">@Html.EditorFor(model => model.BirthDate)</span></b></blockquote>
Correcting this should fix the Model conflict error for you in your view.<br />
<br /></div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com2tag:blogger.com,1999:blog-2800390007610438954.post-39725776725210028472014-01-02T15:13:00.001+05:302015-08-17T23:17:38.437+05:30Accessing Model property in MVC View from Javascript<div dir="ltr" style="text-align: left;" trbidi="on">
For example, you have following model in your MVC application:<br />
<br />
<span style="color: blue;">public class Employee</span><br />
<span style="color: blue;">{</span><br />
<span style="color: blue;">public string EmployeeName</span><br />
<span style="color: blue;">public int EmployeeNumber</span><br />
<span style="color: blue;">}</span><br />
<br />
You have bound this model to your MVC view (Razor/ Html), and there may be a case when you need to access "EmployeeName" in the Javascript from that view.<br />
<br />
You can access value of "EmployeeName" property of your model by following way:<br />
<br />
<span style="color: blue;"><script type="text/javascript"></span><br />
<span style="color: blue;">function showEmployeeName()</span><br />
<span style="color: blue;">{</span><br />
<span style="color: blue;">alert('@(Model.EmployeeName)');</span><br />
<span style="color: blue;">}</span><br />
<span style="color: blue;"><br /></span>
<span style="color: blue;"></script></span></div>
<a href="http://www.codeproject.com" rel="tag" style="display:none">CodeProject</a>Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com23tag:blogger.com,1999:blog-2800390007610438954.post-23901735894834546882013-12-30T17:45:00.000+05:302015-08-25T12:44:12.358+05:30FIX: JQuery date format validation for non US dates in Chrome<div dir="ltr" style="text-align: left;" trbidi="on">
Include following script in your javascript file and reference it in your page(s) to get rid of the most common error of JQuery date format validation in Chrome which doesn't allow non US dates.<br />
<br />
<span style="color: blue;"></span><span style="color: blue;"> $(document).ready(function () {<br />jQuery.validator.methods["date"] = function (value, element) { return true; }</span><br />
<span style="color: blue;"> })</span></div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com4tag:blogger.com,1999:blog-2800390007610438954.post-11947514055774597192013-08-07T12:36:00.000+05:302013-08-07T12:46:53.302+05:30My Bookmarks - Part I: CSS<div dir="ltr" style="text-align: left;" trbidi="on">
Following are references to some of the sites/ posts which I came across while developing websites.<br />
<br />
<b><i>**Credit to the respective post/ blog owners **</i></b><br />
<br />
<br />
<b>Cross-Browser CSS Gradient</b><br />
This concise CSS class can make your DIV or any other HTML control appearing with gradient background without really needing any image.<br />
<br />
<blockquote class="tr_bq">
.gradientBackground<br />
{<br />
background: #999; /* for non-css3 browsers */<br />
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000'); /* for IE */<br />
background: -webkit-gradient(linear, left top, left bottom, from(#ccc), to(#000)); /* for webkit browsers */<br />
background: -moz-linear-gradient(top, #ccc, #000); /* for firefox 3.6+ */<br />
} </blockquote>
Complete reference to post: <a href="http://webdesignerwall.com/tutorials/cross-browser-css-gradient">http://webdesignerwall.com/tutorials/cross-browser-css-gradient</a><br />
A cool website to get color code to use them while setting gradients: <a href="http://tools.dynamicdrive.com/gradient/">http://tools.dynamicdrive.com/gradient/</a></div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-62596122944857958402013-07-31T11:33:00.000+05:302013-07-31T11:33:01.158+05:30Replacement of InnerText property, while updating display text of elements such as (span, etc) in HTML.<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
<span style="font-family: Arial,Helvetica,sans-serif;">This article aims to provide a javascript function to update display text of controls such as (SPAN, etc) in HTML using javascript. </span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="font-family: Arial,Helvetica,sans-serif;">Generally, we tend to update <i>innerText</i> property of such elements in this scenario, but when it works perfectly as expected in IE and Chrome, it fails in Firefox. Because,it's not supported.</span></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="font-family: Arial,Helvetica,sans-serif;">Instead, include below javascript function in your JS library, and call it. This is more elegant and cross-browser solution of this problem.</span></div>
<div style="text-align: left;">
<span style="font-family: Arial,Helvetica,sans-serif;"><br /></span></div>
<br />
<br />
<pre class="default prettyprint prettyprinted"><span style="color: blue;"><code><span class="kwd">function</span><span class="pln"> setTextContent</span><span class="pun">(</span><span class="pln">element</span><span class="pun">,</span><span class="pln"> text</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">while</span><span class="pln"> </span><span class="pun">(</span><span class="pln">element</span><span class="pun">.</span><span class="pln">firstChild</span><span class="pun">!==</span><span class="kwd">null</span><span class="pun">)</span><span class="pln">
element</span><span class="pun">.</span><span class="pln">removeChild</span><span class="pun">(</span><span class="pln">element</span><span class="pun">.</span><span class="pln">firstChild</span><span class="pun">);</span><span class="pln"> </span><span class="com">// remove all existing content</span><span class="pln">
element</span><span class="pun">.</span><span class="pln">appendChild</span><span class="pun">(</span><span class="pln">document</span><span class="pun">.</span><span class="pln">createTextNode</span><span class="pun">(</span><span class="pln">text</span><span class="pun">));</span><span class="pln">
</span><span class="pun">}</span></code></span></pre>
<br />
<br />
Sample call:<br />
<br />
<span style="color: blue;">setTextContent($('span.displayText), result);</span></div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0tag:blogger.com,1999:blog-2800390007610438954.post-78589672968116105982013-07-05T14:49:00.002+05:302013-07-05T14:49:59.593+05:30When should we not use MVC bundling?<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Arial,Helvetica,sans-serif;">As we all know MVC bundling and minification is a very powerful feature, but recently I faced a strange problem in one of the project as detailed below.</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><i><br /></i></span>
<span style="font-family: Arial,Helvetica,sans-serif;"><i>I was using <a href="http://trentrichardson.com/examples/timepicker/" rel="nofollow">Trent Richardson's Timepicker</a>
control to avail time picker functionality in my MVC 4 application. I
had relevant JQuery file bundled using MVC bundling feature. I found
this working quite well in development environment (Visual Studio 2012).
</i></span>
<br />
<span style="font-family: Arial,Helvetica,sans-serif;"><i>But when I deployed the website on IIS, I started facing a strange issue, and there was a javascript error "<em>function expected</em>"
in that particular bundle. I could see the bundle got loaded because
developer tool was showing javascript code when that bundle was selected
in "scripts" tab.</i></span><br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;"><i>
</i></span><span style="font-family: Arial,Helvetica,sans-serif;"><i>Finally, when I referenced the JQuery file directly instead of
bundle, it started working fine on IIS. Though the problem got solved, I was than now curious to know what was wrong with that particular file if
bundled, and if MVC bundling was actually an issue, then why it was
working well in development environment, but not in IIS?</i></span><br />
<br />
<span style="font-family: Arial,Helvetica,sans-serif;">Posting this on various forums gave me following two detailed answers which helped me in identifying in which situations we should avoid using bundling -</span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><br /></span>
<span style="font-family: Arial,Helvetica,sans-serif;"><b>Answer 1</b></span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><br /></span>
<div class="post-text">
<span style="font-family: Arial,Helvetica,sans-serif;"><i>Minification is a complex process by making
scripts/styles smaller using techniques such variable name shortening,
white space elimination, comments removal, etc... It uses ASP.NET Web
Optimization that depends on WebGrease for minification. Of course,
there can have issues but I personnaly never noticed that. </i></span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><i>
</i></span><span style="font-family: Arial,Helvetica,sans-serif;"><i>Here are some situations, where you should not use bundling</i></span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><i>
</i></span><ul>
<li><span style="font-family: Arial,Helvetica,sans-serif;"><i><strong>There is only one file in your bundle</strong>. Why bundling ?</i></span></li>
<li><span style="font-family: Arial,Helvetica,sans-serif;"><i><strong>You are using only famous frameworks such as JQuery or jQuery UI</strong>.
Do not redistribute scripts that are already served by someone else.
Google/Microsoft/Amazon/... already provide CDN for the most popular,
open-source JavaScript libraries.</i></span><br />
<span style="font-family: Arial,Helvetica,sans-serif;"><i><code><script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script></code></i></span></li>
<li><span style="font-family: Arial,Helvetica,sans-serif;"><i><strong>Your bundle takes only a few Bytes</strong>. Web
performance Optimization suggests to limit the number of web requests.
Everything has a cost. Not very optimal, but sometimes it's better to
put inline scripts in your page. </i></span></li>
<li><span style="font-family: Arial,Helvetica,sans-serif;"><i><strong>In some architectures</strong>. Bundles requests contains a
unique identifier used for caching. If any file in the bundle changes,
the ASP.NET optimization framework will generate a new token,
guaranteeing that browser requests for the bundle will get the latest
bundle. When working with some architectures, JS updates can be frequent
and will invalidate all your bundles.</i></span></li>
<li><span style="font-family: Arial,Helvetica,sans-serif;"><i><strong>On Dev Environment</strong>. It's is really really painful to debug a bundle. </i></span></li>
</ul>
<span style="font-family: Arial,Helvetica,sans-serif;"><b>Answer 2</b></span></div>
<div class="post-text">
<br /><span style="font-family: Arial,Helvetica,sans-serif;"><em>What bundling suppose to do is to put together the script/stylesheet
files in a single bundle into a single request and send it to the
client so that the browser has to make less calls to get those required
script files.
In a development environment, when you do debugging in visual studio. It
doesn't do the above process unless you specify it to do so. But in a
production environment, when the debug is set to false in the web.config
file. it will start to do the above process.
There can be some other reasons as well. such as the script might have
two versions. one for debugging and one for production. I came across
such a situation with knockout. in my development environment I had
referenced the debug version of the script. But when I put it into the
production environment, everything came to a hault. There was a release
version for the </em></span></div>
<div class="post-text">
<span style="font-family: Arial,Helvetica,sans-serif;"><em>knockout script file and I had to reference that to make
everything work again</em></span></div>
<div class="post-text">
<span style="font-family: Arial,Helvetica,sans-serif;"><em> </em></span></div>
<div class="post-text">
<span style="font-family: Arial,Helvetica,sans-serif;"><em> </em></span></div>
<div class="post-text">
<span style="font-family: Arial,Helvetica,sans-serif;">Hope this will help you<em><br /></em></span></div>
</div>
Nirman Doshihttp://www.blogger.com/profile/14715472397577554684noreply@blogger.com0