June 18, 2016

When to use IEnumerable, ICollection, IList and List

IEnumerable uses IEnumerator internally.

Whenever we pass IEnumerator to another function ,it knows the current position of item/object.
Whenever we pass IEnumerable collection to another function ,it doesn't know the current position of item/object OR current state.

IEnumerator have one Property current and two methods Reset and MoveNext.

IEnumerable have one method GetEnumerator()

If you want to loop through with the collection one by one and you are not interested in the current cursor position then Ienumerable is nice option.

If you are keen to know the current position of object then should go with IEnumerator.


I think that the question when to use IEnumerable, ICollection, IList or List is a common one that hasn’t often being answered in an easy way. I not only want to do this within this article, but I also want to give you some further information to understand the things more deeply.
If you get the understanding for the principle, you’ll be automatically really confident when you have to do this decision for the next time.
If you only want to know when to use which type, scroll down and have a look at the table providing the scenarios and the relevant types. I strongly recommend reading of the entire article to get a deeper understanding.

Let’s first take a look at the individual types and more importantly its members. It’s generally a good idea to have a look at the types in question if you want to decide which type you should use in a specific situation in your code.
IEnumerable
First of all, it is important to understand, that there are two different interfaces defined in the .NET base class library. There is a non-generic IEnumerable interface and there is a generic type-safe IEnumerable<T> interface.
The IEnumerable interface is located in the System.Collections namespace and contains only a single method definition. The interface definition looks like this:
public interface IEnumerable {
IEnumerator GetEnumerator();
}
The GetEnumerator method must return an instance of an object of a class which implements the IEnumerator interface. We won’t have a look at the definition of the IEnumerator interface this time, but if you are interested, please visit the official msdn documentation.
It is important to know that the C# language foreach keyword works with all types that implement the IEnumerable interface. Only in C# it also works with things that don’t explicitly implement IEnumerable or IEnumerable<T>. I believe you have been using the foreach keyword many times and without worrying about the reason why and how it worked with that type.
IEnumerable<T>
Let’s now take a look at the definition of the generic and type-safe version called IEnumerable<T> which is located in the System.Collections.Generic namespace:
public interface IEnumerable<out T> : IEnumerable
{
IEnumerator<T> GetEnumerator();
}
As you can see the IEnumerable<T> interface inherits from the IEnumerable interface. Therefore a type which implements IEnumerable<T> has also to implement the members of IEnumerable.
IEnumerable<T> defines a single method GetEnumerator which returns an instance of an object that implements the IEnumerator<T> interface. We won’t have a look at this interface this time. Please take a look at the official msdn documentation if you would like to get some more information.
ICollection
As you can imagine, there are also two versions of ICollection which are System.Collections.ICollection and the generic version System.Collections.Generic.ICollection<T>.
Let’s take a look at the definition of the ICollection interface type:
public interface ICollection : IEnumerable
{
int Count { get; }
bool IsSynchronized { get; }
Object SyncRoot { get; }
void CopyTo(Array array, int index);
}
ICollection inherits from IEnumerable. You therefore have all members from the IEnumerable interface implemented in all classes that implement the ICollection interface.
I won’t go much into details of the defined methods and properties this time. I just want to let you know about the official description from the msdn documentation:
Defines size, enumerators, and synchronization methods for all nongeneric collections.
ICollection<T>
When we look at the generic version of ICollection, you’ll recognize that it does not look exactly the same as the non-generic equivalent:
?
public interface ICollection<T> : IEnumerable<T>, IEnumerable
{
int Count { get; }
bool IsReadOnly { get; }
void Add(T item);
void Clear();
bool Contains(T item);
void CopyTo(T[] array, int arrayIndex);
bool Remove(T item);
}
The official msdn documentation looks like this:
Defines methods to manipulate generic collections.
In fact, we have some more methods to add, remove and clear a collection. The way synchronization was implemented differs also. I believe that this happened because the generic version of this interface was introduced with .NET 2.0 whereas the non-generic version was already introduced in .NET 1.1.
IList
The IList interface has of course a non-generic and a generic version. We start with looking at the non-generic IList interface:
?
public interface IList : ICollection, IEnumerable
{
bool IsFixedSize { get; }
bool IsReadOnly { get; }
Object this[int index] { get; set; }
int Add(Object value);
void Clear();
bool Contains(Object value);
int IndexOf(Object value);
void Insert(int index, Object value);
void Remove(Object value);
void RemoveAt(int index);
}
IList implements ICollection and IEnumerable. In addition it provides method definitions for adding and removing elements and to clear the collection. It also provides methods for handling the positioning of the elements within the collection. It also provides an object indexer to allow the user to access the collection with square brackets like:
?
myList[elementIndex]
IList<T>
Now let’s take a look at the generic version of IList:
?
public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
{
T this[int index] { get; set; }
int IndexOf(T item);
void Insert(int index, T item);
void RemoveAt(int index);
}
As we mentioned before when discussing the ICollection<T> interface, there are some more methods defined in the ICollection<T> interface than in the ICollection interface. Therefore the member list of the IList<T> interface is a bit shorter than the non-generic equivalent. We only have some new methods for accessing a collection with specific positioning.
Conclusion
Take a look at the following graphic. Not every interface member is displayed, but it should be enough to give you an overview about all types we discussed.

