Showing posts with label Dot Net. Show all posts
Showing posts with label Dot Net. Show all posts

Sunday, September 26, 2010

Two-Tier Architecture

In case of two-tier architecture, an application is broadly divided into two
  • Client:Implements the user interface
  • Server:Storage of data

In case of this architecture the user and data services are either on the same machine or on the different machines.In two-tier architecture,the business layers are implemented in either of the following methods:

Two-Tier Architecture

  1. Fat Client

    In this type of method business service layer is combined with user service layer.Clients execute the presentation logic and enforce business logic.server stores data and process transactions.this is used when server is loaded with transaction processing activities and is not equipped to process business logic

  2. Fat Server

    Here business service layer is combined with data service layer.As business service is stored on the server, most of the processing takes place on server.

  3. Dividing business services between the user and data services

    You can also implemented in such a way that business services are distributed between user and data services.Here processing of business logic is distributed between data and user services.

Single-Tier Architecture

Early we have discussed about the application architecture .On the basis of that we can explain what is single tier architecture?.In case of Single-tier architecture a single executable file handles all the functions relating to the user,business and data service layers.This is also called Monolithic application.

Single-Tier Architecture

Some of early COBOL programs using this architecture.

Types of Application architectures

Applications are developed to support the organisations in their business operations.Application receives input and accept it and process the data based on business rules and provides data as output.The functions performed by an application can be classified into three types
  1. User Service
  2. Business Service
  3. Data Service
  • User Service
This is regarded as the front end of the solution.This is also called presentation layer because it provides interactive user interface.
  • Business Service
It controls the enforcement of business rules of the data of an organisation. Business rules encircle those practices and activities that define the behaviour of an organisation. Eg: an organisation decides that the credit limit of all client cannot exceed $200000. Business service layer sets rules or validations to pertain these functions.This means the back end does not receive incorrect data
  • Data Service
It comprises the data and functions for manipulating this data.

These three layer form the base of models or architecture used in application development in an organisation.Applications can be

Wednesday, September 15, 2010

Polymorphism in C#

  • When a message can be processed in different ways is called polymorphism."Poly" means many
  • When you derive a class from a base class, the derived class will inherit all members of the base class except constructors, though whether the derived class would be able to access those members would depend upon the accessibility of those members in the base class
  • C# gives us polymorphism through inheritance. Inheritance-based polymorphism allows us to define methods in a base class and override them with derived class implementations. Thus if you have a base class object that might be holding one of several derived class objects, polymorphism when properly used allows you to call a method that will work differently according to the type of derived class the object belongs to
  • It allows you to invoke methods of derived class through base class reference during runtime
  • It has the ability for classes to provide different implementations of methods that are called through the same name
Polymorphism is of two types:
  • Compile time polymorphism/Overloading
  • Runtime polymorphism/Overriding
Compile Time Polymorphism
  • Compile time polymorphism is method and operators overloading. It is also called early binding
  • In method overloading method performs the different task at the different input parameters
  • Use method overloading in situation where you want a class to be able to do something, but here is more than one possibility for what information is supplied to the method that carries out the task
  • You should consider overloading a method when you for some reason need a couple of methods that take different parameters, but conceptually do the same thing
Example
 using System;
namespace method_overloading

{
class Program
{
public class Print
{
public void display(string name)

{
Console.WriteLine("Your name is : " + name);
}

public void display(int age, float marks)

{
Console.WriteLine("Your age is : " + age);

Console.WriteLine("Your marks are :" + marks);
}
}

static void Main(string[] args)

{
Print obj = new Print();

obj.display("George");

obj.display(34, 76.50f);

Console.ReadLine();
}
}
}
Main points to be remembered while overloading are
  • If you use overload for method, there are couple of restrictions that the compiler imposes
  • The rule is that overloads must be different in their signature, which means the name and the number and type of parameters
  • There is no limit to how many overload of a method you can have. You simply declare them in a class, just as if they were different methods that happened to have the same name
Here is an example to overload multiple methods
using System;
namespace method_overloading_polymorphism
{
class Program
{
public class Shape

{
public void Area(float r)
{
  float a = (float)3.14 * r;

  // here we have used funtion overload with 1 parameter.

  Console.WriteLine("Area of a circle: {0}",a);
}

public void Area(float l, float b)

{
  float x = (float)l* b;

  // here we have used funtion overload with 2 parameters.

  Console.WriteLine("Area of a rectangle: {0}",x);

}
public void Area(float a, float b, float c)

{
  float s = (float)(a*b*c)/2;

  // here we have used funtion overload with 3 parameters.

  Console.WriteLine("Area of a circle: {0}", s);
}
}
static void Main(string[] args)
{
Shape ob = new Shape();

ob.Area(2.0f);

ob.Area(20.0f,30.0f);

ob.Area(2.0f,3.0f,4.0f);

Console.ReadLine();
}
}
}
Runtime Time Polymorphism
  • Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism
  • It is also called late binding.
Let's assume the following simple class hierarchy with classes A, B and C for the discussions in this text. A is the super- or base class, B is derived from A and C is derived from class B. In some of the easier examples, we will only refer to a part of this class hierarchy.

Inherited Methods

A method Foo() which is declared in the base class A and not redeclared in classes B or C is inherited in the two subclasses

