Saturday, August 24, 2013

WCF


WCF

Before learning wcf we need to know

Distributed Programing
Communication between multiple server apps or services in the network.
Must be capable to run behind Firewall.
Some share common protocol but not a common platform.

When communication between different platforms than
Heterogeneous Access-àUniversal Access
Performance->Low
Security->Low



When communication between same platform
Heterogeneous Access-àIt is access only through .Net to .Net
Performance->High
Security->High

.Net support for developing Distributed Program
All the distributed technologies speak about the same i.e. consuming of libraries present on remote machines.

We have three concepts
1 Remoting                   
2 Web Services
3 WCF

.NET Remoting
It provides proprietary type of communication.
.NET to .NET

Remoting offers distributed programming between two dot net applications
It is fixed architecture where different customization is provided for development.

In .Net libraries were implemented as Assemblies, where we can consume an assembly residing on local machine by adding its reference. We can now consume Assemblies residing on remote machines also using R emoting.

Architecture





P1 makes a request to a component called Proxy.


Now proxy will take the request with the help of formatter to convert this local call to remote calls. After converting the channel will take & transfer to the remote destination.






Developing a Remoting Application:

-To develop a Remoting Application first we need to understand various things regarding the process of communication, those are:

1. How does both the parties (client and server) exchange information between each other?
Ans: To exchange information between both the parties they make use of a process known as Serialization and Deserialization.

-As applications represent the data in High Level (Object Format) which are not free flow able, needs to be converted into Low Level (Binary or Text) and then transferred to the other system where on the target machine Low Level data has to be converted back into High Level.


-Serializationis a process of converting high level data to low level and De-Serialization is in opposite of serialization that converts low level data to high level.






-To perform serialization & deserialization remoting provides Formatter Classes, those are:

            -Binary Formatters
               -TCPServerChannel
               -TCPClientChannel
            -Soap Formatters
               -HttpServerChannel
               -HttpClientChannel

-Binary Formatters are used for binary serialization and deserialization & Soap Formatters are used for text serialization and deserialization.

Advantages
Any distributed programming developed with remoting libraries will get
Good Performance
Good Security

Drawbacks:
Heterogeneous Environment is not possible with remoting Marsh

2 Web Services
It follows open standards XML Web services.
Firewall Friendly.
.Net to Non .Net Distributed App can be developed.
It is an application which is design as collection of services and can take Http Request process it and gives response as Http Response.
Web Service will only have one formatter that is SOAP
Channel->Http


Architecture



WCF

Unified framework for rapidly building service oriented apps. It is the new way of easily exposing the components.


Architecture



===========================================================

Rules for Developing WCF Service

1       Creating a .Net Component which is compatible to WCF(WCF Runtime Enabled)
To do this WCF provides different classes in the form of attributes, so that we can attach them to our component content. All the libraries and WCF runtime is provided in
System.ServiceModel.dll
System.ServiceModel Namespace
Note: the above .dll is only for the WCF Framework which contains libraries and WCF Runtime.
WCF Recommends interface pattern of development for its service & highly recommended even for normal component development.
Eg:
      [Service Contract]
      Interface IDemo
{
                  //
}
Here [Service Contract] attributes tells us it is a WCF Enabled Component.
Here we are using [Service Contract] attribute because to make .Net Component Distributed.

 [Operation Contract]

To specify methods of service Contract in Service Consumption.
A good service should not have more than 12 [Operation Contract] attributes
We should have at least one [Operation Contract]

2       Every service requires hosting so that it can manage the operating system and other environment related issues.

WCF provides rich hosting options so that user can plan service where good management capabilities
WCF provides the following hosting options

Self-Hosting
Windows Service Hosting
IIS Hosting
Windows Activation Services Hosting [WAS]

3       Endpoints for accessing the service

In WCF Service is exposed to client using end points. Every Client understands as well as communicates with WCF service using Endpoints only

1 Address
 Here we specify the location where our service is available. WCF Follows industry standard addressing pattern, so while writing address instead of .Net style of addressing we have to write W3 style of addressing.

2 Binding
Binding defines how to communicate with service. It includes the Formatter, Channel and other information with just the binding name that we specify.
            For Eg:
Basic Http Binding means SOAP formatter of communication using http channel
            BasicTcpBinding means Binary formatter of communication using Tcp Channel.

3 Contract
This is .Net Type that we provide for client for Request and Response.
Contract -àInterface
WCF will take that interface and give to client.

What is End Point?
It is structure defined with Address+Binding+Contract so that user can communicate with service using this end point information.

Here we are telling that this service should be exposed to client by using those end points

                      

Saturday, August 17, 2013

MVC Controller

MVC Controller

A controller is responsible for controlling the way that a user interacts with an MVC application.
                
A controller determines what response to send back to a user when a user makes a browser request.

·         It is Just a class that implements IController

·         Use the View Method to display the View.

·         Public methods are the action methods

Explanation: In MVC, we write the code in service oriented pattern. Anything you write as service oriented, every method you write in controller should be public, globally accessed because we will have multiple layers.


