April 2016

Most important topics

Static Constructor

  • A static constructor is used to initialize the static data once or performed an action to once only.
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • User has no control when static constructor executed. In real life scenario static constructor use when class write the log file.

Is MVC Design Pattern or Architectural Pattern?
MVC is an architectural pattern. ASP.NET MVC is a part of ASP.NET Web application framework.

Caching
Caching is a use to improve performance of website by storing frequently access data and reusing that data.
Advantages: Reduce hosting server round trip. Reduce database server round trips. Reduce network traffic.
Disadvantages: Avoid caching if data unique per user. Avoid caching if database server has not enough RAM. For caching of dynamic contents that change frequently, define a short cache–expiration time rather than disabling caching.

Delegates
A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.

Difference between Web Services, WCF and Web API
Web Services

  • It supports only http protocol.
  • It is based on SOAP and return data in XML format only
  • It can be host only on IIS

WCF

  • It supports http, https, tcp, MSMQ protocol.
  • It is based on SOAP and return data in XML format only
  • It can be hosted with in the applicaion or on IIS or using window service
  • WCF Rest support XML, JSON and ATOM data format.

Web API

  • It supports http protocol.
  • It supports MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
  • It can be hosted with in the application or on IIS.
  • WCF Rest support XML, JSON data format.

Components of WCF
There are three main components in WCF.

  • Service Class
  • Hosting Environment
  • End Point

Service Class
Service Contract attribute define saying which application interface will be exposed as a service.

[ServiceContract()]
public interface IServiceGetResult
{
}

Operation Contract
Operation Contract dictates which methods should be exposed to the external client using this service.

[ServiceContract()]
public interface IServiceGetResult
{
[OperationContract()]
decimal GetPercentage(Student model);
}

Data Contract
Data Contract attributes defines which type of complex data will be exchanged between the client and the service. They determine which parameters to be serialized. When you are using simple data types like int, it is not necessary that you need to mark the data contract attribute. Because you will always find matching types on the client. However, complex structure like one shown in the below code snippet you will need to define a data contract. Remember data contract define how this data will be passed during transmission. In short data contract attribute define how data will be serialized will transmission.

As data contract are all about serialization you need to import System.Runtime.Serialization name space.
[DataContract]
public class Student
{
int StudentId {get;set;}
string GrNo {get;set;}
string Name {get;set;}
}
public class serviceGetResult: IServiceGetResult
{
public decimal GetPercentage(Student model)
{
decimal percentage=100;
//get percentage stored procedure call or calculation here.
return percentage;
}
}

Hosting Environment
Self-hosting the service in his own application domain. The service comes in to existence when you create the object of Service Host class and the service closes when you call the Close of the Service Host class.

Host in application domain or process provided by IIS Server.

Host in Application domain and process provided by WAS (Windows Activation Service) Server.

End Points
WCF offers its services to its client using an endpoint. An endpoint comprises of three key elements, the address, binding and contract.

WCF endpoints provide the necessary resources and directions, which help clients to communicate with the various services WCF offers.

We commonly refer the three elements (attributes) as ABC.

01) A stands for Address
02) B stands for Binding
03) C stands for Contract

UK Postcode Validation

Regular expression for JavaScript

var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;

var result = regPostcode.test(SearchTerm);

if (!result) {
alert(“Please enter valid postcode”);
return false;
}

Regular expression for C#

string regPostcode = “([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}”;

System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(regPostcode);

if(!regx.IsMatch(address0.PostalCode))
{
results.Add(new ValidationResult(String.Format(“Please enter valid postcode for {0}.”, addressRank)));
}

You can check the regular expression at http://www.regexplanet.com/

Postal code format and sample postcodes are as below.

Format Example
A9 9AA S1 1AA
A99 9AA M60 1NW
AA9 9AA CR2 6XH
AA99 9AA DN55 1PT
A9A 9AA W1A 1HQ
AA9A 9AA EC1M 1BB