Wednesday, February 17, 2010

Sql Server Questions and Answers - Constraints

1) What are constraints?
A constraint is a property assigned to a column or the set of columns in a table that prevents certain types of inconsistent data values from being placed in the column(s). Constraints are used to enforce the data integrity.

2)What are the different types of data integrity?
The following categories of the data integrity exist:
• Entity Integrity
• Domain Integrity
• Referential integrity
• User-Defined Integrity

Entity Integrity ensures that there are no duplicate rows in a table.
Domain Integrity enforces valid entries for a given column by restricting the type, the format, or the range of possible values.
Referential integrity ensures that rows cannot be deleted, which are used by other records (for example, corresponding data values between tables will be vital).
User-Defined Integrity enforces some specific business rules that do not fall into entity, domain, or referential integrity categories.

3)What are the different types of constraints available in sql server
❑ PRIMARY KEY constraints
❑ FOREIGN KEY constraints
❑ UNIQUE constraints (also known as alternate keys)
❑ CHECK constraints
❑ DEFAULT constraints
❑ Rules

4)What is a Primary Key?
Primary keys are the unique identifiers for each row. They must contain unique values (and hence cannot
be NULL). Because of their importance in relational databases, primary keys are the most fundamental of
all keys and constraints.

5)How many types of primary keys exist?
There are two types of Primary Keys:
1.1) Simple Primary Key ==> Defining primary key on a single column is called Simple Primary Key.
1.2) Composite Primary Key==> Defining Primary Key on more than one column is called Composite Primary Key.

6)What is an unquie key? Name the different types of Unique keys?
A Unique Key in a table uniquely identifies each and every row and allowing Nulls per column Combination. There are two types of Unqiue Keys:
1.1) Simple Unique Key ==> Defining Unique key on a single column is called Simple Unique Key.
1.2) Composite Unique Key==> Defining Unique Key on more than one column is called Composite Unique Key.

7) What is Check Constraint?
Check constraint specifies a condition that is enforced for each row of the table on which the constraint is defined. Once constraint is defined, insert or update to the data within the tables is checked against the defined constraint.

8) What is Rule?
A rule specifies the acceptable values that can be inserted into a column. This is similar to CHECK constraint.

9)What is foreign Key?
Foreign keys are both a method of ensuring data integrity and a manifestation of the relationships between tables. When you add a foreign key to a table, you are creating a dependency between the table for which you define the foreign key (the referencing table) and the table your foreign key references (the referenced table).

10)What is candidate key?
A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.

11) What is the difference between Primary Key and Unique Key?
1)Primary Key by definition cannot be null, where as unique key can accept null values but if the unique key is defined on a column which is not null , then this unique key can also be used as an alternate primary key functionality to identify unique rows in a table.
2)By definition you can have only one primary key defined on a table where as you can have multiple unique keys defined on a table
3)Also by default Primary key is created as clustured index and unique key is created as non clustered index

12)What is the difference between RULE and Check Constraint?The major difference between rule and Check is reusability. Check constraint is associated with columns in a Table. So these can't be re-used. Rules are defined with in a database and can be applied to any number of columns.

13)How to create a relationship between two tables? Using Foreign Keys. Create a Foreign Key on child table referencing Unique Key or Primary key of Parent table.

14)What is the table name,that contains the Primary Key, Unique Key and Foreign Key Information?
INFORMATION_SCHEMA.TABLE_CONSTRAINTS, where CONSTRAINT_TYPE column stores the information of Constraint Type.

15)Can we create a Foreign Key with out Primary Key?
Yes. If the table has Unique Key then it is posible to create a Foreign key constraint.

16) Can we have RULE and Check Constraint on the same column?YES

17) Can we apply Integrity Constraints on Computed Columns?
YES

18) Can you drop a Parent Table with out affecting its child tables?
No. First you need to drop all the Foreign Key relationships and then only you can drop Parent Table.

19) How to disable and Enable the constraints?
You need to use ALTER TABLE statement to disable constraint.
ex: ALTER TABLE ACCOUNT NOCHECK CONSTRAINT CHECK_IN_AMOUNT;

20) What is the order of Constraints execution?
There is no predefined order. All the constraints on the column gets executed.

21)Name the data integrity enforced by the sql server constraints?
1)The primary key constraints are used to enforce entity integrity.
2)The unique key constraints are used to enforce entity integrity as the primary key constraints
3)The check constraints are used to enforce domain integrity.
4)The not null constraints are used to enforce domain integrity, as the check constraints.

Questions on Global.asax

1)What is main difference between Global.asax and Web.Config?
ASP.NET uses the global.asax to establish any global objects that your Web application uses. The .asax extension denotes an application file rather than .aspx for a page file. Each ASP.NET application can contain at most one global.asax file. The file is compiled on the first page hit to your Web application. ASP.NET is also configured so that any attempts to browse to the global.asax page directly are rejected. However, you can specify application-wide settings in the web.config file. The web.config is an XML-formatted text file that resides in the Web site’s root directory. Through Web.config you can specify settings like custom 404 error pages, authentication and authorization settings for the Web site, compilation options for the ASP.NET Web pages, if tracing should be enabled, etc.

2)What does Global.asax file do?
The Global.asax file is an optional file that contains code for responding to
application-level events raised by ASP.NET or by HTTP modules.

3)The Global.asax is code-inside with default.How to change Global.asax to code-behind?
In global.asax you need to modify the @ Application directive.
<%@ Application Codebehind="Global.asax.cs" Inherits="YourNamespace.Global" %>
Then create your .asax.cs file, and add a class named Global derived from System.Web.HttpApplication.

4)How is the global.asax file different from an ASP global.asa file? The global.asax file is very similar to the global.asa file in classic ASP."You can add a global.asax file to the root of each Web application on your site."The global.asax supports the familiar Application_Start, Application_End, Session_Start, and Session_End events."In addition, ASP.NET adds support for several new events. Please see Microsoft's .NET documentation for more information.