Which type should you depend on?
Now that we have looked at all of the interfaces in question, we are ready to decide which interface we should depend on in a certain situation. Generally, it’s a great idea to depend only on things we really need. I am going to show you how this decision can be made very easily and what you can expect to gain if you do so.
If you use a narrower interface type such as IEnumerable instead of IList, you protect your code against breaking changes. If you use IEnumerable, the caller of your method can provide any object which implements the IEnumerable interface. These are nearly all collection types of the base class library and in addition many custom defined types. The caller code can be changed in the future and your code won’t break that easily as it would if you had used ICollection or even worse IList.
If you use a wider interface type such as IList, you are more in danger of breaking code changes. If someone wants to call your method with a custom defined object which only implements IEnumerable, it simply won’t work and will result in a compilation error.
Generally you should always use that type that provides a contract for only the methods you really use.
The following table gives you an overview of how you can decide which type you should depend on.
Interface
Scenario
IEnumerable, IEnumerable<T>
The only thing you want is to iterate over the elements in a collection. You only need read-only access to that collection.
ICollection, ICollection<T>
You want to modify the collection or you care about its size.
IList, IList<T>
You want to modify the collection and you care about the ordering and / or positioning of the elements in the collection.
List, List<T>
Since in object oriented design you want to depend on abstractions instead of implementations, you should never have a member of your own implementations with the concrete type List/List.
Recommendation

If you are now curious about interfaces, how they work, how to implement them by your own and how their use can improve your design and therefore the quality of your entire code base, I highly recommend watching C# Interfaces by Jeremy Clark on Pluralsight. He manages to explain several really important concepts in a very easily understandable way.

June 15, 2016

Compatible SQL to test for not null and not empty strings

WITH SampleData AS
    (SELECT 1 AS col1, CAST(NULL AS varchar(10)) AS col2
     UNION ALL
     SELECT 2, ''
     UNION ALL
     SELECT 3, 'hello')
SELECT *
  FROM SampleData
 WHERE NULLIF(col2, '') IS NOT NULL;

June 10, 2016

SOAP vs REST




SOAP REST
SOAP is a protocol. REST is an architectural style.
SOAP stands for Simple Object Access Protocol. REST stands for REpresentational State Transfer.
SOAP can't use REST because it is a protocol. REST can use SOAP web services because it is a concept and can use any protocol like HTTP, SOAP.
SOAP uses services interfaces to expose the business logic. REST uses URI to expose business logic.
JAX-WS is the java API for SOAP web services. JAX-RS is the java API for RESTful web services.
SOAP defines standards to be strictly followed. REST does not define too much standards like SOAP.
SOAP requires more bandwidth and resource than REST. REST requires less bandwidth and resource than SOAP.
SOAP defines its own security. RESTful web services inherits security measures from the underlying transport.
SOAP permits XML data format only. REST permits different data format such as Plain text, HTML, XML, JSON etc.
SOAP is less preferred than REST. REST more preferred than SOAP.

http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

Cursor Sample

SQL Server

DECLARE @EMPLOYEE_ID AS VARCHAR (25);

DECLARE @EMPLOYEE_NAME AS VARCHAR (100);

DECLARE @EMAIL_ID AS VARCHAR (25);

DECLARE @getAccountID AS CURSOR;

SET @getAccountID = CURSOR
    FOR SELECT t.EMPLOYEE_ID,
               t.EMPLOYEE_NAME,
               t.EMAIL_ID
        FROM   [dbo].[EMPLOYEES_TIBCO] AS t
               INNER JOIN
               [dbo].[Vms_User_Details] AS v
               ON t.EMPLOYEE_ID = v.UserId
        WHERE  t.EMAIL_ID IS NOT NULL;

OPEN @getAccountID;

FETCH NEXT FROM @getAccountID INTO @EMPLOYEE_ID, @EMPLOYEE_NAME, @EMAIL_ID;

WHILE @@FETCH_STATUS = 0
    BEGIN
        UPDATE  [Vms_User_Details]
            SET UserName = @EMPLOYEE_NAME,
                E_Mail   = @EMAIL_ID
        WHERE   UserId = @EMPLOYEE_ID;
        FETCH NEXT FROM @getAccountID INTO @EMPLOYEE_ID, @EMPLOYEE_NAME, @EMAIL_ID;
    END

