20 June 2011

Choose the right collection type for your needs in dotnet framework

Choose the right collection type for your needs,

-when you are not concerned about the order of the items in the collections, your first inclination should be to use a System.Collections.Generic.Dictionary

-The three basic operations (Add, Remove, and Contains) all operate quickly even if the collection contains millions of items.

-Using the new LinkedList collection can potentially help you improve performance in scenarios where you need to maintain order yet still achieve fast inserts.

-Unlike List, LinkedList is implemented as a chain of dynamically allocated objects.

-The downside of a linked list from a performance point of view is increased activity by the garbage collector as it has to traverse the entire list to make sure objects were not disposed of.
-Inserting an item into a LinkedList is much faster than doing so into a List

-Dictionary may be most appropriate. However, lookup is only fast in the average case, and certain datasets could cause that performance to degrade quickly.

-SortedDictionary, uses a balanced tree implementation as the underlying data store; this provides relatively fast lookups and maintains items in a sorted order but insertions will most likely be slower (and will vary based on the number of items in the collection)

-Alternatively, you can also use SortedList, which uses two separate arrays to maintain the keys and the values separately and to maintain them both in order (in the worse case you would have to shift all the keys and values).


Custom Collections

-In certain situations, you may not find that all of your requirements are implemented in any of the existing collection types in the Framework.

-It may be that you have a unique set of requirements or that you can still use an existing collection with minor changes.

If you decide you want to write a custom collection, you should first look into extending an existing collection type.

System.Collections.ObjectModel namespace.

These collections already implement the most needed interfaces and give you the baseline functionality you expect.


17 June 2011

Difference between namespace and assembly

Difference between namespace and assembly

Assembly
An Assembly is a logical unit of functionality that runs under the control of the .NET common language runtime (CLR). An assembly physically exists as a .dll file or as an .exe file.

An Assembly can contains one or many namespaces

Namespace
Namespaces are a way of grouping type names and reducing the chance of name collisions.

16 June 2011

15 June 2011

View in Sql Server 2008

View in Sql Server 2008

Creates a virtual table whose contents (columns and rows) are defined by a query. Use this statement to create a view of the data in one or more tables in the database.
For example, a view can be used for the following purposes:

-To focus, simplify, and customize the perception each user has of the database.

-As a security mechanism by allowing users to access data through the view, without granting the users permissions to directly access the underlying base tables.

-To provide a backward compatible interface to emulate a table whose schema has changed.

Example
--Partitioned view as defined on Server1
CREATE VIEW Customers
AS
--Select from local member table.
SELECT *
FROM CompanyData.dbo.Customers_33
UNION ALL
--Select from member table on Server2.
SELECT *
FROM Server2.CompanyData.dbo.Customers_66
UNION ALL
--Select from mmeber table on Server3.
SELECT *
FROM Server3.CompanyData.dbo.Customers_99

14 June 2011

Interface vs Abstract Class in C#

Interface vs Abstract Class

Multiple inheritance

Interfaces:A class may inherit several interfaces.
Abstract Class:A class may inherit only one abstract class.

Default implementation

Interfaces:An interface cannot provide any code, just the signature.
Abstract Class:An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modifiers

Interfaces:An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public
Abstract Class:An abstract class can contain access modifiers for the subs, functions, properties

Core VS Peripheral

Interfaces:Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
Abstract Class:An abstract class defines the core identity of a class and there it is used for objects of the same type.

Usage in Application

Interfaces:If various implementations only share method signatures then it is better to use Interfaces
Abstract Class:If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Speed

Interfaces:Requires more time to find the actual method in the corresponding classes.
Abstract Class:Compare to Interface it is very fast

Fields and Constants

Interfaces:No fields can be defined in interfaces
Abstract Class:An abstract class can have fields and constrants defined


Main Limitation of Interface

If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method
in all derived class.

Main Limitation of Abstract Class

A class may inherit only one abstract class.

Normalization with Examples

Normalization with Examples

Normalization is the process of organizing data in a database.
This includes creating tables and establishing relationships between those tables according to rules designed both to protect
the data and to make the database more flexible by eliminating redundancy and inconsistent dependency.

Redundant data wastes disk space and creates maintenance problems.
If data that exists in more than one place must be changed, the data must be changed in exactly the same way in all locations. A customer address change is much easier to implement if that data is stored only in the Customers table and nowhere else in the database.

What is an "inconsistent dependency"? While it is intuitive for a user to look in the Customers table for the
address of a particular customer, it may not make sense to look there for the salary of the employee who calls on that customer.
The employee's salary is related to, or dependent on, the employee and thus should be moved to the Employees table.
Inconsistent dependencies can make data difficult to access because the path to find the data may be missing or broken.


First Normal Form

Eliminate repeating groups in individual tables.
Create a separate table for each set of related data.
Identify each set of related data with a primary key.
Do not use multiple fields in a single table to store similar data. For example, to track an inventory item that may come from two
possible sources, an inventory record may contain fields for Vendor Code 1 and Vendor Code 2.

What happens when you add a third vendor? Adding a field is not the answer; it requires program and table modifications and
does not smoothly accommodate a dynamic number of vendors. Instead, place all vendor information in a separate table called Vendors,
then link inventory to vendors with an item number key, or vendors to inventory with a vendor code key.

Second Normal Form

Create separate tables for sets of values that apply to multiple records.
Relate these tables with a foreign key.
Records should not depend on anything other than a table's primary key (a compound key, if necessary).
For example, consider a customer's address in an accounting system. The address is needed by the Customers table, but also by the Orders,
Shipping, Invoices, Accounts Receivable, and Collections tables. Instead of storing the customer's address as a separate entry in each
of these tables, store it in one place, either in the Customers table or in a separate Addresses table.

Third Normal Form

Eliminate fields that do not depend on the key.
Values in a record that are not part of that record's key do not belong in the table. In general, any time the contents of a group
of fields may apply to more than a single record in the table, consider placing those fields in a separate table.

For example, in an Employee Recruitment table, a candidate's university name and address may be included. But you need a complete list
of universities for group mailings. If university information is stored in the Candidates table, there is no way to list universities
with no current candidates. Create a separate Universities table and link it to the Candidates table with a university code key.

EXCEPTION: Adhering to the third normal form, while theoretically desirable, is not always practical.
If you have a Customers table and you want to eliminate all possible interfield dependencies,
you must create separate tables for cities, ZIP codes, sales representatives, customer classes, and any other factor that may be
duplicated in multiple records. In theory, normalization is worth pursing. However, many small tables may degrade performance or
exceed open file and memory capacities.


Other Normalization Forms

Fourth normal form, also called Boyce Codd Normal Form (BCNF), and fifth normal form do exist, but are rarely considered in practical
design. Disregarding these rules may result in less than perfect database design, but should not affect functionality.

For More Information Click

Partial Class in C#

Partial Class in C#

It is possible to split the definition of a class or a struct, or an interface over two or more source files.
Each source file contains a section of the class definition, and all parts are combined when the application is compiled.
There are several situations when splitting a class definition is desirable:

When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.

When working with automatically generated source, code can be added to the class without having to recreate the source file.
Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on.
You can create code that uses these classes without having to edit the file created by Visual Studio.

Example for Partial Class
public partial class Employee
{
public void DoWork()
{
}
}

public partial class Employee
{
public void GoToLunch()
{
}
}

At compile time, attributes of partial-type definitions are merged. For example, the following declarations:


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...