5)List the event handlers that can be included in Global.asax?
Application_Start,
Application_End,
Application_AcquireRequestState,
Application_AuthenticateRequest,
Application_AuthorizeRequest,
Application_BeginRequest,
Application_Disposed,
Application_EndRequest,
Application_Error,
Application_PostRequestHandlerExecute,
Application_PreRequestHandlerExecute,
Application_PreSendRequestContent,
Application_PreSendRequestHeaders,
Application_ReleaseRequestState,
Application_ResolveRequestCache,
Application_UpdateRequestCache,
Session_Start,
Session_End

You can optionally include "On" in any of method names. For example, you can name a BeginRequest event handler.Application_BeginRequest or Application_OnBeginRequest.You can also include event handlers in Global.asax for events fired by custom HTTP modules.Note that not all of the event handlers make sense for Web Services (they're designed for ASP.NET applications in general, whereas .NET XML Web Services are specialized instances of an ASP.NET app). For example, the Application_AuthenticateRequest and Application_AuthorizeRequest events are designed to be used with ASP.NET Forms authentication.

6)How can you use Global.asax to catch unhandled exceptions?Unhandled exceptions can be caught by using Application_Error() method of global.asax.
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception objErr = Server.GetLastError().GetBaseException();
string err = "Error in: " + Request.Url.ToString() +
". Error Message:" + objErr.Message.ToString();

}


A complete information on Global.asax can be found at : http://articles.techrepublic.com.com/5100-10878_11-5771721.html

Sql server Optimization techniques and best practices

1)Go for views and stored procedures
Rather than writing the heavy-duty queries, we can use stored procedures and views, Stored procedure will be compiled only once, hence it will not create execution plan every time. Also it will reduce network traffic, also we can restrict the user from accessing tables by using views. this will give more security to data.

2)Use Table Variables
At the maximum, go for table variable rather than opting for Temporary variable because table variable requires less locking when comparing to Temporary variable. Logging is also less when compare to Temporary tables.

3)Distinct Clause
This causes additional overhead, this first get all the data related to the condition specified then removes the duplicate rows,. this leads to performance degradation on large set of data. Apply distinct only if it's necessary. if u have duplicate rows, first try to remove the duplicate rows from the table.

4)use constraints
Instead of working with triggers, the best thing is to use constraints at the maximum, this will boost the performance. A proper handling of constraints will restrict unwanted data, entering into the database.

5)Use Union All
In the performance wise observation, Union All is faster, it will not consider the duplicate rows, hence fetches the records more faster. where as Union statement does a distinct over the result set.

6)SQL Server Cursors
It will occupy the memory available for other resources, hence degrades the performance. for fetching a 100 rows, it will execute 100 individual queries.
Use cursor only in the place which is unavoidable. Instead use correlated sub-queries or derived tables in case of performing row-by-row operations.

7)Add the WITH RECOMPILE option to the CREATE PROCEDURE statement if you know that your query will vary each time it is run from the stored procedure.
The WITH RECOMPILE option prevents reusing the stored procedure execution plan, so SQL Server does not cache a plan for this procedure and the procedure is always recompiled at run time. Using the WITH RECOMPILE option can boost performance if your query will vary each time it is run from the stored procedure, because in this case the wrong execution plan will not be used


8)Avoid using Having Clause
The HAVING clause is used to restrict the result set which is returned by the
GROUP BY clause. When one use GROUP BY with the HAVING clause, the GROUP BY clause will divide the rows into the set of grouped rows and aggregates their values, and then the HAVING clause eliminates the unwanted aggregated groups. In many cases, you can write your select statement, so that it will contain only WHERE and GROUP BY clauses without HAVING clause. This will improve the performance of your query.

9)Row Count
If you want to return the total row count, Use the below query to get the row count of a particular table, it is much faster than count(*).

SELECT rows
FROM sysindexes
WHERE id = OBJECT_ID('table_name') AND (indid < 2) because the count(*) will involve in a full table scan, hence degrades the performance, also it will take more and more time for larger set of data.
10)SET NOCOUNT ON
Do Include SET NOCOUNT ON statement on your stored procedures, this will stop
the message indicating the number of rows affected by a T-SQL statement.
This will reduce network traffic, because your client will not receive
the message indicating the number of rows affected by a T-SQL statement.

11)Use Where Clause
Use where clause, it will restrict the result set and will increase the performance because SQL Server will return to the client only the particular rows instead of all rows from the table. hence it would reduce the network traffic and boost the overall performance of the query.

12)Use Top
Use top keyword if you want to get a particular set of results.
Also you can make use of SET Rowcount. this will improve the performance by reducing the traffic between the client and server.

Select Top 1 Col from Table

This will return only one row as a result.

13)Use Selected Columns
Don't use select *, instead select the fields which you want to retrieve. The server will give the client only the selected columns, this will also reduce the traffic and increase the speed of your query.

14)Index Optimization
Each and every index will increase the time it takes to perform INSERT, UPDATE
and DELETE, So the number of index in a table must be less to have good performance. At the maximum use 4-5 indexes on one table
In case if your table is read only then the index can be more.

Try to put index on the correct column, there is of no use if you apply the index in the Gender column, where there will be only two options, Male or female. Use index in the integer field, than applying the index in characters
Clustered indexes are preferable than non-clustered indexes. Apply the covering index, if your application performs the same query repeatedly

15) stored procedures prefixed with 'sp_'
Any stored procedures prefixed with 'sp_' are first searched for in the Master database rather than the one it is created in. This will cause a delay in the stored procedure being executedAny stored procedures prefixed with 'sp_' are first searched for in the Master database rather than the one it is created in. This will cause a delay in the stored procedure being executed.

16)all stored procedures referred to as dbo.sprocname
When calling a stored procedure you should include the owner name in the call, i.e. use EXEC dbo.spMyStoredProc instead of EXEC spMyStoredProc.Prefixing the stored procedure with the owner when executing it will stop SQL Server from placing a COMPILE lock on the procedure while it determines if all objects referenced in the code have the same owners as the objects in the current cached procedure plan.

