pages

Thursday, 28 September 2017

Filter Attributes in MVC

An action filter is an attribute that can be applied to an Action or the controller.

Types of filters in MVC:

1.Authorization filters
2.Action filters
3.Result filters
4.Exception filters


Filters in MVC execute in the same order as mentioned above.

Authorization filters always execute before action filters and exception filters always execute at last.
The base class for all action filters is the FilterAttribute class which is in System.Web.Mvc namespace
The base class which is ActionFilterAttribute has the following methods which we can override:
· OnActionExecuting – This method is called before a controller action is executed.
· OnActionExecuted – This method is called after a controller action is executed.
· OnResultExecuting – This method is called before a controller action result is executed.
· OnResultExecuted – This method is called after a controller action result is executed.

Tuesday, 23 May 2017

Async and Sessionless controller in Asp.net MVC

I know it's very late and both the concepts are released long back.

Nov 2010 Microsoft released MVC3 RC in which provided a feature for Controller class to be free from session state was provided. Here is a link to Scott Gu's blog releasing same

Scott tells while answering a question on the post: Session state is designed so that only one request from a particular user/session occurs at a time. So if you have a page that has multiple AJAX callbacks happening at once they will be processed in serial fashion on the server. Going session-less means that they would execute in parallel.


Attribute Name is SessionState and it takes Enum of type SessionStateBehaviour which has the following members:

  • Default
  • Disabled
  • ReadOnly
  • Required

Usage:     [SessionState(SessionStateBehavior.Disabled)]

In case you opt to use SessionState attribute TempData and other session related thing won't work as mentioned in a small demo on Dot net curry.

Sessionless Controllers specifies the type of Session support you want for your class. On the other hand, as the name suggests Async Controllers let us perform operations without making the thread wait for completing the operation.

This is a nice blog by an MVP about calling the web services using Normal, Async and Sessionless manner.

Async controllers are deprecated in MVC4 and they are implemented using Task class.  The new model for asynchronous methods is called the Task-based Asynchronous Pattern (TAP), More can be read here about it.

Hope it helps.

Tuesday, 16 May 2017

Difference between Tier and Layers

During an interview this question was asked and I was pretty sure that what ever layers we create in our code (Presentation, BLL, DLL etc) are the tiers but I was wrong.
An answer on stack overflow sums it up very well.
layer = a part of your code, if your application is a cake, this is a slice.
tier = a physical machine, a server.

Scott Hanselman also have a nice blog post on same. He says:
a "Tier" is a unit of deployment, while a "Layer" is a logical separation of responsibility within code.  You may say you have a "3-tier" system, but be running it on one laptop.  You may say your have a "3-layer" system, but have only ASP.NET pages that talk to a database. 

and then he explains about what should be there in your Presentation, BLL and DLL and what should not be there.


Sunday, 23 April 2017

Iqueryable vs Ienumerable a comparison.

There is always a doubt in developers mind regarding usage of IQueryable and IEnumerable.

IQueryable extends IEnumerable. IQueryable should be used while working with remote databases. 

Well, here are some facts which can help resolving your doubt and can strengthen your decision-making as which one to use.






Some important points about Ienumberable<T>
  • It's read-only.
  • Iterates only in forward direction.
  • Doesn't supports adding or removing objects.
  • It's a base interface for any generic collection
When to use IEnumberable:
  • Working with read-only collection
  • Need to read objects in the forward direction.
  • Want to iterate collection using for-each.



Some important points about IQueryable<T>
  • It implements IEnumerable so the results can be iterated using for-each.
  • Best suited for querying against the remote data source.
When to use IQueryable
  • your data source is queryable.
  • Data needs to be applied 
  • The deferred way is needed to load the data.





IEnumerable
IQueryable
Namespace
System.Collections Namespace
System.Linq Namespace
Derives from
No base interface
Derives from IEnumerable
Supported
Supported
Not Supported
Supported
How does it work
While querying data from the database, IEnumerable executes a select query on the server side, load data in-memory on the client side and then filter data. Hence does more work and becomes slow.
While querying data from the database, IQueryable executes a select query on the server side with all filters. Hence does less work and becomes fast.
Suitable for
LINQ to Object and LINQ to XML queries
LINQ to SQL queries
Custom Query
Doesn’t support
Supports using CreateQuery and Execute methods
Extension method
parameter
Extension methods supported in IEnumerable takes functional objects.
Extension methods supported in IEnumerable takes expression objects, i.e., expression tree.
When to use
When querying data from in-memory collections like ListArray, etc.
When querying data from out-memory (like remote database, service) collections.
Best Uses
In-memory traversal
Paging
Table from code project

Monday, 13 February 2017

Visual Studio Spell Checker

If you have to deal with a Grammer freak Lead or manager who checks all the spellings or you are a perfectionist looking for correct spellings all the time. You can install this extention to your visual studio.

VS2017 and Later


Some features:

  • An option is available to spell check any WPF text box within Visual Studio
  • Specify one or more dictionary languages to use for spell checking. The package comes with several dictionaries for various languages.
  • Specify additional folders to search for custom dictionaries or user dictionaries.
  • Enable or disable spell checking as you type.
  • Ignore words with digits, in all uppercase, and/or in mixed case.
  • Ignore .NET and C-style format string specifiers. 
  • Ignore words that look like filenames and e-mail addresses.
  • Ignore words that look like XML elements in spell checked text.
  • Treat underscores as separators.
  • Various options for excluding specific elements of C# source code files from being spell checked.
  • Exclude files from spell checking by filename extension.
  • Specify a list of XML elements in which the content should be ignored when spell checking XML files.
  • Specify a list of XML attributes for which the value should be spell checked when spell checking XML files.
  • Specify additional folders to search for dictionaries.
  • Determine localized resource file language from the filename.
  • Configurations can be specified at any level (solution, project, folder, or file).  Options can be inherited or overridden.


You can also contribute to project on GitHub: Spell Checker GitHub