Total Pageviews

Thursday, September 13, 2012

Inheritance

A class will inherit the members of its direct base class. The upshot of inheritance is that a class will
implicitly contain all members of its direct base class, except for any instance constructors, finalizers,
and static constructors.
A derived class can add new members to those it inherits, but it cannot remove the definition of an
inherited member.
Instance constructors, finalizers, and static constructors are not inherited, but all other members are.
A class can declare virtual methods, properties, indexers, and events, and derived classes can override
the implementation of these function members.
Members inherited from a constructed generic type are inherited after type substitution.

Tuesday, September 11, 2012

Abstract Class Design

Because abstract classes should never be instantiated, it is important to correctly define their constructors. It is also important to ensure that the functionality of your abstract class is correct and easily extended. The following guidelines help ensure that your abstract classes are correctly designed and work as expected when implemented.

Do not define public or protected internal (Protected Friend in Visual Basic) constructors in abstract types.
Constructors with public or protected internal visibility are for types that can be instantiated. Abstract types can never be instantiated.

Do define a protected or an internal constructor in abstract classes.
If you define a protected constructor in an abstract class, the base class can perform initialization tasks when instances of a derived class are created. An internal constructor prevents the abstract class from being used as the base class of types that are not in the same assembly as the abstract class.

Do provide at least one concrete type that inherits from each abstract class that you ship.
This practice helps library designers locate problems or oversights in the abstract class design. It also means that for high-level scenarios where developers might not understand abstract classes and inheritance, they can use the concrete class without having to learn these concepts. For example, the .NET Framework provides the abstract classes WebRequest and WebResponse to handle sending requests to, and receiving replies from a Uniform Resource Identifier. As one of several concrete implementations for these abstract classes, the Framework includes the HttpWebRequest and HttpWebResponse classes, which are HTTP-specific implementations of the abstract classes.

Abstract Class

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method.
Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. For example:


// compile with: /target:library
  
  public class D
  {
      public virtual void DoWork(int i)
      {
          // Original implementation.
  
      }
  }
  
  public abstract class E : D
  {
      public abstract override void DoWork(int i);
  }
  
  public class F : E
  {
      public override void DoWork(int i)
      {
          // New implementation.
  
      }
  }
 
 
 
Define Abstract Properties 
 An abstract property declaration does not provide an implementation of the 
property accessors -- it declares that the class supports properties, but leaves 
the accessor implementation to derived classes. 
 
public abstract double Area
      {
          get;
      }
 

Static Class and Singleton Pattern



Static classes and singletons both provide sharing of redundant objects in memory, but they are very different in usage and implementation.

Static Class - You can not create the instance of static class.
Singleton pattern - you can create one instance of the object and reuse it.
Static classes- are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Singleton instance is created for the first time when the user requested.
Static Class class cannot have constructor.
singleton class can have constructor.


++++++++++++++++++
static members can operate only on static data and call static methods of the
defining class. If you attempt to make use of nonstatic class data or call a nonstatic method of the class within a
static member’s implementation, you’ll receive compile-time errors.Singleton has a private constructor
Use singleton as parameter.

Static Class - You can not create the instance of static class.
Singleton pattern - you can create one instance of the object and reuse it.
Static classes- are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Singleton instance is created for the first time when the user requested.
Static Class class cannot have constructor.
singleton class can have constructor.

http://www.dotnetspider.com/forum/145836-Difference-between-Static-class-Singleton-Instance.aspx


Introducing the Singleton Pattern
Our ideal solution is called the singleton design pattern. Here is an implementation of a singleton that I will use. As you know, a singleton is a single-instance object. It is highly efficient and very graceful. Singletons have a static property that you must access to get the object reference.



Here we looked at some aspects of the singleton design pattern in the C# language. Singletons allow you to reuse code and control object state much easier. This improves code-sharing, and can result in a far cleaner body of code. With less code, your programs will usually have fewer bugs and will be easier to maintain.

Monday, September 10, 2012

Types of Indexes on tablels

The following table lists the types of indexes available in SQL Server and provides links to additional information.