17)Transactions being kept as short as possible
If you are use SQL transactions, try to keep them as short as possible. This will help db performance by reducing the number of locks. Remove anything that doesn't specifically need to be within the transaction like setting variables, select statements etc.

Improving performance in Asp.net applications

The following are the some of the guidelines to create a good ASP.NET application.

State Management
1)Disable session when not using it. This can be done at the application level in the "machine.config" file or at a page level. ASP.NET uses a built-in session state mechanism as well as supports your Custom Session State model. However, the cost of Session storage becomes heavy when the users, objects stored increase significantly. Turn off Session State for pages which dont require Session. Typically in a web application there may be static as well as dynamic pages. In the static pages, which dont require Session, the Session State can be turned off. Wherever, you just require Session Data as ReadOnly (where you wont be updating the Session Data), the SessionState can be made ReadOnly.

To turn off Session State at page level,
<%@ Page EnableSessionState="False" %>

To make it ReadOnly at page level,
<%@ Page EnableSessionState="ReadOnly" %>

If your Website has a lot of content pages which are static and only certain pages require Session Data, try considering Turning off SessionState at the Web.Config level and only enable it for those pages which require Session Data. Always the Page settings will override the web.config settings. Session State can be turned off in the web.config as follows:-

<pages enableSessionState="false"></pages>


2)The in-proc model of session management is the fastest of the three options. SQL Server option has the highest performance hit.

3)Minimize the amount and complexity of data stored in a session state. The larger and more complex the data is, the cost of serializing/deserializing of the data is higher (for SQL Server and State server options).

4)Avoid storing STA COM objects in session state.

5)Use the ReadOnly attribute when you can.

6)Use static properties instead of the Application object to store application state.

7)Use application state to share static, read-only data.

8)Do not store single-threaded apartment (STA) COM objects in application state.

9)Turn off View State
View State is the wonderful mechanism which stores the page as well as controls' data, state etc., across round trips. However, the View State can grow heavy significantly increasing the load and hence, View State must be turned off for pages which dont require the same. ViewState can be turned off at page level as follows:-

<%@ Page EnableViewState="False" %>

10)Save server control view state only when necessary.

11)Minimize the number of objects you store in view state.

Server Round Trips
1)Use Server.Transfer for redirecting between pages in the same application. This will avoid unnecessary client-side redirection.

2)Avoid unnecessary round-trips to the server - Code like validating user input can be handled at the client side itself.

3)Use Page.IsPostback to avoid unnecessary processing on a round trip.

Use of Server Controls
1)Use server controls in appropriate circumstances. Even though are they are very easy to implement, they are expensive because they are server resources. Sometimes, it is easier to use simple rendering or data-binding.

2)Avoid creating deep hierarchies of controls.

Exception Handling
1)Don't rely on exceptions in the code. Exceptions reduce performance. Do not catch the exception itself before handling the condition.

// Consider changing this...
try { result = 100 / num;}
catch (Exception e) { result = 0;}

// to this...
if (num != 0)
result = 100 / num;
else
result = 0;

2)To guarantee resources are cleaned up when an exception occurs, use a try/finally block. Close the resources in the finally clause. Using a try/finally block ensures that resources are disposed even if an exception occurs. Open your connection just before needing it, and close it as soon as you're done with it. Your motto should always be "get in, get/save data, get out." If you use different objects, make sure you call the Dispose method of the object or the Close method if one is provided. Failing to call Close or Dispose prolongs the life of the object in memory long after the client stops using it. This defers the cleanup and can contribute to memory pressure. Database connection and files are examples of shared resources that should be explicitly closed.

Com Components
1)Port call-intensive COM components to managed code. While doing Interop try avoiding lot of calls. The cost of marshalling the data ranges from relatively cheap (i.e. int, bool) to more expensive (i.e. strings). Strings, a common type of data exchanged for web applications, can be expensive because all strings in the CLR are in Unicode, but COM or native methods may require other types of encoding (i.e. ASCII).

2)Release the COM objects or native resources as soon as the usage is over. This will allow other requests to utilize them, as well as reducing performance issues, such as having the GC release them at a later point.

DataBase Related:
1)Use SQL server stored procedures for data access.

2)Use the SQLDataReader class for a fast forward-only data cursor.

3)Datagrid is a quick way of displaying data, but it slows down the application. The other alternative, which is faster, is rendering the data for simple cases. But this difficult to maintain. A middle of the road solution could be a repeater control, which is light, efficient, customizable and programmable.

4)Cache data and page output whenever possible.

Collections:
1) Avoid the use of ArrayList. Because any objects added into the Arraylist are added as System.Object and when retrieving values back from the arraylist, these objects are to be unboxed to return the actual valuetype. So it is recommended to use the custom typed collections instead of ArrayList. .NET provides a strongly typed collection class for String in System.Collection.Specialized, namely StringCollection. For other type of class, rename the _ClassType attribute in the attached file to your required type.

2) Reconsider the use of Hashtable instead try other dictionary such as StringDictionary, NameValueCollection, HybridCollection. Hashtable can be used if less number of values are stored.

3)Enumerating into collections sometimes is more expensive than index access in a loop. This is because the CLR can sometimes optimize array indexes and bounds checks away in loops, but can't detect them in for each type of code.

Others:
1)Disable debug mode before deploying the application.

2)For applications that rely extensively one external resource, consider enabling web gardening on multiprocessor computers. The ASP.NET process model helps enable scalability by distributing work to several processes, one on each CPU. If the application is using a slow database server or calls COM objects that access external resources, web gardening could be a solution.

3)Do a "pre-batch" compilation. To achieve this, request a page from the site.

4)Setting the AutoEventWireup attribute to false in the Machine.config file means that the page will not match method names to events and hook them up (for example, Page_Load). If page developers want to use these events, they will need to override the methods in the base class (for example, they will need to override Page.OnLoad for the page load event instead of using a Page_Load method). If you disable AutoEventWireup, your pages will get a slight performance boost by leaving the event wiring to the page author instead of performing it automatically.

