Friday, April 23, 2010

Abstraction and Encapsulation Defined

Abstraction

Abstraction is the ability to generalize an object as a data type that has a specific set of characteristics and is able to perform a set of actions.

Object-oriented languages provide abstraction via classes. Classes define the properties and methods of an object type.

Examples:

You can create an abstraction of an Employee with characteristics, such as FirstName, LastName, Department, Experience etc. and actions such as AssignTask, PerformTask and ReportTo etc. The characteristics are called properties, and the actions are called methods.

Encapsulation

Once you have your class properly design then you can create an object out of it. Object now contains the data and behaviors defined in the class, so a client application can treat this object like a black box accessible only through its interface (which mean its properties, methods/functions etc). This is a key object-oriented concept called Encapsulation.

The idea is that any programs that make use of this object won't have direct access to the behaviors or data – but rather those programs must make use of our object's interface.

Examples:

Employee objEmp = new Employee();
objEmp.FirstName = "Vidya Vrat";
objEmp.LastName= "Agarwal";

objEmp.AssignTask(objEmp.FirstName, objEmp.LastName);

Note: - consider that this function will do so check on your time availabilty and the allocate you some task to the employee provided to this function as an argument.

Finalizer in .NET

Besides we have .NET Garbage Collection to do the memory cleanup. But still in some cases you may wish to have your own Finalizer to do the cleanup.

There are some questions around it.

1 -When do I need to implement a finalizer on my class?
2- Should I always do it, or only when I'm holding onto unmanaged resources?
3- Should I always implement the IDisposable interface when I have a finalizer, and vice versa?

Explaination:-


Finalizers only need to be implemented when you hold onto resources that need cleaning up. For example, a FileStream which holds onto a native file handle. Unfortunately, finalizers place a significant amount of performance-hit and pressure on the GC and should be used only when it is absolutely needed.

What is IDisposable

The IDisposable interface allows users of your class to deterministically (as you know GC is Non-deterministic) do the clean-up. Therefore, any class that implements a finalizer should also implement IDisposable.

Scenarion: - Consider a scenario where my managed class has a FileStream as a private member. FileStream holds onto unmanaged resources and implements both IDisposable and a finalizer. When no more references to the instance exist, this FileStream will be similarly unreachable and will be available for finalization.

There's no reason for my class to be in the queue of objects that have registered for finalization since the instance of FileStream will be there. But my class should provide a way for a user to immediately release all resources it holds onto, either directly or indirectly, and therefore my class should implement IDisposable.

My implementation of Dispose will simply call the FileStream's Dispose method. Note, though, that you need to be careful not to dispose of shared resources (resources used by other instances, for example).

If you write a class that will dispose of a resource made available to it from an external source, make sure your documentation is clear on the subject so that others know whether they're handing over control of any resources they give you.

How to implement Dispose


If you do need to implement a finalizer in your class, your Dispose method should use the GC.SuppressFinalize method to ensure that finalization of your instance is suppressed.

This will remove the instance from the set of objects that require finalization, reducing the pressure on the GC during the collection process through heap.

A common pattern implemented throughout the Microsoft® .NET Framework is to add to a class a Dispose method that takes a Boolean as a parameter.

This Boolean indicates whether the class is being disposed because the IDisposable.Dispose method is called or because the finalizer is run (both the finalizer and IDisposable.Dispose delegate to this method).

If it's being disposed deterministically, GC.SuppressFinalize is invoked. If it's being called from a finalizer, avoid using managed members of your class that implement finalizers as they may have already been finalized.

Sunday, April 11, 2010

Limitations of GAC deployed assembly

1- Strong name key (.snk) musts be assigned to the assembly.
2- Only a .dll can be deployed in the GAC.
3- User created .dll will not be listed under "Add Reference --> .NET tab.
Only Visual Studio .NET specific assemblied are listed in there.

How to use a GAC deployed assembly in an application

Steps to use a GAC deployed assembly

1- A user created assembly deployed in the GAC can be seen Browse tab by navigating to the C:\Wiindows\Assembly
2- When your application needs to add the GAC deployed assembly you need to browse to the \bin\debug folder of the source .dll project.
3- It will be then listed under References folder.

Steps to deply an assembly in the GAC

GAC (Global Assembly Cache) is the .NET provided folder location to contain all the shared assemblies across .NET Framework and applications.

One of the basic and mandate requirement before any assembly could be deployed in the GAC is to Sign the assembly with Strong name.

---------------------------------------------
Steps to deploy an assembly into the GAC
---------------------------------------------

1- Sign the class-Library project generated .dll with Strong name.
Select the Class-Library project--> Right-click--> select Properties, under Signing tab, create a new Strong name key by passing a name you would like to have.
2- It will create a file with the provided name having .snk as file extenion.
it will be saved under the same project folder
3- Now if needed you can create various versions of the same assembly (.dll) by
giving desired version number to the assembly file in the AssemblyInfo.cs
4- When you Build the solution, it will generate the .dll with the provided version number (default is 1.0.0.0) in the application root's bin\debug folder.
5- After each build (ver 1.0.0.0 and 2.0.0.0) to separate folders, as its the same .dll and only version if different and \bin\debug doesn't differentiate between two versions.
6- Drag-drop the MathLibrary 1.0 and 2.0 into the GAC.

Monday, April 5, 2010

Difference between .NET assembly and a .dll/.exe

What is the difference you see between a .NET assembly and any other .dll or .exe?

Sunday, April 4, 2010

Talk on .NET assemblies

This is the talk session held on Sunday 4th April 2010 in the Pearl Solutions LLC's conference room at 14040 NE 8th ST Bellevue WA.


Presenter- Vidya Vrat Agarwal
Attendees - Mohd Shams,Purna, Naksha, Priyanka, Upasana, Ekta and Sukhpreet.