Wednesday, May 30, 2007

Abstract Class VS Interfaces

As i was going thru some of my reviewers, I noticed that there are two types of classes in C# wherein they can't be instantiated and if you inherit either one of them, you should implement their methods. This are the abstract class and the Interface.

Now in my opinion this are the difference between the two:
  • An abstract class can have constructors while an Interface can't - Interfaces acts just like a blueprint of class except that it don't have any implementations, so what's the need of having a constructor if you wouldn't put in any implementation on it.
  • An abstract class can have concrete methods an Interface can't - Like a normal class an abstract class can have fields, concrete methods and constructors. An abstract class also could have access modifiers, again, just like a normal class.
  • You can do multiple inheritance with an Interface however this is not possible in an abstract class - an abstract class is like an incomplete class, and C# does not support multiple class inheritance, it would be a disaster.

So to sum it all up an abstract class is really like a class except that some of it's methods are not implemented or defined however an Interface is like a blueprint or a contract, it's methods should be implemented in the deriving class. Another thing is when you implement the methods of an abstract class you should add the override modifier to it, in interfaces you don't need to add it.

Wednesday, May 16, 2007

ADO.Net Transaction Object

A Transaction is an atomic unit of work that must be completed in its entirety. The transaction succeeds if it is committed and otherwise fails if it is aborted.

Essential properties of transactions (ACID):
  • Atomicity - work cannot be broken into smaller parts.
  • Consistency - data must always be consistent.
  • Isolation - the transaction should be running by itself and should not be affected nor affect other transactions.
  • Durability - committed transactions should always be persisted.

ex:

using (SqlConnection cn = new SqlConnection()) {
cn.ConnectionString = "CONNECTION STRING HERE!";
cn.Open();
using (SqlTransaction tran = cn.BeginTransaction()) {
try {

using (SqlCommand cmd = cn.CreateCommand()) {
cmd.Transaction = tran;
cmd.CommandText = "SQL TEXT HERE!";
// SQL EXECUTE HERE!
}
tran.Commit();
}
catch (Exception xcp) {
tran.Rollback();
}
}
}

Wednesday, May 2, 2007

Type of Scope Modifiers in C#

  • internal - defines a type, method or data member as public scope in the context of an assembly. The type, method, or data member cannot be accessed from outside the assembly.
  • internal protected - just like the internal scope method however the method or data member has public scope in the subclassed type.
  • private - defines a data member or method that is not accessible outside of the type defining the method or data member.
  • protected - methods and data members are scoped as private however if the method or data member is callable from a subclassed type.
  • public - defines method and data members accessible regardless of the assembly or program scope.