5)Buffering is on by default. Turning it off will slow down the performance. Don't code for string buffering - Response.Write will automatically buffer any responses without the need for the user to do it. Use multiple Response.Writes rather than create strings via concatenation, especially if concatenating long strings.

Tuesday, February 16, 2010

Best Data Access Layer Practices

A Data Access Layer comprises of a collection of classes, interfaces and their methods and properties that are used to perform CRUD (Create, Read, Update and Delete) operations in the application. A Data Access Layer encapsulates the code that is used to connect to the database and perform these operations and it actually works as a link between the business entities in your application and the actual data storage layer. You typically use the Data Access Layer to create and populate business entities with data from the database and for updating and storing business entities in the database.

Features of a Data Access Layer

A Data Access Layer should provide the following features:

■Connect to the database
■Open and Close connections
■Support for CRUD operations
■Transaction management
■Provider independence
■Concurrency management

Some of the points that we should keep in mind are :
•Create all your database access routines as generic, versatile objects, rather than client-side repeated-code methods. All your UI should do is interact with these components, and not have to work out any details.

•If you want to page data or provide your application with functionality use a Dataset as the preferred method of disconnected data, otherwise use a Datareader for all your data retrieval. For XML users this would translate to an XmlReader , and StreamReader for text files, both equivalent as that of a DataReader for their respective file types.


•Use the correct managed data provider for your particular database, ex. System.Data.SqlClient for SQL Server , System.Data.OleDb for Access, System.Data.OracleClient for Oracle , etc.


•Use Strongly-Typed Datasets over the standard, common un-Typed ones when possible, as this yields better performance. A generated typed Dataset is basically an early bound class inheriting the Dataset base class, as opposed to the typical late bound, non-inheritable Dataset. Thus, it allows for greater flexibility in dealing with your data. In turn, you'll be dealing with easier to read code, whereby you can access fields and tables by customizable names, instead of the conventional collection-based way.

•Take full advantage of .NET Caching, as this will significantly boost performance and greatly diminish any persistent database interaction. However, and an important however is, if you decide on caching your data from within your data object, then conventional caching methods won't apply. Within the confines of components, and its interaction with the caller page, these requests happen via an HTTP request. Therefore, caching within your component can only be implemented by using the HttpContext.Cache Class property, part of .NET's Page class.


•Use parameterized Stored Procedures along side .NET's Command Class Prepare() method, that caches your query for all future SQL Server uses.
objCommand.Prepare()

•Make chunky calls to your database rather than smaller, chatty calls. It's better to group all similar, associated calls in one SQL Server access. In other words, use one solid connection to retrieve as much as you can, as opposed to multiple ones.


•Take advantage of connection pooling (whereby all your connection strings are identical) by storing all your connection strings in your web.config file. Furthermore, you'll find that your application will scale alot better with any increased network traffic by doubling, even tripling the default Max Pool Size of 100 to 300, and even bumping up the default Connect Timeout of 10 seconds to 60. Additionally, if your not enlisting any transactional procedures, include enlist=false; to your database's connection string for added performance.
Additionally, if your not enlisting any transactional procedures, include enlist=false ; to your database's connection string for added performance.


•Finally, remember to close, clear and dispose of all your data objects no matter what. If you would like to further confirm that your database connection is indeed closed, you would write:


A good article on building a data access layer is as follows :http://msdn.microsoft.com/en-us/library/aa581776.aspx

Best Exception Handling Practises

Best Practices for handling exceptions

1) Exception is an expensive process, for this reason, you should use exceptions only in exceptional situations and not to control regular logic flow.

For example:

void EmpExits ( string EmpId)
{
//... search for employee
if ( dr.Read(EmpId) ==0 ) // no record found, ask to create
{
throw( new Exception("Emp Not found"));
}
}

The best practice is :

bool EmpExits ( string EmpId)
{
//... search for Product
if ( dr.Read(EmpId) ==0 ) // no record found, ask to create
{
return false
}
}

2) Avoid exception handling inside loops, if its really necessary implement try/catch block surrounds the loop.

3)Adopt the standard way of handling the exception through try, catch and finally block. This is the recommended approach to handle exceptional error conditions in managed code, finally blocks ensure that resources are closed even in the event of exceptions.

For example:

SqlConnection conn = new SqlConnection("...");
try
{
conn.Open();
//.some operation
// ... some additional operations
}
catch(¦)
{
// handle the exception
}
finally
{
if (conn.State==ConnectionState.Open)
conn.Close(); // closing the connection
}

4)Where ever possible use validation code to avoid unnecessary exceptions .If you know that a specific avoidable condition can happen, precisely write code to avoid it. For example, before performing any operations checking for null before an object/variable can significantly increase performance by avoiding exceptions.

For example:

double result = 0;
try
{
result = firstVal/secondVal;
}
catch( System.Exception e)
{
//handling the zero divided exception
}

This is better then the above code

double result = 0;
if(secondVal >0)
result = firstVal/secondVal;
else
result = System.Double.NaN;

5)Do not rethrow exception for unnecessary reason because cost of using throw to rethrow an existing exception is approximately the same as creating a new exception and rethrow exception also makes very difficult to debug the code.

For example:

try
{
// Perform some operations ,in case of throw an exception
}
catch (Exception e)
{
// Try to handle the exception with e
throw;
}

6)The recommended way to handle different error in different way by implement series of catch statements this is nothing but ordering your exception from more specific to more generic for example to handle file related exception its better to catch FileNotFoundException, DirectoryNotFoundException, SecurityException, IOException, UnauthorizedAccessException and at last Exception.

7) .NET errors should be capture through SqlException or OleDbException.
•Use the ConnectionState property for checking the connection availability instead of implementing an exception.

•Use Try/Finally more often, finally provides option to close the connection or the using statement provides the same functionality.

•Use the specific handler to capture specific exception, in few scenarios if you know that there is possibility for a specific error like database related error it can be catch through SqlException or OleDbException as below.

