.Net
.Net
.Net
NET, it gives a feeling that it is something to do only with internet or networked applications. Even though it is true that .NET provides solid foundation for developing such applications it is possible to create many other types of applications. Following list will give you an idea about various types of application that we can develop on .NET. 1. ASP.NET Web applications: These include dynamic and data driven browser based applications. 2. Windows Form based applications: These refer to traditional rich client applications. 3. Console applications: These refer to traditional DOS kind of applications like batch scripts. 4. Component Libraries: This refers to components that typically encapsulate some business logic. 5. Windows Custom Controls: As with traditional ActiveX controls, you can develop your own windows controls. 6. Web Custom Controls: The concept of custom controls can be extended to web applications allowing code reuse and modularization. 7. Web services: They are web callable functionality available via industry standards like HTTP, XML and SOAP. 8. Windows Services: They refer to applications that run as services in the background. They can be configured to start automatically when the system boots up. .NET Framework SDK .NET Framework SDK - that provides classes, interfaces and language compilers necessary to program for .NET. Additionally it contains excellent documentation and Quick Start tutorials that help you learn .NET technologies with ease.
Development Tools: In such cases you require some Integrated Development Environment (IDE) that allows for Rapid Action Development (RAD). The new Visual Studio.NET is such an IDE. VS.NET is a powerful and flexible IDE that makes developing .NET applications a breeze. Some of the features of VS.NET that make you more productive are: - Drag and Drop design - IntelliSense features - Syntax highlighting and auto-syntax checking Excellent debugging tools - Integration with version control software such as Visual Source Safe (VSS) - Easy project management Visual Studio.NET Editions
- Professional - Enterprise Developer - Enterprise Architect Enterprise Developer The features include things such as a collaborative team development AND built-in project templates with architectural guidelines
Visual Studio .NET Enterprise Architect (VSEA) edition contains all the features of Visual Studio .NET Enterprise Developer edition and additionally includes capabilities for designing, specifying, and communicating application architecture and functionality. The additional features include Visual designer for XML Web services, Unified Modeling Language (UML) support and enterprise templates for development guidelines and policies. .NET Redistributable In order to run application developed using .NET Framework the machine must have certain runtime files installed. They are collectively called as .NET redistributable. .NET redistributable provides one redistributable installer that contains the common language runtime The redistributable is available as a stand-alone executable and can be installed manually or as a part of your application setup. Note that if you have installed .NET Framework SDK, there is no need of installing redistributable separately. Features of .NET Rich Functionality out of the boX: .NET framework provides a rich set of functionality out of the box. It contains hundreds of classes that provide variety of functionality ready to use in your applications. Easy development of web applications ASP.NET is a technology available on .NET platform for developing dynamic and data driven web applications. ASP.NET provides an event driven programming model (similar to Visual Basic 6 that simplify development of web pages (now called as web forms) with complex user interface.(user interface elements like calendar and grids) OOPs Support The philosophy of .NET is Object is mother of all. Languages like Visual Basic.NET now support many of the OO features that were lacking traditionally. Even primitive types like integer and characters can be treated as objects Multi-Language Support . .NET provides something attractive in this area. It supports multiple languages. This means that if you have skills in C++, you need not throw them but just mould them to suit .NET environment. Currently four languages are available right out of the box namely Visual Basic.NET, C# (pronounced as C-sharp), Jscript.NET and Managed C++ (a dialect of Visual C++). Multi-Device Support
. . .NET provides promising platform for programming PDAs, mobiles. .NET Compact Framework and Mobile Internet Toolkit are step ahead in this direction. Automatic memory management The garbage collector takes care of freeing unused objects at appropriate intervals. No more DLL Hell If you have worked with COM components, you probably are aware of DLL hell. DLL conflicts are a common fact in COM world. The main reason behind this was the philosophy of COM one version of component across machine. Also, COM components require registration in the system registry. .NET ends this DLL hell by allowing applications to use their own copy of dependent DLLs. Also, .NET components do not require any kind of registration in system registry. Strong XML support Now days it is hard to find a programmer who is unaware of XML. XML has gained such a strong industry support that almost all the vendors have released some kind of upgrades or patches to their existing software to make it XML compatible. Currently, .NET is the only platform that has built with XML right into the core framework. .NET tries to harness power of XML in every possible way. In addition to providing support for manipulating and transforming XML documents, .NET provides XML web services that are based on standards like HTTP, XML and SOAP. Ease of deployment and configuration Deploying windows applications especially that used COM components were always been a tedious task. Since .NET does not require any registration as such, much of the deployment is simplified. The configuration is done via special files having special XML vocabulary. Since, most of the configuration is done via configuration files, there is no need to sit in front of actual machine and configure the application manually. Security Microsoft has taken great efforts to make .NET platform safe and secure for enterprise applications. Features such as type safety, code access security and role based authentication make overall application more robust and secure. First we will understand about the 2- tier, 3- tier and then move on to N- tier Windows DNA(Windows Distributed interNet Applications). Why to divide an application into logical layers? Breaking a large piece of software into smaller pieces can make it easier to build, easier to reuse and easier to modify. 2tier:
A two-tiered application is an application whose functionality can only be segmented into two logical tiers, presentation services and data services. The presentation services of a two-tiered application are responsible for gathering information from the user, interacting with the data services to perform the application's business operations, and presenting the results of those operations to the user. presentation layer include a Web browseror even a character-based user interface. A web browser talking to a web server is an example of a client talking to a server. Here there is presentation logic (presentation tier) happening at the client, and data/file access (data access tier) and logic happening at the server. 3 Tier: Client Server The presentation services tier is responsible for: Gathering information from the user Sending the user information to the business services for processing Receiving the results of the business services processing Presenting those results to the user The business services tier is responsible for: Receiving input from the presentation tier. Interacting with the data services to perform the business operations. Sending the processed results to the presentation tier. The data services tier is responsible for the: Storage of data. Retrieval of data. Maintenance of data. Integrity of data. In Windows DNA applications commonly implement their business logic using one or more of three implementation options. Asp Pages COM components Stored procedures running in the DBMS Microsoft .net solutions:
N-tier architecture with .NET Let us look into how .Net fit into n tier architecture. When you talk about a true distributed n-tier type of application, you are talking about separating the components of the different tiers on different machines Constituents of .NET Platfor The Common Language Runtime (CLR) At the base is the CLR. It is considered as the heart of the .NET framework. .NET applications are compiled to a common language known as Microsoft Intermediate Language or IL. The CLR, then, handles the compiling the IL to machine language, at which point the program is executed. The CLR environment is also referred to as a managed environment, in which common services, such as garbage collection and security, are automatically provided. The .NET Class Framework
The next layer up in the framework is called the .NET Class Framework also referred as .NET base class library. The .NET Class Framework consists of several thousand type definitions, where each type exposes some functionality.
ADO.NET
ADO.NET is a set of classes that expose data access services to the .NET programmer. ADO.NET provides a rich set of components for creating distributed, data-sharing applications. It is an integral part of the .NET Framework, providing access to relational, XML, and application data.
Introduction
The first day I began to study ADO.NET I got confused because of new classes that Microsoft introduce in .NET Framework. In this article I want to introduce some basic classes of ADO.Net, hopefully those beginner who want to learn ADO.NET will not get confused like I did.
Provider Objects
There are the objects define in each .NET data provider.The name are prefaced with a name unique to the provider.For example,the actual connection object for OLE DB provider is OleDBConnection;(this use for MS Access,Oracle...).Provider for SQLServer is SqlConnection,and the ODBC .NET provider connection class isOdbcConnection.
Connection Object
I named them in the previous paragragh,this object is the first object that we have to use and needed before using any other ADO.NET objects.Obviously,it makes connection string to the data source.
Collapse | Copy Code
Or this one:
Collapse | Copy Code
SqlConnection myConnection = new SqlConnection("Data Source=(local);" + "Integrated Security=SSPI;"+ "Initial Catalog=Test"); // Open connection myConnection.Open();
Command Object
We use this object to give command such as a SQL query to a data source,for example SELECT * FROM TABLE. The provider specific names are SqlCommand and OleDBCommand.
Collapse | Copy Code
SqlCommand myCommand = myConnection.CreateCommand(); //this code is from MSDN with a little change myCommand.CommandText = "select count(*) as NumberOfRegions from region"; Int count = (int) myCommand.ExecuteScalar();
ExecuteScalar() return first column of the first row in the resultset.For more information about this method you can check MSDN.
CommandBuilder Object
This object is used to build SQL commands for data modification from objects based on a single table query. Provider names are SqlCommandBuilder and OleDBCommandBuilder.I'll give you an example soon.
DataReader Object
This is fast and simple object to use which reads a forward-only read-only stream of data from data source.This object gives the maximum performance for simply reading data.The providers name are SqlDataReader for SQLServer and OleDBDataReader for OLE DB.Imagine these line of code in a console application:
Collapse | Copy Code
// create connection object for Microsoft Access OLE DB Provider OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=C:\Test.MDB"); // open connection object myConnection.Open(); // create SQL command object on this connection OleDbCommand myCommand = myConnection.CreateCommand(); // initialize SQL SELECT command to retrieve desired data myCommand.CommandText = "SELECT Column1 FROM Table1"; // create a DataReader object based on previously defined command object
myReader["Column1"]);
The output of this code is all rows of column1 from table1.Don't forget to close both Connection andDataReader at the end of your operations.
DataAdapter Object
This class is for general purpose.It can performs lots of operation to the data source,like upadting changed data and another operation. The providers name are SqlDataAdapter for SQLServer and OledbDataAdapter for OLE DB.
Collapse | Copy Code
OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=C:\Test.MDB"); myConnection.Open(); OleDbDataAdapter myAdapter = new OleDbDataAdapter("SELECT Column1 from Table1", myConnection); Console.WriteLine("{0}\n",myAdapter.SelectCommand.CommandText); OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdapter); OleDbCommand insertCommand = myBuilder.GetInsertCommand(); Console.WriteLine("{0}\n",insertCommand.CommandText);
SELECT Column1 from Table1 INSERT INTO 'Column1' ('Column1') VALUES (?)
Consumer Objects
DataSet Object
It represents a set of related tables refrenced as one unit in your application.For example Table1,Table2 and Table3might all be tables in one DataSet.With this object you can get all the data in each table quickly,examine,change it while disconnected from server,and then update the server with the changes in one efficient operation.
Collapse | Copy Code
// Create DataAdapter object for update and other operations SqlDataAdapter myAdapter
= new SqlDataAdapter("SELECT * FROM Table1", myConnection); // Create DataSet to contain related data tables, rows, and columns DataSet myDataSet = new DataSet(); // Fill DataSet using query defined previously for DataAdapter myAdapter.Fill(myDataSet, "Table1"); // Show data before change Console.WriteLine("Record before change: {0}", myDataSet.Tables["Table1"].Rows[3]["Column1"]); // Change data in Table1, row 3, Column1 myDataSet.Tables["Table1"].Rows[3]["Column1"] = "Hello"; // Show data after change Console.WriteLine("Record after change: {0}", myDataSet.Tables["Table1"].Rows[3]["Column1"]); // Call Update command to mark change in table myAdapter.Update(myDataSet, "Table1");
Don't forget, you have to use Fill() method for your DataSet class to do operation in it.
Another Objects
There are some other simple objects,DataTable,DataColumn,DataRow and it is very clear what they are. This codes add new row to data source.
Collapse | Copy Code
DataRow myRow = myDataSet.Tables["Table1"].NewRow(); myRow["Column1"] = "Hi"; myRow["Column2"] = "How do you do?"; myDataSet.Tables["Table1"].Rows.Add(myRow); myAdapter.Update(myDataSet, "Table1");
User Interface The next layer consists of the user and programming interface that allows .NET to interact with the outside world. The following are the types of interaction interfaces that are supported by the .NET framework: Web Forms Windows Forms Web Services Languages: Any language that conforms to the CLS can run on the CLR. CLS is the the minimum set of features that their compilers must support if they are to target the runtime. .NET Products Microsoft Visual Studio .NET Microsoft Visual Studio .NET represents the best development environment for the .NET platform. .NET Services: XML is revolutionizing how applications talk to other applicationsor more broadly, how computers talk to other computersby providing a universal data format that lets data be easily adapted or transformed:
They are based on XML, the universal language of Internet data exchange, and can be called across platforms and operating systems, regardless of programming language. .NET is a set of Microsoft software technologies for connecting your world of information, people, systems, and devices through the use of XML Web services. .NET Runtime: Code developed with a language compiler that targets the runtime is called managed code. To enable the runtime to provide services to managed code, language compilers must emit metadata, which the runtime uses to locate and load classes, lay out instances in memory, resolve method invocations, generate native code, enforce security, and set run-time context boundaries. The runtime automatically handles objects, releasing them when they are no longer being used. Objects whose lifetimes are managed in this way are called managed data. Automatic memory management eliminates memory leaks as well as many other common programming errors. The CLR makes it easy to design components and applications whose objects interact across languages. For example, you can define a class and then use a different language to derive a class from your original class, or call a method on the original class. You can also pass an instance of a class to a method on a class written in a different language. This crosslanguage integration is possible because of the common type system defined by the runtime, and they follow the runtime's rules for defining new types, as well as for creating, using, persisting, and binding to types. Performance improvements. The ability to easily use components developed in other languages. Extensible types provided by a class library. A broad set of language features. In this section we will cover some of the more significant features provided to .NET applications by the CLR. These include: Memory Management Common Type System Automatic Memory Management The Garbage Collector (GC) is responsible for collecting the objects no longer referenced by the application. The GC may automatically be invoked by the CLR or the application may explicitly invoke the GC by calling GC.Collect. Common type system: Common Type System The Common Type System defines how data types are declared, used, and managed in the runtime, and is also an important part of the runtimes support for the Cross- Language Integration. The common type system performs the following functions: Establishes a framework that enables cross-language integration, type safety, and high performance code execution. Provides an object-oriented model that supports the complete implementation of many programming languages. Defines rules that languages must follow, which helps ensure that objects written in different languages can interact with each other. The Common Type System can be divided into two general categories of types, and Value type each of which is further divided into subcategories. Common Type System Architecture Reference type
The .NET type system has two different kinds of types namely Value types and Reference types. Value types directly contain the data, and instances of value types are either allocated on the stack or allocated inline in a structure. Value types can be built-in (implemented by the runtime), user-defined, or enumerations. The core value types supported by the .NET platform reside within the root of the System namespace. There types are often referred to as the .NET Primitive Types. They include: Boolean Byte Char DateTime Decimal Double Guid Int16 Int32 Int64 SByte Single Timespan Reference types store a reference to the value's memory address, and are allocated on the heap. Reference types can be self-describing types, pointer types, or interface types. The type of a reference type can be determined from values of self- describing types. Selfdescribing types are further split into arrays and class types. Value Type vs. Reference Type The primary difference between reference and value types is how instances of the two types are treated by the CLR. One difference is that the GC collects instances of reference types that are no longer referenced by the application. Another difference is when one variable is set equal to another or passed as a parameter to a method call. When a variable of a reference type (A) is set equal to another variable of the same type (B), variable A is assigned a reference to B. Both variables reference the same object. When a variable of value type (A) is set equal to another variable of the same type (B), variable A receives a copy of the contents of B. Each variable will have its own individual copy of the data. Custom Types A Custom Type is a set of data and related behavior that is defined by the developer. A developer can define both custom reference type and custom value types. In vb.net we can define custom types by using the Structure keyword. Lets look at an example wherein we define a custom value type. Boxing and Unboxing Value Types Sometimes it is required to treat an instance of a value type as if it were an instance of a reference type. An example of this is when a value type is passed ByRef as a parameter of a method. This is where the concept of Boxing becomes important. Boxing occurs when an instance of a value type is converted to a reference type. An instance of a value type can be converted either to a System.Object or to any other interface type implemented by the value type The .NET Class Framework Just-In-Time Compilation (JIT) After they are in this intermediate language, a process called Just-In-Time (JIT) compilation occurs. JIT allows parts of your application to execute when they are needed, which means that if something is never needed, it will never compile down to the native code. By using the JIT, the CLR can cache code that is used more than once and reuse it for subsequent calls, without going through the compilation process again.
Language Features Of c#: All .NET languages use, at their root, functionality from the set of classes provided by the .NET Framework. Therefore, everything you can do in VB.NET you can do in C#, and vicea-versa.
The differences occur in three main areas: syntax, object-oriented principles, and the Visual Studio .NET IDE. IDE differences include things like compiler settings or attributes. There is also a fourth area of difference: language features that are present in one language but have no equivalent in the other. The programming language C# derives from C and C++; however apart from being entirely object oriented it is type safe and simple too C# provides you with convenient features like garbage collection Constants & Variables A variable is a named memory location. They are programming elements that can change during program execution Variables are declared as follows int a; They can also be initialized at the time of declaration as follows: int a = 10; Constants are very similar to variables. The main difference is that the value contained in memory cannot be changed once the constant is declared. When you declare a constant its value is also specified and this value cannot be changed during program execution Constants are declared as follows const int a;
Simple Types (Primitive Data types) Simple or value type variables are those, which are assigned space in the stack instead of the heap. All the primitive types such as int, double etc are value type variables. The simple types basically consist of Boolean and Numeric types, where Numeric is further divided into Integral and Floating Point. The first rule of value types is that they cannot be null. Anytime you declare a variable of value type, you have allocated the number of bytes associated with that type on the stack and are working directly with that allocated array of bits. In addition, when you pass a variable of value type, you are passing that variables value and not a reference to the underlying object. Object Type Object type or reference type variables are those, which are allocated storage space in the heap. Reference type objects can be null. When a reference type is allocated under the covers a value is allocated on the heap and a reference to that value is returned. There are basically four reference types: classes, interfaces, delegates and arrays.
That was a simple explanation of what happens in the memory, but depending on the data type, your variable is allocated that type of memory. There are two types of memory allocation: stack memory and heap memory. In the coming sections, we will try to understand these two types of memory in more detail.
public void Method1() { // Line 1 int i=4; // Line 2 int y=2; //Line 3 class1 cls1 = new class1(); }
Its a three line code, lets understand line by line how things execute internally.
Line 1: When this line is executed, the compiler allocates a small amount of memory in the stack. The stack is responsible for keeping track of the running memory needed in your application. Line 2: Now the execution moves to the next step. As the name says stack, it stacks this memory allocation on top of the first memory allocation. You can think about stack as a series of compartments or boxes put on top of each other. Memory allocation and de-allocation is done using LIFO (Last In First Out) logic. In other words memory is allocated and de-allocated at only one end of the memory, i.e., top of the stack.
Line 3: In line 3, we have created an object. When this line is executed it creates a pointer on the stack and the actual object is stored in a different type of memory location called Heap. Heap does not track running memory, its just a pile of objects which can be reached at any moment of time. Heap is used for dynamic memory allocation.
One more important point to note here is reference pointers are allocated on stack. The statement, Class1 cls1;does not allocate memory for an instance of Class1, it only allocates a stack variable cls1 (and sets it to null). The time it hits the new keyword, it allocates on "heap". Exiting the method (the fun): Now finally the execution control starts exiting the method. When it passes the end control, it clears all the memory variables which are assigned on stack. In other words all variables which are related toint data type are de-allocated in LIFO fashion from the stack. The big catch It did not de-allocate the heap memory. This memory will be later de-allocated by the garbage collector.
Delegates: With delegates, a program can dynamically call different methods at runtime. The closest equivalent of a delegate in other languages is a function pointer, but whereas a function pointer can only reference static functions, a delegate can reference both static and instance methods.