Posts

Creating Custom Action Filters

The possible uses for action filters are as varied as the actions to which they can be applied. Some possible uses for action filters include the following: Logging in order to track user interactions. "Anti-image-leeching" to prevent images from being loaded in pages that are not on your site. Web crawler filtering to change application behavior based on the browser user agent. Localization to set the locale. Dynamic actions to inject an action into a controller. Implementing a Custom Action Filter An action filter is implemented as an attribute class that inherits from ActionFilterAttribute. ActionFilterAttribute is an abstract class that has four virtual methods that you can override: OnActionExecuting, OnActionExecuted, OnResultExecuting, and OnResultExecuted. To implement an action filter, you must override at least one of these methods. The ASP.NET MVC framework will call the OnActionExecuting method of your action filter before it calls any action method that is marked...

Authorization filter in MVC Applications

Many Web applications require users to log in before the users are granted access to restricted content. In some applications, even users who are logged in might have restrictions on what content they can view or what fields they can edit. To restrict access to an ASP.NET MVC view, you restrict access to the action method that renders the view. To accomplish this, the MVC framework provides the AuthorizeAttribute class. Using the Authorize Attribute When you mark an action method with the Authorize attribute, access to that action method is restricted to users who are both authenticated and authorized. If you mark a controller with the attribute, all action methods in the controller are restricted. The Authorize attribute lets you indicate that authorization is restricted to predefined roles or to individual users. This gives you a high degree of control over who is authorized to view any page on the site. If an unauthorized user tries to access a method that is marked with the Authori...

Action Filtering in MVC Applications

In ASP.NET MVC, controllers define action methods that usually have a one-to-one relationship with possible user interactions, such as clicking a link or submitting a form. For example, when the user clicks a link, a request is routed to the designated controller, and the corresponding action method is called. Sometimes you want to perform logic either before an action method is called or after an action method runs. To support this, ASP.NET MVC provides action filters. Action filters are custom attributes that provide a declarative means to add pre-action and post-action behavior to controller action methods. MVC Action Filter Types ASP.NET MVC provides the following types of action filters: Authorization filter, which makes security decisions about whether to execute an action method, such as performing authentication or validating properties of the request. The AuthorizeAttribute class is one example of an authorization filter. Action filter, which wraps the action method execution....

Threading in .NET

Introduction and Concepts C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads. A C# client program (Console, WPF, or Windows Forms) starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multithreaded by creating additional threads. Here’s a simple example and its output: All examples assume the following namespaces are imported: using System; using System.Threading; class ThreadTest { static void Main() { Thread t = new Thread (WriteY); // Kick off a new thread t.Start(); // running WriteY() // Simultaneously, do something on the main thread. for (int i = 0; i } static void WriteY() { for (int i = 0; i } } xxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyy yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxx...
Image
  My dear Sachin... I love u :-)

C# Cloning

Introduction "Introduction Cloning C# objects is one of those things that appears easy but is actually quite complicated with many 'gotchas.' This articl..." Cloning C# objects is one of those things that appears easy but is actually quite complicated with many "gotchas." This article describes the most common ways to clone a C# object. Shallow vs. Deep Cloning There are two types of object cloning: shallow and deep. A shallow clone copies the references but not the referenced objects. A deep clone copies the referenced objects as well. Hence, a reference in the original object and the same reference in a shallow-cloned object both point to the same object. Whereas a deep-cloned object contains a copy of everything directly or indirectly referenced by the object. ICloneable Interface The ICloneable interface contains a single Clone method, which is used to create a copy of the current object. public interface ICloneable { object Clone(); } The problem wi...

The Great Rhino Mocks

What is Rhino Mocks? Rhino Mocks allows you to easily create mock objects and setup a wide range of expectations on them using strongly typed notation instead of compiler-opaque strings. It's as simple as: IProjectView projectView = mocks.StrictMock (); Expect.Call(projectView.Title).Return("Project's Title"); Definitions: Mock Object - an object that pretends to be another object and allows you to set expectations on its interactions with another object. Interaction Based Testing - you specify a certain sequence of interactions between objects, initiate an action, and then verify that the sequence of interactions happened as you specified it. State Based Testing - you initiate an action and then check for the expected results (return value, property, created object, etc). Expectation - general name for validation that a particular method call is the expected one. Record & Replay model - a model that allows for recording actions on a mock object and then replay...