23 May 2013

Difference between Asp.Net MVC and Web Forms


Difference between Asp.Net MVC and Web Forms:

Asp.Net Web Forms
Asp.Net MVC

Asp.Net Web Form follows a traditional event driven development model.
Asp.Net MVC is a lightweight and follow MVC (Model, View, and Controller) pattern based development model.
Asp.Net Web Form has server controls.
Asp.Net MVC has html helpers.
Asp.Net Web Form has state management (like as view state, session) techniques.
Asp.Net MVC has no automatic state management techniques.
Asp.Net Web Form has file-based URLs means file name exist in the URLs must have its physically existence.
Asp.Net MVC has route-based URLs means URLs are divided into controllers and actions and moreover it is based on controller not on physical file.
Asp.Net Web Form follows Web Forms Syntax
Asp.Net MVC follow customizable syntax (Razor as default)
In Asp.Net Web Form, Web Forms(ASPX) i.e. views are tightly coupled to Code behind(ASPX.CS) i.e. logic
In Asp.Net MVC, Views and logic are kept separately.
Asp.Net Web Form has Master Pages for consistent look and feels.
Asp.Net MVC has Layouts for consistent look and feels.
Asp.Net Web Form has User Controls for code re-usability.
Asp.Net MVC has Partial Views for code re-usability.
Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access.
Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing interactive web application with latest web standards.
Visual studio and Visual web developer (free) are tools for developing Asp.Net Web Forms.
Visual studio and Visual web developer (free) are tools for developing Asp.Net MVC application.
Asp.Net Web Form is not Open Source.
Asp.Net Web MVC is an Open Source.

22 May 2013

Sql Query to return name with First letter and Last letter is same

Sql Query to return name with First letter and Last letter is same

Consider below details are in the table - StudentTest



Below Query is used to return name with both First and Last letter is same


SELECT * FROM studenttest WHERE Substring([StudentName], 1, 1) =  Substring([StudentName],LEN(STUDENTNAME), LEN(STUDENTNAME))



13 May 2013

Delegates in C#


Delegates in C#

-Delegates are just function pointers, That is, they hold references to functions.

