Hello All, We are going to start new batch from next week. message/call or mail us for more details.

4 September 2012

Advantages and disadvantages of LINQ over Stored procedures


Below is the three advantages of LINQ over stored procedures. 

Debugging - As debug point concern, as LINQ is part of .NET, we can use the visual studio's debugger to debug the queries but it is tough to debug the Stored procedure as it will not support the visual studio debugger. 

Deployment - In case of deployment, we need to provide an additional script for stored procedures to execute but in case of LINQ, it will complie into single DLL hence deployment becomes easier. 

Type Safety - As LINQ is type safe, the queries errors are type checked at compile time. Better suggest to use LINQ because it helps to encounter an error at the compile time rather than at runtime exception.


The disadvantage with LINQ is, it is not a precompiled statement where as stored procedures are precompiled. In case of LINQ the queries need to be compile before the execution. So according to this, I can say stored procedures are faster in performance as compared to LINQ.

*********************************************************************************


Linq Advantage and Disadvantage
LINQ (Language Integrated Query) is a Microsoft programming model and methodology that gives a formal query capabilities into Microsoft .NET-based programming languages.

It offers a compact, expressive, and intelligible syntax for manipulating data. The real value of LINQ comes is to apply the same query to an SQL database, a DataSet, an array of objects in memory and to many other types of data as well. It requires the presence of specific language extensions.

LINQ may be used to access all types of data, whereas embedded SQL is limited to addressing only databases that can handle SQL queries.

Below are the advantages and Disadvantages of LINQ


Advantages:
 It is a cleaner and typesafety.
It is checked by the compiler instead of at runtime
It can be debugged easily.
 It can be used against any datatype - it isn't limited to relational databases, you can also use it against XML, regular objects.
It can able to query not just tables in a relational database but also text files and XML files.

Disadvantages:

LINQ sends the entire query to the DB hence takes much network traffic but the stored procedures sends only argument and the stored procedure name. It will become really bad if the queries are very complex. 
Preformance is degraded if we don't write the linq query correctly.
If you need to make changes to the way you do data access, you need to recompile, version, and redeploy your assembly.


*********************************************************************************

What are the advantages of LINQ in n-tier applications ?



Advantages
1. Quick turn around for development
2. Queries can be dynamically
3. Tables are automatically created into class
4. Columns are automatically created into properties
5. Relationship are automatically appeaded to classes
6. Lambda expressions are awesome
7. Data is easy to setup and use
 
Disadvantages
1. No clear outline for Tiers
2. No good way of view permissions
3. Small data sets will take longer to build the query than execute
4. There is an overhead for creating queries
5. When queries are moved from sql to application side, joins are very slow
DBML concurrency issues
6. Hard to understand advance queries using Expressions
 



Working with ASP.NET Validation controls


I will examine about the naming conventions which have to be followed while working with ASP.NET and also about Validation controls. You can either use Visual Web Developer Express Edition or Visual Studio 2005 to learn the topics covered in this article.

Naming Conventions

You have to follow certain general principles while working with every programming language. This is true with ASP.NET as well. Each data type and WebForm controls are mentioned by their respective prefixes. It would be better to follow this convention so that it would be easy to recognize the code at a later point of time. The table given below shows a list of commonly used WebForm controls and their standard prefixes. 

Table - 1: Naming Conventions
WebForm ControlsPrefixWebForm ControlsPrefix
ButtonbtnRadioButtonrad
CheckBox
chk
RadioButtonList
radl
CheckBoxList
chkl
TextBox
txt
DataGrid
dgrd
RequiredFieldValidator
valr
DataList
dlst
CompareValidator
valc
DrpDownList
drop
RangeValidator
valg
Image
img
RegularExpressionValidator
vale
ImageButton
ibtn
CustomValidator
valx
Label
lbl
ValidationSummary
vals
LinkButton
lbtn
Table
tbl
ListBox
lst
Calendar
cal
Validation controls
One of the main tasks for every web developer is to check for errors while developing web pages. According to the server side terminology, this concept is called as Validation. Prior to ASP.NET, this task was achieved by using JavaScript and VBScript. JavaScript was commonly used by many developers and the main disadvantage of this is that developers had to write lot of code. ASP.NET simplified this task by providing us with built-in WebForm controls. With these controls you can force the user to input correct entry into the appropriate fields. You can also program in such a way that the user enters the URL in correct format. The .NET Framework 2.0 provides us with the validation controls for comparing the values between two text fields, comparing between specific ranges and a control for writing custom codes. There are six controls which are available for performing validation. They are
  1. RequiredFieldValidator
  2. CompareValidator
  3. RegularExpressionValidator
  4. RangeValidator
  5. CustomValidator
  6. ValidationSummary
Let us discuss each one of them in detail