using System;
namespace Polymorphism
{
class A
{
 public void Foo() { Console.WriteLine("A::Foo()"); }
}

class B : A {}

class Test
{
 static void Main(string[] args)
 {
     A a = new A();
     a.Foo();  // output --> "A::Foo()"

     B b = new B();
     b.Foo();  // output --> "A::Foo()"
 }
}
}
The method Foo() can be overridden in classes B and C:
using System;
namespace Polymorphism
{
class A
{
     public void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A
{
     public void Foo() { Console.WriteLine("B::Foo()"); }
}
class Test
{
   static void Main(string[] args)
   {
       A a;
       B b;
       a = new A();
       b = new B();
       a.Foo();  // output --> "A::Foo()"
       b.Foo();  // output --> "B::Foo()"
       a = new B();
       a.Foo();  // output --> "A::Foo()"
   }
}
}
There are two problems with this code.
  • The output is not really what we, say from Java, expected. The method Foo() is a non-virtual method. C# requires the use of the keyword virtual in order for a method to actually be virtual. An example using virtual methods and polymorphism will be given in the next section
  • Although the code compiles and runs, the compiler produces a warning: ...\polymorphism.cs(11,15): warning CS0108: The keyword new is required on 'Polymorphism.B.Foo()' because it hides inherited member 'Polymorphism.A.Foo()'

This issue will be discussed in section Hiding and Overriding Methods.

Virtual and Overridden Methods

Only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.

using System;
namespace Polymorphism
{
class A
{
    public virtual void Foo() { Console.WriteLine("A::Foo()"); }
}

class B : A
{
    public override void Foo() { Console.WriteLine("B::Foo()"); }
}

class Test
{
    static void Main(string[] args)
    {
        A a;
        B b;

        a = new A();
        b = new B();
        a.Foo();  // output --> "A::Foo()"
        b.Foo();  // output --> "B::Foo()"

        a = new B();
        a.Foo();  // output --> "B::Foo()"
    }
}
}

Method Hiding

Why did the compiler in the second listing generate a warning? Because C# not only supports method overriding, but also method hiding. Simply put, if a method is not overriding the derived method, it is hiding it. A hiding method has to be declared using the new keyword. The correct class definition in the second listing is thus:

using System;
namespace Polymorphism
{
  class A
  {
      public void Foo() { Console.WriteLine("A::Foo()"); }
  }

  class B : A
  {
      public new void Foo() { Console.WriteLine("B::Foo()"); }
  }

  class Test
  {
      static void Main(string[] args)
      {
          A a;
          B b;

          a = new A();
          b = new B();
          a.Foo();  // output --> "A::Foo()"
          b.Foo();  // output --> "B::Foo()"

          a = new B();
          a.Foo();  // output --> "A::Foo()"
      }
  }
}

Combining Method Overriding and Hiding

Methods of a derived class can both be virtual and at the same time hide the derived method. In order to declare such a method, both keywords virtual and new have to be used in the method declaration:

class A
       {
           public void Foo() {}
       }

       class B : A
       {
           public virtual new void Foo() {}
       }


A class C can now declare a method Foo() that either overrides or hides Foo() from class B:

       class C : B
       {
           public override void Foo() {}
           // or
           public new void Foo() {}
       }

Object Data Source in c#

  • Create App_Code Folder in your Project root.
  • right-click on the App_Code and select Add->Add New Item to add a new DataSet called "Sample.xsd" to the project.
  • Double click on DataSet Sample.xsd . You will get a tool box for your Data set.
  • Grab a Table Adapter control .You may see another Table Adapter as shown below .
  • Create appropriate connection string or select already created one.
  • Click Next and tick Use Sql Statements and Write a Query to select the rows.You can use the Query Builder for automatic queries and check the results there itself.
  • Rename DataTable1 to Sample (for an example) . Which renames the adapter also.
  • After successful creation of the sql query which creates a Method by default say GetData().
  • Right-click on the App_Code and select Add->Add New Item to add a new Class file called "AssignValues.cs" to the project. Double Click on the class and create a method in it
public class AssignValues

{
public void Fill(GridView gdv)

{
gdv.ID = string.Empty;

SampleTableAdapters.MemberTableAdapter mtadptr = null;        

mtadptr = new SampleTableAdapters.MemberTableAdapter();

Sample.MemberDataTable dt=null;

dt=mtadptr.GetData();

gdv.DataSourceID=string.Empty;

gdv.DataSource=dt;

gdv.DataBind();
}

}
Explanation for the above code is given below Grid view is an object parameter you passing it from in which event you need to fill the data.Then creating an object of Sample data set dt and filling the data into it and bind it. After that you can make an instance of the class and call the method in any other c# code page of the Project files. Pass the parameter Grid View say GridView1 after grabbing a Grid view from the Tool Box.

Monday, August 30, 2010

First post for Programmers

HI Friends, last day my father asks me to do some works on web on the basis of my software experience in the platform i am working.He told me that the benifits are three
  • You will learn that topic thouroughly
  • Makes the others to learn if somebody are inneed of that resources
  • You can earn from your blog by the contents
  • Your Laziness factor will be less to large extend also, if you starts writing
I know why he suggest me to that,because he knows very well about me that i am a lazy person(not in a large scale).I straight away told him that its possible but many things are to be done before doing that like collecting the theory part of thattopic and with simple steps i had to well explain that in each post by giving some screen casts and also its time consuming and i am lacking that too.Listening to my speech he just give me a silent smile and shake his head and went away.Even though it is a good smile but the way and the time of smile make me to think little bit more. So i started thinking of writing a blog which well describes the various concepts in my platform like Ruby on Rails and dot net.So i decided to write one blog which is well explains the ruby on rails applications and dot net basics.