Friday, February 2, 2018

Application Architecture


Before start our journey with C#, let’s discuss the architecture of an application in .Net Framework. As we already know that there is a pretty good rule of Separation of Concern (SOC) in Software Engineering, it helps us to classify different modules and different sections with respect to their working separately and then we can use this module of code in our program where we want.
Similarly, in .Net Base Class Libraries to support different types of functions for a specific purpose like application connectivity with database, data filtration, to print something on the console there are different classes in different namespaces.

Why we create the Namespaces?
As we already know that we can’t create two variables of the same name inside of same block of code. Similarly we can’t make any class, any identifier with same name in its own scope. So we take help of namespaces.
Namespace is just like a block in which we define our classes. And we can access those classes with this specific namespace name. If the same name of class is also define somewhere else in any other namespace. Then it will make no effect on our application because its namespace block name is changed. So when we are writing code in Visual Studio, then sometimes our IDE asks from us this method or this class is from using statement and then we should know about those using statements, actually which we need to include in our program which is relevant to our classes or methods which we are using.
When we make a project then we create a custom namespace with the same name of our project.


What is Assembly?
Microsoft Official Documentation says that
“Assembly is the fundamental unit of Deployment”.
But this definition doesn’t clear us the concept of an assembly.
Let’s discuss what is an assembly? It is actually the precompiled code which can be executed by the CLR. The using statements which we use in our programs are actually precompiled chunks of code, these are the namespaces.
Basically assemblies are used for the Reusability purpose, when we add the class library in our project and add some methods in it and build that project it will create the assembly in (.dll extension). DLL stands for dynamic linked libraries, it has not its own memory space for deployment. We make some common code in our class libraries and generate its assembly and we can use it in our any framework like ASP.NET, WPF, UWP, ADO.NET etc.
When we create the program then we can see some using statements appear in our files these are actually the assemblies which provides us the classes or methods relevant to the nature of our project/application.

Open your Object Browser, 'mscorlib' is an assembly and {} shows the namespaces of the relevant assembly.
Here 'Console' is the class of 'System' namespace and it has following methods.

How Can We Create a Proper Meaningful Assembly?
Make the Project and add some functionalities to it. Then build it and now open the location of this project
Bin/Debug/…
Here you’ll see its assembly.
Now if you wanna consume it in your different project then you need to add reference to this compiled assembly.

Example
      1)    Create the Console Application.
      2)    Add Class Library Project in the Solution.
      3)    Add this Code to Your Class Library Project.
 using System;  
 namespace AssemblyDLL  
 {  
   public class Class1  
   {  
     public void PrintBismillah()  
     {  
       Console.WriteLine("In The Name of ALLAH Almighty");  
     }  
   }  
 }  
     4)  Now Build this Class Library Project and Reference its Dll to our Console Project.
      5)    And now add this code in console project ‘Program.cs’.
 using AssemblyDLL;  
 using System;  
 namespace AssemblyDemo  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       Class1 obj = new Class1();  
       obj.PrintBismillah();  
       Console.WriteLine("Salam");  
       Console.ReadLine();  
     }  
   }  
 }  

     6)    Run the project
Here you’ve observed that the code which we’ve written in our Class library (.dll) is executing in console project memory space. Similarly we can use this precompiled dll in any framework where we need this common functionality.
Now understand this concept at little bit enterprise level, actually the thing is we make a module of specific tasks in which we use our relevant assemblies which support our code. Then we build it. And now this new created assembly which is in the form of (.dll) we can use it in our project.

Remember Assemblies has many types of extensions but here we have just two types of assemblies
  • ·        .exe
  • ·        .dll

0 comments:

Post a Comment