CLOSE @getAccountID;

DEALLOCATE @getAccountID;

T-SQL Code Formatter

Mobile compatibility test

Enter your sites url and Google gives a detailed report on its mobile friendliness.

June 9, 2016

ASP.NET MVC Terminology

http://www.danylkoweb.com/Blog/aspnet-mvc-terminology-EK
https://blog.udemy.com/asp-net-mvc-tutorial/
https://mycodelines.wordpress.com/2014/04/07/differences-between-asp-net-webforms-engine-and-razor-view-engine/
http://www.dotnet-tricks.com/Tutorial/mvclist
http://www.tutorialsteacher.com/mvc/asp.net-mvc-tutorials
http://csharppulse.blogspot.in/2013/08/mvc3-interview-questions-and-answers.html
http://tutlane.com/tutorial/aspnet-mvc/

  • MVC folder structure
  • Routing - Global.aspx and routing table, bundling
  • Model
  • Controller  - Single or Multiple Action Method
  • View
  • MVC Pipeline
  • HTML Helper - @, @:, @{ }, @HTML
  • Ajax Helper - AjaxOption class and Partial view
  • Razor syntax
  • Entity Framework
  • Security and deployment

Controllers
  • Action Method (Public)
  • Action Result (return type of action)
  • ActionSelector (NonAction,ActionName,ActionVerbs)
  • Filter (Authorization ,Action ,Result ,Exception )
  • Custom Action Fiter (OnActionExecuted,OnActionExecuting,OnResultExecuted,OnResultExecuting)
  • AttributeRouting
Model
  • DataAnnotation 
  • ModelState.Valid -Validation done at view
  • ModelState.AddModelError -Check the validation at DB level
  • Remote Validation
Pass data Controller to View
  • TempData
  • ViewBag
  • ViewData
  • ViewModels
  • Model

Pass Data to Controller from View
  • Model Binders (querystring,Requst[],FormCollection, Binding to complex data)
  • Binding Attribute
View (csaspx, vbaspx,etc)
  • Routing View Engine (@,@:,@{},@model,etc)
  • Html Helpers (@Html)
  • Partial View
  • Layouts, RenderBody, RenderSection and RenderPage

Display Modes : Display modes are used to return different Views based on a device, or Display Mode

Areas

Web API


Asp.Net MVC1

  1. Released on Mar 13, 2009
  2. Runs on .Net 3.5 and with Visual Studio 2008 & Visual Studio 2008 SP1
  3. MVC Pattern architecture with WebForm Engine
  4. Html Helpers
  5. Ajax helpers
  6. Routing
  7. Unit Testing

Asp.Net MVC2

  1. Released on Mar 10, 2010
  2. Runs on .Net 3.5, 4.0 and with Visual Studio 2008 & 2010
  3. Strongly typed HTML helpers means lambda expression based Html Helpers
  4. Templated Helpers
  5. Support for Data Annotations Attribute
  6. Client-side validation
  7. UI helpers with automatic scaffolding & customizable templates
  8. Attribute-based model validation on both client and server
  9. Overriding the HTTP Method Verb including GET, PUT, POST, and DELETE
  10. Areas for partitioning a large applications into modules
  11. Asynchronous controllers

Asp.Net MVC3

  1. Released on Jan 13, 2011
  2. Runs on .Net 4.0 and with Visual Studio 2010
  3. The Razor view engine
  4. Improved Support for Data Annotations
  5. Remote Validation
  6. Compare Attribute
  7. Sessionless Controller
  8. Child Action Output Caching
  9. Dependency Resolver
  10. Entity Framework Code First support
  11. Partial-page output caching
  12. ViewBag dynamic property for passing data from controller to view
  13. Global Action Filters
  14. Better JavaScript support with unobtrusive JavaScript, jQuery Validation, and JSON binding
  15. Use of NuGet to deliver software and manage dependencies throughout the platform
  16. Good Intellisense support for Razor into Visual Studio

Asp.Net MVC4

  1. Released on Aug 15, 2012
  2. Runs on .Net 4.0, 4.5 and with Visual Studio 2010SP1 & Visual Studio 2012
  3. ASP.NET Web API
  4. Enhancements to default project templates
  5. Mobile project template using jQuery Mobile
  6. Display Modes
  7. Task support for Asynchronous Controllers
  8. Bundling and minification
  9. Support for the Windows Azure SDK

Asp.Net MVC5

  1. Released on 17 October 2013
  2. Runs on .Net 4.5, 4.5.1 and with Visual Studio 2013
  3. One Asp.Net
  4. Asp.Net Identity
  5. ASP.NET Scaffolding
  6. Authentication filters - run prior to authorization filters in the ASP.NET MVC pipeline
  7. Bootstrap in the MVC template
  8. ASP.NET Web API2