RequiredFieldValidator
This is one of the important controls used in majority of web applications. If a user fails to enter the required information on the form then the message given on this validation control will appear thereby forcing the user to fill up the required information. The code given below will illustrate this concept in detail
<p>Name: <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<
asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter your name" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</
p>
<
p>Age:&nbsp;&nbsp;&nbsp; <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<
asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter your age" ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
I have placed two textboxes and the required number of RequiredFieldValidator controls. The message given along with the ErrorMessage property will be displayed if the user doesn't enters any data on to the relevant text boxes (See the Figure given below)
      Figure 1
      You should set ControlToValidate property to the appropriate WebForm control in addition to usual properties such as ID. You can check out the functioning of more properties by navigating the properties box using Visual Studio 2005. 

      CompareValidator
      With the help of this control, you can compare the value of one control with the value of another control. This control has a unique set of properties and it has been explored in the code given below
      <asp:Label id="Label1" runat="server">Enter Password</asp:Label>
      <
      asp:TextBox id="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
      <asp:Label id="Label2" runat="server">Enter Password again</asp:Label> <asp:TextBox id="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
      <asp:CompareValidator id="CompareValidator1" runat="server" ErrorMessage="Please verify password again" ControlToValidate="TextBox2" ControlToCompare="TextBox1"></asp:CompareValidator>
      <
      asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
      I have placed two textboxes and a CompareValidator control on the WebForm. The code will verify the password entered by the user and it should be equal. If it is not equal then the message inside the ErrorMessage property will be displayed. You may notice the two core properties such as ControlToValidate and ControlToCompare. The above validation control has another important property - Operator. This property has got seven values such as Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual and DataTypeCheck.

      Let us now examine another usage of this control. This example will compare the two values and one of the values should be greater than the second one.
      Enter First Number <asp:TextBox id="TextBox3" runat="server"></asp:TextBox>Enter Second Number <asp:TextBox id="TextBox4" runat="server"></asp:TextBox>
      <
      asp:CompareValidator id="CompareValidator2" runat="server" ErrorMessage="First Number should be greater than the second one" ControlToValidate="TextBox4" ControlToCompare="TextBox3"Operator="GreaterThan"></asp:CompareValidator>
      <
      asp:Button id="Button2" runat="server" Text="Button"></asp:Button>
      You can use these values in a wide range of applications at different scenarios. I would suggest you to refer to the MSDN documentation for more detailed coverage regarding the usage of these property values.

      RegularExpressionValidator
      This control is used to verify the accuracy of strings using special characters. It will check for the specific pattern which should match with the code. You have to specify a regular expression using ValidationExpression property. The usage of this control is little complicated as compared to the above discussed controls. For example, you can verify whether a user has entered @ while inputting their e-mail address. It would be good if you have Visual Studio .NET 2003 to work with this control as it will automatically fulfill the coding part. The development environment comes with a Regular Expression Editor which will give you the corresponding code for various tasks such as expressions for Internet E-mail Address, Internet URL, US Zip Code and much more (See the figure given below)

      Figure 2
      The code given below shows some of the usage of this control

      Internet E-mail Address
      <asp:RegularExpressionValidator id="RegularExpressionValidator1" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 120px" runat="server" ErrorMessage="RegularExpressionValidator"ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
      Internet URL
      <asp:RegularExpressionValidator id="RegularExpressionValidator1" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 120px" runat="server" ErrorMessage="RegularExpressionValidator"ValidationExpression="http://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?"></asp:RegularExpressionValidator>
      US Phone Number
      <asp:RegularExpressionValidator id="RegularExpressionValidator1" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 120px" runat="server" ErrorMessage="RegularExpressionValidator"ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"></asp:RegularExpressionValidator>
      US Social Security Number
      <asp:RegularExpressionValidator id="RegularExpressionValidator1" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 120px" runat="server" ErrorMessage="RegularExpressionValidator"ValidationExpression="\d{3}-\d{2}-\d{4}"></asp:RegularExpressionValidator>
      You may notice that the value of ValidationExpression property is little complicated and it is not at all necessary to remember these codes. Visual Studio .NET provides code for various scenarios. You can also write your own regular expressions if you have good knowledge about REGEX. 

      RangeValidator
      With the help of this control, you can check whether users have entered a value within a specific range as specified. You can specify the range values using MinimumValue and MaximumValue properties.
      <p>
      <
      asp:Label id="Label1" runat="server">Enter your age (1-120)</asp:Label>&nbsp;<asp:TextBoxid="TextBox1" runat="server"></asp:TextBox>
      </
      p>
      <
      p>
      <
      asp:RangeValidator id="RangeValidator1" runat="server" ErrorMessage="Please enter the correct age" ControlToValidate="TextBox1" MinimumValue="1" MaximumValue="120" Display="Dynamic"></asp:RangeValidator>
      </
      p>
      <
      p>
      <
      asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
      The figure given below shows the final output of the above program. You may notice that the error message will be displayed if you enter a value outside the specified range.

      Figure 3
      CustomValidator
      The Validation controls that you have seen above are built-in within the .NET Framework. You have to apply them as such by utilizing its properties in your ASP.NET application. With the help of CustomValidator control, you can write your own validation rules and apply them on the appropriate WebForm control. A complete discussion regarding this control is beyond the scope of this article. Please refer to the MSDN documentation for additional information.

      ValidationSummary
      This control collects all the values from the ErrorMessage property and displays them on ASP.NET page. The data will be presented either by directly on the page or inside a MessageBox. The code given below shows the usage of this control in different ways

      ValidationSummary as BulletList
      Name <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
      <
      asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Name"ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
      </
      p>
      <
      p>Age <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
      <
      asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="Enter Age"ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
      </
      p>
      <
      p>
      <
      asp:ValidationSummary id="ValidationSummary1" runat="server"></asp:ValidationSummary>
      </
      p>
      <
      p>
      <
      asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
      ValidationSummary as MessageBox
      Name <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
      <
      asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Name"ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
      </
      p>
      <
      p>Age <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
      <
      asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="Enter Age"ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
      </
      p>
      <
      p>
      <
      asp:ValidationSummary id="ValidationSummary1" runat="server" ShowMessageBox="True"></asp:ValidationSummary>
      </
      p>
      <
      p>
      <
      asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
      Summary
      In this article, you have learned how to work with different validation controls included with ASP.NET 2.0. You will learn more about these controls only if you play with them.