try
{ ...
}
catch (SqlException sqlexp) // specific exception handler
{ ...
}
catch (Exception ex) // Generic exception handler
{ ...
}
8) When creating user-defined exceptions, you must ensure that the metadata for the exceptions is available to code executing remotely, including when exceptions occur across application domains. For example, suppose Application Domain A creates Application Domain B, which executes code that throws an exception. For Application Domain A to properly catch and handle the exception, it must be able to find the assembly containing the exception thrown by Application Domain B. If Application Domain B throws an exception that is contained in an assembly under its application base, but not under Application Domain A's application base, Application Domain A will not be able to find the exception and the common language runtime will throw a FileNotFoundException. To avoid this situation, you can deploy the assembly containing the exception information in two ways:
a) Put the assembly into a common application base shared by both application domains
- or -
b)If the domains do not share a common application base, sign the assembly containing the exception information with a strong name and deploy the assembly into the global assembly cache.

9) In most cases, use the predefined exceptions types. Define new exception types only for programmatic scenarios. Introduce a new exception class to enable a programmer to take a different action in code based on the exception class.

10)Do not derive user-defined exceptions from the Exception base class. For most applications, derive custom exceptions from the ApplicationException class.

11)Include a localized description string in every exception. When the user sees an error message, it is derived from the description string of the exception that was thrown, rather than from the exception class.

12)Use grammatically correct error messages, including ending punctuation. Each sentence in a description string of an exception should end in a period.

13)Provide Exception properties for programmatic access. Include extra information in an exception (in addition to the description string) only when there is a programmatic scenario where the additional information is useful.

14) Throw an InvalidOperationException if a property set or method call is not appropriate given the object's current state.
Throw an ArgumentException or a class derived from ArgumentException if invalid parameters are passed.

Its recommend to use "Exception Management Application Block" provided by Microsoft. It is a simple and extensible framework for logging exception information to the event log or you can customize to write the exception information to other data sources without affecting your application code and implemented all best practices and tested in Microsoft Lab.

Tuesday, February 2, 2010

C# Serialization Interview Questions

Define Serilization
Serialization can be defined as the process of storing the state of an object instance to a storage medium. During this process, the public and private fields of the object and the name of the class, including the assembly containing the class, is converted to a stream of bytes, which is then written to a data stream. When the object is subsequently deserialized, an exact clone of the original object is created.

why should you use serilization when the values of obj can be stored on disk and later used whenever required?
It is often necessary to store the value of fields of an object to disk and then retrieve this data at a later stage. Although this is easy to achieve without relying on serialization, this approach is often cumbersome and error prone, and becomes progressively more complex when you need to track a hierarchy of objects. Imagine writing a large business application containing many thousands of objects and having to write code to save and restore the fields and properties to and from disk for each object. Serialization provides a convenient mechanism for achieving this objective with minimal effort.

Explain how CLR manages serilization?
The Common Language Runtime (CLR) manages how objects are laid out in memory and the .NET Framework provides an automated serialization mechanism by using reflection. When an object is serialized, the name of the class, the assembly, and all the data members of the class instance are written to storage. Objects often store references to other instances in member variables. When the class is serialized, the serialization engine keeps track of all referenced objects already serialized to ensure that the same object is not serialized more than once. The serialization architecture provided with the .NET Framework correctly handles object graphs and circular references automatically. The only requirement placed on object graphs is that all objects referenced by the object that is being serialized must also be marked as Serializable. If this is not done, an exception will be thrown when the serializer attempts to serialize the unmarked object.


Why would you want to use serialization?
The two most important reasons are to persist the state of an object to a storage medium so an exact copy can be recreated at a later stage, and to send the object by value from one application domain to another.

Give examples where serialization is used?For example, Serialization is used to save session state in ASP.NET and to copy objects to the clipboard in Windows Forms.It is also used by remoting to pass objects by value from one application domain to another.

How will you serialize a class?
The easiest way to make a class serializable is to mark it with the Serializable attribute as follows:
[Serializable]
public class MyObject {
public int n1 = 0;
public int n2 = 0;
public String str = null;
}

How will you deserialize an object?
First, create a formatter and a stream for reading, and then instruct the formatter to deserialize the object.
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin",
FileMode.Open,
FileAccess.Read,
FileShare.Read);
MyObject obj = (MyObject) formatter.Deserialize(fromStream);
stream.Close();

Which class is responsible for binary serialization?
The BinaryFormater is the class responsible for the binary serialization and It's commonly used for the .Net Remoting.

What does it take to make my object serializable?
Your class must have the attribute SerializableAttribute and all its members must also be serializable, except if they are ignored with the attribute NonSerializedAttribute. Private and public fields are serialized by default.

What are the main advantages of binary serialization?
Smaller
Faster
More powerful (support complex objects and read only properties)

Will my read only properties be serialized?
Yes if the properties encapsulate a field. By default all the private and public fields are serialized. Binary serialization is not related to properties.

Is it possible to have circular reference?
Yes it is, you can have circular reference and the binary serialization process will work fine. .Net generate the object graph before the executing the serialization and finally generate the stream. Unlike Xml serialization process, the BinaryFormater has no problem with the circular reference.

Why my Dataset is so big when it's serialized in binary?
By default the DataSet is serialized in Xml and the binary stream only wraps the Xml Data inside it. That's mean that the size is similar to the Xml size. .Net 2.0 add a new property named RemotingFormat used to change the binary serialization format of the DataSet. SerializationFormat.Binary will generate a better result.
For previous version, it's also possible to download the DataSetSurrogate to reduce the size and increase the performance.

How can I implement a custom serialization?
If you need to control the serialization process of your class, you can implement the ISerializable interface which contains a method GetObjectData and a special constructor .

Why use custom serialization ?
By using it you will be able to handle version change in your class or get a better performance result. An exception of type SerializationException is raised if the fields does not exists.
#region ISerializable Members
//Special constructor
protected CustomInvoice(SerializationInfo info, StreamingContext context) {
clientName = info.GetString("clientName");
date = info.GetDateTime("date");
total = info.GetDouble("total");
}
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public void GetObjectData(SerializationInfo info, StreamingContext context) {
info.AddValue("clientName", clientName);
info.AddValue("date", date);
info.AddValue("total", total);
}
#endregion