A Delegate is a class. When you create an instance of it, you pass in the function name (as a parameter for the delegate's constructor) to which this delegate will refer.

Use a delegate when
- An eventing design pattern is used.
Easy composition is desired.
It is desirable to encapsulate a static method.

Every delegate has a signature. For example:
Delegate int SomeDelegate(string s, bool b);

is a delegate declaration. When I say this delegate has a signature, I mean that it returns an int type and takes two parameters of type string and bool.

Consider the following function:

private int SomeFunction(string str, bool bln){...}
You can pass this function to SomeDelegate's constructor, because of their similar signatures.


SomeDelegate sd = new SomeDelegate(SomeFunction);

Now, sd refers to SomeFunction, in other words, SomeFunction is registered to sd. If you call sd, SomeFunction will be invoked. Keep in mind what I mean by registered functions. Later, we will refer to registered functions.


sd("somestring", true);

12 May 2013

Global.asax file in asp.net

Global.asax file in asp.net

The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level events raised by ASP.NET or by HttpModules. 

The Global.asax file is optional. If you do not define the file, the ASP.NET page framework assumes that you have not defined any application or session event handlers.


The Global.asax file itself is configured so that any direct URL request for it is automatically rejected; external users cannot download or view the code written within it.

There can be only one Global.asax file per application and it should be located in the application's root directory only.



The Global.asax contains two types of events those are
Events which are fired for every request
Events which are not fired for every request


Events which are fired for every request

Application_BeginRequest() – This event raised at the start of every request for the web application.


Application_AuthenticateRequest – This event rose just before the user credentials are authenticated. We can specify our own authentication logic here to provide custom authentication.


Application_AuthorizeRequest() – This event raised after successful completion of authentication with user’s credentials. This event is used to determine user permissions. You can use this method to give authorization rights to user. 


Application_ResolveRequestCache() – This event raised after completion of an authorization request and this event used in conjunction with output caching. With output caching, the rendered HTML of a page is reused without executing its code.


Application_AcquireRequestState() – This event raised just before session-specific data is retrieved for the client and is used to populate Session Collection for current request.


Application_PreRequestHandlerExecute() – This event called before the appropriate HTTP handler executes the request.


Application_PostRequestHandlerExecute() – This event called just after the request is handled by its appropriate HTTP handler.


Application_ReleaseRequestState() – This event raised when session specific information is about to serialized from the session collection.


Application_UpdateRequestCache() – This event raised just before information is added to output cache of the page.


Application_EndRequest() – This event raised at the end of each request right before the objects released. 


Now we will see


Events which are not fired for every request


Application_Start() – This event raised when the application starts up and application domain is created.


Session_Start() – This event raised for each time a new session begins, This is a good place to put code that is session-specific.


Application_Error() – This event raised whenever an unhandled exception occurs in the application. This provides an opportunity to implement generic application-wide error handling.


Session_End() – This event called when session of user ends.


Application_End() – This event raised just before when web application ends.


Application_Disposed() – This event fired after the web application is destroyed and this event is used to reclaim the memory it occupies.



5 May 2013

Class, Object

Class
Class defines the attributes and behavior used by all instance of class.
for ex
Class name : Car
Attributes [Properties] : Color, Speed 
Behavior [Methods] : Accelerate, Turn and Brake

No memory is allocated when class is created.
Default access specifier for class is Private.
Class is a reference type, so when call the class by creating the object memory is allocated on the heap.

Object
An Object is an instance of class.
Object have individual copies of attributes and share a common set of behavior.
for ex


Class name : Car
Attributes [Properties] : Color, Speed 
Behavior [Methods] : Accelerate, Turn and Brake



Object is creating using new keyword.

Inheritance, Association, Aggregation, and Composition


Inheritance, Association, Aggregation, and Composition
      1.  Inheritance [IS a relationship]
      2.  Association [Using Relationship]
      3. Aggregation [ Has a Relationship]
      4. Composition -  [Part of relationship]

1.      Inheritance [ IS a relationship]
Example
Consider there are two Classes.
1.Employee
2.Manager 
Here Employee is the Parent class; Manager is the child class.Manager is one of the types of Employee. Class Manager is a relation with Class Employee.
I.e. Manager is (a or an) Employee

2. Association [Using relationship]
Example
       1. Manager
       2. SwipeCard
Here Manager and SwipeCard are two different classes.
Manager uses a swipe card to enter XYZ premises.

I.e. Manager Using swipe card to enter XYZ premises.



The above diagram shows how the SwipeCard class uses the Manager class and the Manager class uses the SwipeCard class. You can also see how we can create objects of the Manager class and SwipeCard class independently and they can have their own object life time.
This relationship is called the “Association” relationship.

3.      Aggregation [ Has a Relationship]

Aggregation gives us a 'has-a' relationship. Within aggregation, the lifetime of the part is not managed by the whole. 
The above UML describes Aggregation relation which mentions that Employee refers to Address. And life time of Address is not managed by Employee. It is like"Employee has a Address". Let us see how it can be implemented in C#.



So how do we express the concept of aggregation in C#? Well, it's a little different to composition. Consider the following code:
public class Address
{
 . . .
}
public class Person
{
     private Address address;
     public Person(Address address)
     {
         this.address = address;
     }
     . . .
}
Person would then be used as follows:
Address address = new Address ();
Person person = new Person(address);
    Or
Person person = new Person (new Address() );

4. Composition  -  [Part of relationship]
Composition gives us a 'part-of' relationship.

If we were going to model a car, it would make sense to say that an engine is part-of a car. Within composition, the lifetime of the part (Engine) is managed by the whole (Car), in other words, when Car is destroyed, Engine is destroyed along with it. So how do we express this in C#?
public class Engine
{
 . . .
 
}
public class Car
{
    Engine e = new Engine();
    .......
}
As you can see in the example code above, Car manages the lifetime of Engine.




Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...