·         Apply [NonAction] attribute to turn this off
Explanation:
[NonAction]
Public void Add ()
This method will be public to your project, but not accessible to other project.



·         Invoking via routing mechanisms(**********)
Explanation: MVC says we will have friendly Uri
Uri—uniform resource identifier
Today it is not URL, today they are URI



What is routing:
Is the process where requested URL is mapped to MVC Controller method resulting in the simple URL’S to invoke methods
Aspx request—handler->aspx engine
Webserveràhandleràextensions
Webserver are based on handler and handlers are based on extensions
Routing will do the mapping to webserver understandable format.

How mapping is performed
Based on routing instructions that the developers provide .
In every mvc project ,that we create by default following route instructions is added
Note: By default one route instruction is added.

Where is the route mapping code written?

The route mapping code is written in the “global.asax” file.


Why Routing Instructions
Because mvc works with routing, so we have one routing instruction by default.
Local hostà routing
Is the mediator between webserver handler and URL

For example, consider the following URL:  /Job/Details/3
• This URL is parsed into three parts like this:
Controller = Job Controller
Action = Details & Id = 3
The default route includes default values for all three segments. The default Controller is Home Controller,
The default Action is Index,
And the default Id is an empty string.

Controllers
Core Action Functionality

What are the different types of results in MVC?

In MVC, we will not have the return types as string, int, etc.

Actions methods should return an Action Result
• ViewResult if you want to show a View
• ContentResult directly returning content
• RedirectResult to redirect to a new URL
• RedirectToRouteResult redirect to route
• EmptyResult null object pattern
• JsonResult returning a Json
JavaScriptResult - script that can be executed on the client

--------------------------------------------------------------------------------------------------------
1.    FileContentResult - Returns a file to the client
2.    FileStreamResult - Returns a file to the client, which is provided by a Stream
3.    FilePathResult - Returns a file to the client


Routing

MVC gives you great control over how URLs are mapped to your controllers. It gives you the ability to define your URLs in a human readable SEO (Search Engine Optimization) friendly fashion, to remap old URLs to new functionality and side-by-side utilize classic ASP.NET sites inside of MVC3. It also results in hiding what kind of page user is calling and what environment we are working in. Most of the new websites are following this and it is important to understand that routing is not URL rewriting as routing will have customization's and many attachments towards request/response.

When we create any type of MVC application by default GLOBAL.ASAX file is created becoz ASP.NET implements MVC using this global application class mainly. Routes defined in the Global.asax.cs file of our MVC3 web application/site. In this global.asax file most important element relative to our work is RegisterRoutes method. By default, there is only one route defined in the RegisterRoutes method that looks like the line below.




Custom MVC Routes

 In a ASP.NET site for a magazine, you might have a URL that looks like www.internet.com/ViewArticle.aspx?id=123. This URL passes the ID number of the article to view, but the URL itself doesn't describe the content in any human readable way

 If the URL instead was www.internet.com/MVC_Routing/123 a human-or a web crawler-could read that and know that the article is about MVC_ Routing
Defination of Routing :
It is a program or process which is responsible to map the given URL to a controller and its action method.
Explanation: Is the process where user will type the URL which will be converted to handlers and there will be the mediator who will take this Uri and map to the server.
Now product ID for what(tv,mobile,etc)
In mvc we can define as
Initially routing was provided in MVC today available in entire .Net.
This was introduced because of the way ASP.NET MVC works rather, than being handled by a page handler each request needs to be routed to a specific action method of a specific controller Class.

Routing provides "clean" URLs
URL is mapped to a route handler
Extra level of indirection
Handlers can be changed without impacting URL
URL can be changed without impacting handler

URL Example Example http://www.mysite.com/Home/ProductList 



Note




Catch-all route
Put this route last
It will match any URL which has not matched any other route


============================

Thursday, August 15, 2013

MVC Annotations and Validations

[Required]
A value must be provided.

[Range]
Eg: 1-10


[Regular Expression]
Value must specify a regular expression.

[StringLength]
Value must be a min length and less than a max length

[Compare]
Value must equal another property

[Remote]
Value is validated client side with a JSON call to the Server.

Eg: The user name Available.

[Extensible]
Specify your own custom Validations


A namespace has to be added( System.ComponentModel.Data.Annotations)


Examples:

Prod.cs





we will see how to create custom validations by implementing the ValidationAttribute class or IValidatableObject interface.

Validation Attributes class will make our class as Annotations.



To enable client-side validation we have to do couple of things. First we have to make sure the ClientValidationEnabled and UnObtrusiveJavaScriptEnabled are set to true in web.config.

<appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />

  </appSettings>


The next thing is include the necessary JavaScript files in the layout page.


There are some cases where we need to do basic validations. In some cases we need to apply validations to class properties that are not supported by the built-in validation attributes. For example in our application we need price to be validated as
Price<minPrice || price>maxPrice

Example 1



Validation attributes can be applied to models as well, if the custom validation attribute is applied to a model then the complete model itself will be passed as value parameter to the IsValid methods. When the custom validation depends upon more than one property of a class then we can apply the attribute to the class or model itself.