When does a change in my object break the deserialization?
Binary serialization is not tolerant to version change. There is no problem when a new field is added but if a field is missing, an exception is throw. New field will get the default value. .Net 2.0 include a new attribute named OptionalFieldAttribute. For previous version, you must implement your own custom serialization and handle manually the changes.

When does a change in my object NOT break the deserialization?
Version fault exception are only checked if the assembly is signed, all version change are ignored otherwise.

How can I make my object version tolerant?
Use the OptionalFieldAttribute or implement your own custom serialization with ISerializable.

Why set VersionAdded of OptionalFieldAttribute since it's a free text?
The parameter is only for informative purpose. .Net never checked it by default but it could be used in a custom serialization. Use the reflection to get the attribute of field and check the added version value.

Does BinaryFormatter from .Net 1.1 is compatible with the BinaryFormatter 2.0?
Absolutely, the BinaryFormatter 2.0 is 100% with other version, but it's not the case with the SoapFormatter.

How can I modify a value just before the serialization or just after the deserialization?
You can add custom attribute to some method. Your marked method will get called a the right time. This is usefull to initialize a property after the deserialization or to clean up your instance before the serialization.
OnDeserializingAttribute :This event happens before deserialization
OnDeserializedAttribute :This event happens after deserialization
OnSerializingAttribute :This event happens before serialization
OnSerializedAttribute :This even happens after serialization
[Serializable]
public class SecurityToken {
private string password;
private string userName;

private string Decrypt(string aPassword) {
// Decrypt the password here !!!
return password;
}
private string Encrypt(string aPassword) {
// Encrypt the password here !!!
return password;
}
[OnSerializing()]
internal void OnSerializingMethod(StreamingContext context) {
password = Encrypt(password);
}
[OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context) {
password = Decrypt(password);

// Set the default
if (userName == null) {
userName = Environment.UserName;
}
}
public string Password {
get {
return password;
}
set {
password = value;
}
}
public string UserName {
get {
return userName;
}
set {
userName = value;
}
}
}

How can I create a generic Binary deserialization method?
// Binary deserialization (generic version with the return value casted)
public static T DeserializeBinary(string aFileName) {
using (FileStream _FileStream = new FileStream(aFileName, FileMode.Open)) {
BinaryFormatter _Formatter = new BinaryFormatter();
return (T)_Formatter.Deserialize(_FileStream);
}
}

Which class is responsible for Xml serialization?
XmlSerializer is responsible of the Xml serialization.

What is the difference between the SoapFormatter and the XmlSerializer?
SoapFormatter is used to create a Soap envelop and use an object graph to generate the result. The XmlSerializer process use only the public data and the result is a more common xml file. The Web Service in .Net use an XmlSerializer to generate the output contained in the Soap message. The SoapFormatter and the BinaryFormatter are used in the .Net Remoting serialization process.
What does it take to make my object serializable?
Nothing, but there is some constraint :
Your object must have a public empty constructor.
Field or property must be public
Their return type must also respect serialization rules.
Property must be read write.

What are the main advantages of Xml serialization?
Based on international standard (XML).
Cross platforms.
Readable and can be edited easily.

How do I encapsulate the Xml serialization method?
public static void SerializeXml( object aObject, string aFileName) {
using (FileStream _FileStream = new FileStream(aFileName, FileMode.Create)) {
XmlSerializer _Serializer = new XmlSerializer ( aObject.GetType());
_Serializer.Serialize(_FileStream, aObject);
}
}

How do I encapsulate the Xml deserialization method?
public static object DeserializeXml( string aFileName, Type aType) {
using (FileStream _FileStream = new FileStream(aFileName, FileMode.Create)) {
XmlSerializer _Serializer = new XmlSerializer (aType);
return _Serializer.Deserialize(_FileStream);
}
}

How can I create a generic Xml deserialization method?
public static T DeserializeXml(string aFileName) {
using (FileStream _FileStream = new FileStream(aFileName, FileMode.Open)) {
XmlSerializer _Serializer = new XmlSerializer (typeof(T));
return (T)_Serializer.Deserialize(_FileStream);
}
}

How can I ignore a property in serialization?
If you use a XmlSerializer, mark your property with the custom attribute XmlIgnoreAttribute and if you use a SoapFormatter, use a SoapIgnoreAttribute instead.

How can I rename a field or a property in the Xml output?
Use the attribute XmlElementAttribute or XmlAttributeAttribute with the new name as parameter. To rename a class, use the XmlTypeAttribute.
[XmlType("city")]
public class Town {
private string name;
private string state;
[XmlElement("townname")]
public string Name {
get {
return name;
}
set {
name = value;
}
}
[XmlAttribute("state")]
public string State {
get {
return state;
}
set {
state = value;
}
}
}

How can I serialize a property as an Xml attribute?
By default properties are serialized as Xml elements, but if you add an XmlAttributeAttribute to a property, .Net will generate an attribute instead. It's must be type compatible with an Xml attribute. See example here
[XmlAttribute("state")]
public string State {
get {
return state;
}
set {
state = value;
}
}

How can I implement custom serialization?
You need to Implement the interface IXmlSerializable. This class is available in the .Net 1.X but it's was not documented. It's now official available with .Net 2.0. With custom serialization, it's possible to optimize the output and generate only what is needed. In this example, we generate only the non empty properties
public class SessionInfo : IXmlSerializable {
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema() {
return null;
}
public void ReadXml(XmlReader reader) {
UserName = reader.GetAttribute("UserName");

while (reader.Read()) {
if (reader.IsStartElement()) {
if (!reader.IsEmptyElement) {
string _ElementName = reader.Name;
reader.Read(); // Read the start tag.
if(_ElementName == "MachineName") {
MachineName = reader.ReadString();
} else {
reader.Read();
}
}
}
}
}
public void WriteXml(XmlWriter writer) {
if (!String.IsNullOrEmpty(UserName))
writer.WriteAttributeString("UserName", UserName);

if (!String.IsNullOrEmpty(MachineName))
writer.WriteElementString("MachineName", MachineName);
}
#endregion
private string machineName;
private string userName;

public string MachineName {
get {
return machineName;
}
set {
machineName = value;
}
}
public string UserName {
get {
return userName;
}
set {
userName = value;
}
}
}

