Thursday, 5 July 2018

.NET Standard: What it is and how to use

What really is dot net standard?  

Let me try to put it in simple words. We used to use different BCL's for different frameworks and remembering it is a tough job. So Microsoft once again has come up with a solution to help developers to put a little less load on on the brain to remember the API's. They have replaced all the BCL's with a common one which they are calling as .net Standard. 

.Net Standard 2.0 has around 32,000 API's. You can download the list from here or you can check it here at GitHub.

.Net Standard replaces Portable Class Libraries (PCLs) as the tool for building .NET libraries that work everywhere 

How to use it? 

If you have .net core installed on your machine you can start working on .net Standard just by creating a Class Library. Yes, the old Class library is now Class Library for .net standard. 

For those who understand pictures better or would like to read less. 

Above pictures are taken from a slide of Immo Landwerth. 

The table below can be used as the reference for writing a class library using the standard.

If you are choosing a version for writing .net standard, you should consider a trade-off which is the 
higher the version of .net standard more apis are available. Lower the version, more platforms have already implemented it.

You can follow .net standard on GitHub 

Below are the list of API's added newly in .net standard 2.0

Microsoft.Win32.SafeHandles 22
System 1,366
System.CodeDom.Compiler 46
System.Collections 335
System.Collections.Generic 23
System.Collections.ObjectModel 1
System.Collections.Specialized 281
System.ComponentModel 1,843
System.ComponentModel.Design 565
System.ComponentModel.Design.Serialization 155
System.Configuration.Assemblies 13
System.Data 1,587
System.Data.Common 907
System.Data.SqlTypes 971
System.Diagnostics 826
System.Diagnostics.CodeAnalysis 2
System.Diagnostics.Contracts 89
System.Diagnostics.SymbolStore 146
System.Diagnostics.Tracing 2
System.Drawing 520
System.Dynamic 226
System.Globalization 217
System.IO 309
System.IO.Compression 8
System.IO.IsolatedStorage 134
System.IO.MemoryMappedFiles 66
System.IO.Pipes 125
System.Linq 366
System.Linq.Expressions 50
System.Net 1,360
System.Net.Cache 52
System.Net.Http.Headers 20
System.Net.Mail 289
System.Net.Mime 75
System.Net.NetworkInformation 696
System.Net.Security 194
System.Net.Sockets 168
System.Net.WebSockets 188
System.Numerics 1
System.Reflection 403
System.Reflection.Emit 3
System.Resources 91
System.Runtime 12
System.Runtime.CompilerServices 190
System.Runtime.ConstrainedExecution 22
System.Runtime.ExceptionServices 6
System.Runtime.InteropServices 106
System.Runtime.InteropServices.ComTypes 15
System.Runtime.Serialization 486
System.Runtime.Serialization.Formatters 13
System.Runtime.Serialization.Formatters.Binary 23
System.Runtime.Serialization.Json 80
System.Runtime.Versioning 33
System.Security 124
System.Security.Authentication 11
System.Security.Authentication.ExtendedProtection 36
System.Security.Claims 206
System.Security.Cryptography 575
System.Security.Cryptography.X509Certificates 76
System.Security.Permissions 88
System.Security.Principal 41
System.Text 83
System.Text.RegularExpressions 31
System.Threading 330
System.Threading.Tasks 66
System.Timers 39
System.Transactions 223
System.Web 32
System.Xml 1,362
System.Xml.Linq 7
System.Xml.Resolvers 25
System.Xml.Schema 967
System.Xml.Serialization 943
System.Xml.XPath 261
System.Xml.Xsl 140

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 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.

System.Collections Namespace
System.Linq Namespace
Derives from
No base interface
Derives from IEnumerable
Not 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
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
Table from code project