In our scenario the Start Date should be a future date and it is independent of other properties so let's implement the first IsValid method. The implementation is quite simple as shown below
Eg2:
Public class PriceValidationAttribute:ValidationAttribute
{
Private decimal minPrice=10;
Private decimal maxprice=100.00

Public override bool IsValid(object value)
{
Decimal price=(decimal)value;
if (price<this.minprice || price>this.maxPrice)
return false;
}


Understanding


The custom validation we have applied to the Party model is done only at the server side and how we can do that in the client-side also? The ASP.NET MVC team understands this problem and has come up with a solution for that. The solution is we have to implement an interface called IClientValidatable in our custom attribute class to enable client-side validation. The interface contains a single method named GetClientValidationRules that returns a collection of ModelClientValidationRule instances.
public classFutureDateValidatorAttribute : ValidationAttribute, IClientValidatable
{
    public override bool IsValid(object value)
    {
        return value != null && (DateTime)value > DateTime.Now;
    }

    public IEnumerable<ModelClientValidationRule>
           GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {        
            ErrorMessage = ErrorMessage,
            ValidationType = "futuredate"
        };
    }
}



In the implementation we are just returning a single instance setting the error message and "futuredate" for theValidationType. We can use any other name instead of "futuredate" for ValidationType.
Implementing only this interface not completely solves the problem! we have to do couple of things more. We have to create a jQuery validator and adapter. In the validator we write the logic to evaluate the StartDate is a future date or not and in the adapter we set the error message that has to be displayed when the validation fails.
 
jQuery.validator.addMethod('futuredate', function (value, element, params) {
    if (!/Invalid|NaN/.test(new Date(value))) {
        return new Date(value) > new Date();
    }
    return isNaN(value) && isNaN($(params).val()) || (parseFloat(value) > parseFloat($(params).val()));
}, '');
 
jQuery.validator.unobtrusive.adapters.add('futuredate', {}, function (options) {
    options.rules['futuredate'] = true;
    options.messages['futuredate'] = options.message;
});


We have successfully created a custom validation by implementing the ValidationAttribute and IClientValidatable to perform the validation at both the client and server side.


IValidatableObject

The MVC framework provides another way to do custom validations using IValidatableObject. Unlike theValidationAttribute the IValidatableObject is implemented in the model class itself. TheIValidatableObject contains a single method called Validate that returns a collection of ValidationResultinstances.

public interface IValidatableObject
{    
    IEnumerable<ValidationResult> Validate(ValidationContext validationContext);
}
The important differences between the ValidationAttribute and IValidatableObject are: the former one is used to perform a single validation while the later one is used to perform single or multiple validations. If we want the validation to happen both at the server-side and at the client-side then the ValidationAttribute is the right choice. If we want the validations should happen only at the server-side then  IValidatableObject is the right choice. The IClientValidatable only supports ValidationAttribute for client-side validations and notIValidatableObject
ValidationAttribute is used to perform a single validation. IValidatableObject can be used to do multiple validations.
The MVC framework provides another way to do custom validations using IValidatableObject. Unlike theValidationAttribute the IValidatableObject is implemented in the model class itself. TheIValidatableObject contains a single method called Validate that returns a collection of ValidationResultinstances.

public interface IValidatableObject
{    
    IEnumerable<ValidationResult> Validate(ValidationContext validationContext);
}
The important differences between the ValidationAttribute and IValidatableObject are: the former one is used to perform a single validation while the later one is used to perform single or multiple validations. If we want the validation to happen both at the server-side and at the client-side then the ValidationAttribute is the right choice. If we want the validations should happen only at the server-side then  IValidatableObject is the right choice. The IClientValidatable only supports ValidationAttribute for client-side validations and notIValidatableObject

ValidationAttribute is used to perform a single validation. IValidatableObject can be used to do multiple validations.

We are going to add two more validations to our Party class. The party provider doesn't allow the party to continue after 10 PM (bad!) and they don't server drinks if the NoOfJoinees is less than 5 (too bad!). If you see these validations they are pretty much tied to the business and they can't be reusable across classes, so the best way to go is IValidatableObject approach. The other thing is we can do a set of validations using this approach.
Below listing shows the implementation.

public class Party : IValidatableObject
{
[Required(ErrorMessage = "Start date is required")]
[FutureDateValidator(ErrorMessage = "Start date should be a future date")]
public DateTime StartDate { get; set; }

[Required(ErrorMessage = "Duration is required")]
public int DurationInHours { get; set; }

[Required(ErrorMessage = "No. of joinees is required")]
[Range(2, 10, ErrorMessage = "No. of joinees should be minimum 2 and not more than 10")]
public int NoOfJoinees { get; set; }

public bool Drinks { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (StartDate.TimeOfDay > new TimeSpan(22 - DurationInHours, 0, 0))
{
yield return new ValidationResult("The party should not exceed after 10.00 PM");
}

if (NoOfJoinees < 5 && Drinks)
{
yield return new ValidationResult("Drinks are only allowed if no. of joinees is 5 or more.");
}
}
}





Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...