Index type Description Additional information
ClusteredA clustered index sorts and stores the data rows of the table or view in order based on the clustered index key. The clustered index is implemented as a B-tree index structure that supports fast retrieval of the rows, based on their clustered index key values.Clustered Index Design Guidelines
Clustered Index Structures
NonclusteredA nonclustered index can be defined on a table or view with a clustered index or on a heap. Each index row in the nonclustered index contains the nonclustered key value and a row locator. This locator points to the data row in the clustered index or heap having the key value. The rows in the index are stored in the order of the index key values, but the data rows are not guaranteed to be in any particular order unless a clustered index is created on the table.Nonclustered Index Design Guidelines
Nonclustered Index Structures
UniqueA unique index ensures that the index key contains no duplicate values and therefore every row in the table or view is in some way unique.
Both clustered and nonclustered indexes can be unique.
Unique Index Design Guidelines
Index with included columnsA nonclustered index that is extended to include nonkey columns in addition to the key columns.Index with Included Columns
Indexed viewsAn index on a view materializes (executes), the view and the result set is permanently stored in a unique clustered index in the same way a table with a clustered index is stored. Nonclustered indexes on the view can be added after the clustered index is created.Designing Indexed Views
Full-text A special type of token-based functional index that is built and maintained by the Microsoft Full-Text Engine for SQL Server. It provides efficient support for sophisticated word searches in character string data. Full-Text Indexes
Spatial A spatial index provides the ability to perform certain operations more efficiently on spatial objects (spatial data) in a column of the geometry data type. The spatial index reduces the number of objects on which relatively costly spatial operations need to be applied. Spatial Indexing Overview
FilteredAn optimized nonclustered index, especially suited to cover queries that select from a well-defined subset of data. It uses a filter predicate to index a portion of rows in the table. A well-designed filtered index can improve query performance, reduce index maintenance costs, and reduce index storage costs compared with full-table indexes.Filtered Index Design Guidelines
XMLA shredded, and persisted, representation of the XML binary large objects (BLOBs) in the xml data type column.Indexes on XML Data Type Columns

 

Indexing Tables

An index is an on-disk structure associated with a table or view that speeds retrieval of rows from the table or view. An index contains keys built from one or more columns in the table or view. These keys are stored in a structure (B-tree) that enables SQL Server to find the row or rows associated with the key values quickly and efficiently.
A table or view can contain the following types of indexes:
  • Clustered

    • Clustered indexes sort and store the data rows in the table or view based on their key values. These are the columns included in the index definition. There can be only one clustered index per table, because the data rows themselves can be sorted in only one order.

    • The only time the data rows in a table are stored in sorted order is when the table contains a clustered index. When a table has a clustered index, the table is called a clustered table. If a table has no clustered index, its data rows are stored in an unordered structure called a heap.
  • Nonclustered

    • Nonclustered indexes have a structure separate from the data rows. A nonclustered index contains the nonclustered index key values and each key value entry has a pointer to the data row that contains the key value.

    • The pointer from an index row in a nonclustered index to a data row is called a row locator. The structure of the row locator depends on whether the data pages are stored in a heap or a clustered table. For a heap, a row locator is a pointer to the row. For a clustered table, the row locator is the clustered index key.

    • You can add nonkey columns to the leaf level of the nonclustered index to by-pass existing index key limits, 900 bytes and 16 key columns, and execute fully covered, indexed, queries.
How Indexes Are Used by the Query Optimizer
Well-designed indexes can reduce disk I/O operations and consume fewer system resources therefore improving query performance. Indexes can be helpful for a variety of queries that contain SELECT, UPDATE, DELETE, or MERGE statements. Consider the query SELECT Title, HireDate FROM HumanResources.Employee WHERE EmployeeID = 250 in the AdventureWorks database. When this query is executed, the query optimizer evaluates each available method for retrieving the data and selects the most efficient method. The method may be a table scan, or may be scanning one or more indexes if they exist.
When performing a table scan, the query optimizer reads all the rows in the table, and extracts the rows that meet the criteria of the query. A table scan generates many disk I/O operations and can be resource intensive. However, a table scan could be the most efficient method if, for example, the result set of the query is a high percentage of rows from the table.
When the query optimizer uses an index, it searches the index key columns, finds the storage location of the rows needed by the query and extracts the matching rows from that location. Generally, searching the index is much faster than searching the table because unlike a table, an index frequently contains very few columns per row and the rows are in sorted order.
The query optimizer typically selects the most efficient method when executing queries. However, if no indexes are available, the query optimizer must use a table scan. Your task is to design and create indexes that are best suited to your environment so that the query optimizer has a selection of efficient indexes from which to select. SQL Server provides the Database Engine Tuning Advisor to help with the analysis of your database environment and in the selection of appropriate indexes.