How can I serialize a property array?
Array are compatible with the serialization, but all elements must be of the same type.

How can I serialize an array with different types?
It's possible to tag the array with all the possible type. Use XmlInclude on the class containing the array or XmlArrayItem. All the possible types must be specified with the attributes. Array types must be known because of the Xml schema. XmlInclude could be used for property that returned differente types. It's complicate object inheritance since all types must be known. This example will fail with a message "There was an error generating the XML document." because the Cars could contain undefined types.
public class Car {}
public class Ford : Car{}
public class Honda: Car {}
public class Toyota: Car {}
public class CarSeller {
private List cars = new List();
public List Cars {
get {
return cars;
}
set {
cars = value;
}
}
}
...
Ford _Ford = new Ford();
Honda _Honda = new Honda();
Toyota _Toyota = new Toyota();
CarSeller _Seller = new CarSeller();
_Seller.Cars.Add(_Ford);
_Seller.Cars.Add(_Honda);
_Seller.Cars.Add(_Toyota);
SerializationHelper.SerializeXml(_Seller, @"seller.xml");
Three possible solutions:
#1 Add XmlIncludeAttribute to the Seller class:
[XmlInclude(typeof(Ford))]
[XmlInclude(typeof(Honda))]
[XmlInclude(typeof(Toyota))]
public class CarSeller {
private List cars = new List();

public List Cars {
get {
return cars;
}
set {
cars = value;
}
}
}

#2 Add XmlArrayItem to the property named Cars:
public class CarSeller {
private List cars = new List();

[XmlArrayItem(typeof(Ford))]
[XmlArrayItem(typeof(Honda))]
[XmlArrayItem(typeof(Toyota))]
public List Cars {
get {
return cars;
}
set {
cars = value;
}
}
}

#3 Implement our own serialization with IXmlSerializable :
see items above

