Sunday, September 16, 2007

Finalize vs Dispose

In my opinion the difference between "Finalize" and "Dispose":

  • Finalize is always called by the GC, so we could never determine when this would happen.
  • Finalize cannot be overriden or called.
  • In order to use Dispose(), the class should impelement the IDisposable interface.
  • Dispose() is the right place for freeing up unmanaged resources like DB connection, handles, etc...
  • Dispose() is called when using the "using" block.

Thursday, August 2, 2007

Criteria of Structures

  • Logically represents a single value
  • Has an instance size less than 16 bytes
  • Will not be changed after creation
  • Will not be cast to a reference type

Thursday, June 21, 2007

Layers vs Tiers

In application designing, we always here about this tiers and layers words... N-tier, N-layer, etc... now what is the difference between the two? Simple, layers indicate the logical separation of code, wherein you assign a specific responsibility per layer while tiers relate to the physical distribution of the application for example it is more about where the process runs (client pc - web server - database server).

The Three Principal Layers

Presentation Layer - Display of information, collection of user input, command-line input, interfaces to external systems

Application Logic - Processing data, managing transactions

Data Access Layer - Interaction with data store, messaging systems, remote system access

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.

Tuesday, April 24, 2007

Guidelines for using method permission requests in C#

  • Use SecurityAction.PermitOnly declarations to limit the permissions available to each method.
  • Use Security.Deny declarations to further refine the permissions available to each method.
  • Use CodeAccessPermissoin.Assert when you want to allow partially trusted code to call a method that requires permissions the caller might lack.
  • Use CodeAccessPermission.PermitOnly to imperatively reduce permissions when a section of a method requires fewer permissions than the rest of the method.
  • Use CodeAccessPermission.Demand only when your assembly implements customized functionality that does not rely on functionality built into the .Net Framework, such as calls to unmanaged code.

Wednesday, April 18, 2007

Some info about Windows Services in 2.0

This are some tips i've learned about windows services:
  • to debug a windows service, we need to start the service first and attach the debugger to the service's process.
  • you must always create installation components for windows services.
  • the Main method of the service application should always issue the Run command for the services.
  • error messages should be logged in the windows event logger.
  • run your service with the fewest privileges possible to minimize potential damage.

Monday, April 16, 2007

String VS StringBuilder

A few months ago i was wondering what is the purpose of the stringbuilder class in C#, if what it can do can also be done in a simple String/string variable type... and is done, imao, with more ease.

For example let's look at string concatenation:

//StringBuilder
StringBuilder sbExample = new StringBuilder();
sbExample.Append("Some ");
sbExample.Append("Text Here...");
return sbExample.ToString();


//String
String strExample = "Some ";
strExample = strExample + "Text Here...";
return strExample;


In this example, it is much easier to perform string concatenation with a plain simple string type than the stringbuilder, now what exactly is the difference between the two?

The answer: i've been reading the MS Book Application Foundation and here i've learned that a string memory address is immutable... if you concatenate or replace the string value, it does not overwrite its current memory location and instead allocates a new memory location for the new string value, meanwhile with the stringbuilder it always use the same memory it is allocated with whenever you try to modify it's value...

eureka! now i know, for better optimization, when dealing with strings use stringbuilder instead of the basic string value type, there might be an increase in the effort in coding, but what is a little sacrifice for a little more optimized performance.

small things can become big things too ;)

Friday, April 13, 2007

A year of OOP practice

Well it's been a year since i started OOP programming, i've earned quite a few certifications in Microsoft, but still I found myself lost in this 3 letter abbreviated concept...

i've downloaded dozens of OOP ebooks, but frankly, i've not gone through the first chapter... hmmm, why? lazy me, nope i've been spending time studying somewhat kinda new technologies to me (i was a c programmer before) C#, .Net, ASP.Net, SQL Server 2000/2005, HTML(yep even this one!) etc.. etc..

i remembered when i first started with OOP, i've been hooked up to procedural programming for so long as i can remember that my codes looked like procedural programming codes... thousands of lines, redundant functions or should i say methods, seldom code reuse... but as days, weeks, months have gone by i could say my codes began to trim down, and started to look cleaner (for my current colleagues, yep it trimmed down), what a great improvement... thank god i've discovered inheritance, interfaces, properties and other wonderful techniques of OOP.

and now i must continue my journey in discovering and learning this wonderful technique...

OOP technique rocks!