How can I serialize a collection?
Collection are serialized correctly, but they must contains only object of same types. Read only properties of type ArrayList, List and other collections will be serialized and deserialized correctly.
Public class Role {
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
public class UserAccount {
private string userName;

public string UserName {
get {
return userName;
}
set {
userName = value;
}
}
// Generic version
private List roles = new List();

public List Roles {
get {
return roles;
}
}
// ArrayList version
private ArrayList roleList = new ArrayList();

public ArrayList RoleList {
get {
return roleList;
}
}
// String collection version
private StringCollection roleNames = new StringCollection();

public StringCollection RoleNames {
get {
return roleNames;
}
}
UserAccount _UserAccount = new UserAccount();
_UserAccount.UserName = "dhervieux";

Role _RoleAdmin = new Role();
_RoleAdmin.Name = "Admin";
Role _RoleSales = new Role();
_RoleSales.Name = "Sales";
_UserAccount.RoleList.Add(_RoleAdmin);
_UserAccount.RoleList.Add(_RoleSales);
_UserAccount.Roles.Add(_RoleAdmin);
_UserAccount.Roles.Add(_RoleSales);
_UserAccount.RoleNames.Add("Admin");
_UserAccount.RoleNames.Add("Sales");
SerializationHelper.SerializeXml(_UserAccount, @"useraccount.xml");
UserAccount _Result =
SerializationHelper.DeserializeXml(@"useraccount.xml");


Why is the first serialization of each type of object is so long?
XmlSerializer generate an assembly in memory optimized for each type. That's explain why the first call to a Web Service is so long. In .Net 2.0, there is an option in the project properties of Visual Studio to generate the Xml serialization assembly. Use it directly in the IDE or use sgen.exe, this tools come with the .Net Framework SDK.

How can I optimize the serialization process?
Pregenerate your serialization assembly with Visual Studio or sgen.exe. Implementing your own serialization could also increase the performance.

How can I serialize an array directly to stream?
Yes it possible. .Net will name the Array and save the content. All the data must be of the same type.
bool[] _BoolArray = new bool[] { true, false, false, true };
// Serialization
SerializationHelper.SerializeXml(_BoolArray, @"boolarray.xml");
//Deserialization
_Result = SerializationHelper.DeserializeXml(@"directboolarray.xml");
will produce:


true
false
false
true


Which serializer is used by a Web Services?
Web Services are using SOAP to communicate, but returned objets or parameters are serialized with the XmlSerializer.

Does read only properties are serialized?
No, they are not, except for collections.

How can I serialize a multidimensional array
You need to encapsulate your array in a structure or a class an serialize it. Multidimensional array are not serializable by default.

How can I avoid serialization for an empty list or property with a default value?
There is an undocumented way of doing that, you need to create a method named ShouldSerialize where is replaced by the property name. This method should return a boolean that indicate if the property must be serialized or not. For exemple, if you have list with no item, there is no need to serialize an empty list.
public class Registration {
private string[] users = new string[0];
public bool ShouldSerializeUsers() {
return users.Length > 0;
}
public string[] Users {
get {
return users;
}
set {
users = value;
}
}
}
Result :


Without the ShouldSerializeUsers :





Why my object is marked as Serializable (like SortedList) and it's does not work?
The SerializationAttribute is only used for the binary serialization. That does not mean that it will work with an XmlSerializer. That's the case of the SortedList.

How to remove the default namespace in the serialization?
It's possible to remove the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" and xmlns:xsd="http://www.w3.org/2001/XMLSchema" from the serialization result, it's possible to add an XmlSerializerNamespaces with an empty namespace mapping.
User _User = new User(new string[] { "Admin", "Manager" });
using (FileStream _FileStream = new FileStream("user.xml", FileMode.Create)) {
XmlSerializer _Serializer = new XmlSerializer(_User.GetType());
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
_Serializer.Serialize(_FileStream, _User, ns);
}

A good article on Serilization : http://msdn.microsoft.com/en-us/magazine/cc163902.aspx

What is new in VS 2008?

New Features of Visual Studio 2008 for .NET Professionals

1. LINQ Support
LINQ essentially is the composition of many standard query operators that allow you to work with data in a more intuitive way regardless.

The benefits of using LINQ are significant – Compile time checking C# language queries, and the ability to debug step by step through queries.

2. Expression Blend Support
Expression blend is XAML generator tool for silverlight applications. You can install Expression blend as an embedded plug-in to Visual Studio 2008. By this you can get extensive web designer and JavaScript tool.

3. Windows Presentation Foundation
WPF provides you an extensive graphic functionality you never seen these before. Visual Studio 2008 contains plenty of WPF Windows Presentation Foundation Library templates. By this a visual developer who is new to .NET, C# and VB.NET can easily develop the 2D and 3D graphic applications.

Visual Studio 2008 provides free game development library kits for games developers. currently this game development kits are available for C++ and also 2D/3D Dark Matter one image and sounds sets.

4. VS 2008 Multi-Targeting Support
Earlier you were not able to working with .NET 1.1 applications directly in visual studio 2005. Now in Visual studio 2008 you are able to create, run, debug the .NET 2.0, .NET 3.0 and .NET 3.5 applications. You can also deploy .NET 2.0 applications in the machines which contains only .NET 2.0 not .NET 3.x.

5. AJAX support for ASP.NET
Previously developer has to install AJAX control library separately that does not come from VS, but now if you install Visual Studio 2008, you can built-in AJAX control library. This Ajax Library contains plenty of rich AJAX controls like Menu, TreeView, webparts and also these components support JSON and VS 2008 contains in built ASP.NET AJAX Control Extenders.

6. JavaScript Debugging Support
Since starting of web development all the developers got frustration with solving javascript errors. Debugging the error in javascript is very difficult. Now Visual Studio 2008 makes it is simpler with javascript debugging. You can set break points and run the javaScript step by step and you can watch the local variables when you were debugging the javascript and solution explorer provides javascript document navigation support.

7. Nested Master Page Support
Already Visual Studio 2005 supports nested master pages concept with .NET 2.0, but the problem with this Visual Studio 2005 that pages based on nested masters can't be edited using WYSIWYG web designer. But now in VS 2008 you can even edit the nested master pages.

8. LINQ Intellisense and Javascript Intellisense support for silverlight applications
Most happy part for .NET developers is Visual Studio 2008 contains intellisense support for javascript. Javascript Intellisense makes developers life easy when writing client side validation, AJAX applications and also when writing Silverlight applications

Intellisense Support: When we are writing the LINQ Query VS provides LINQ query syntax as tool tips.

9. Organize Imports or Usings
We have Organize Imports feature already in Eclipse. SInce many days I have been waiting for this feature even in VS. Now VS contains Organize Imports feature which removes unnecessary namespaces which you have imported. You can select all the namespaces and right click on it, then you can get context menu with Organize imports options like "Remove Unused Usings", "Sort Usings", "Remove and Sort". Refactoring support for new .NET 3.x features like Anonymous types, Extension Methods, Lambda Expressions.

10. Intellisense Filtering
Earlier in VS 2005 when we were typing with intellisense box all the items were being displayed. For example If we type the letter 'K' then intellisense takes you to the items starts with 'K' but also all other items will be presented in intellisense box. Now in VS 2008 if you press 'K' only the items starts with 'K' will be filtered and displayed.

11. Intellisense Box display position
Earlier in some cases when you were typing the an object name and pressing . (period) then intellisense was being displayed in the position of the object which you have typed. Here the code which we type will go back to the dropdown, in this case sometimes programmer may disturb to what he was typing. Now in VS 2008 If you hold the Ctrl key while the intellisense is dropping down then intellisense box will become semi-transparent mode.

12. Visual Studio 2008 Split View
VS 205 has a feature show both design and source code in single window. but both the windows tiles horizontally. In VS 2008 we can configure this split view feature to vertically, this allows developers to use maximum screen on laptops and wide-screen monitors.

Here one of the good feature is if you select any HTML or ASP markup text in source window automatically corresponding item will be selected in design window.

13. HTML JavaScript warnings, not as errors:
VS 2005 mixes HTML errors and C# and VB.NET errors and shows in one window. Now VS 2008 separates this and shows javascript and HTML errors as warnings. But this is configurable feature.

14. Debugging .NET Framework Library Source Code:
Now in VS 2008 you can debug the source code of .NET Framework Library methods. Lets say If you want to debug the DataBind() method of DataGrid control you can place a debugging point over there and continue with debug the source code of DataBind() method.

15. In built Silverlight Library
Earlier we used to install silverlight SDK separately, Now in VS 2008 it is inbuilt, with this you can create, debug and deploy the silverlight applications.

16. Visual Studio LINQ Designer
Already you know in VS 2005 we have inbuilt SQL Server IDE feature. by this you no need to use any other tools like SQL Server Query Analyzer and SQL Server Enterprise Manger. You have directly database explorer by this you can create connections to your database and you can view the tables and stored procedures in VS IDE itself. But now in VS 2008 it has View Designer window capability with LINQ-to-SQL.

17. Inbuilt C++ SDK
Earlier It was so difficult to download and configure the C++ SDK Libraries and tools for developing windows based applications. Now it is inbuilt with VS 2008 and configurable

18. Multilingual User Interface Architecture - MUI
MUI is an architecture contains packages from Microsoft Windows and Microsoft Office libraries. This supports the user to change the text language display as he wish.

Visual Studio is now in English, Spanish, French, German, Italian, Chinese Simplified, Chinese Traditional, Japanese, and Korean. Over the next couple of months. Microsoft is reengineering the MUI which supports nine local languages then you can even view Visual studio in other 9 local languages.

19. Microsoft Popfly Support
Microsoft Popfly explorer is an add-on to VS 2008, by this directly you can deploy or hosting the Silverlight applications and Marshup objects