My Adsense

Thursday, December 27, 2012

Dropbox as SVN Repository


Version controlling is one of the most important concepts in software development. It helps us to maintain different version of files and folders in a very easy way among different peoples. One of the most popular versions controlling system is SVN. 

This post is not about describing the features of SVN, rather it will focus some advanced issues here.

The most critical (and important) problem I faced while working in online distributed team is to maintain the codes among my different team mates. Well, there are many free online SVN servers out there which will provide you space to store your SVN repository, but there is another way you can bypass those SVN server if you don’t want to put your code on their servers.

Below are the steps.

1. Setup environment

Download TortoiseSVN and install.

2. Create folder

2.1 Go to your Dropbox folder and create a new folder named 'DropboxSvnRepository'


3. Create repository

3.1 Right click on the 'DropboxSvnRepository' folder--TortoiseSVN-- Create Repository Here



3.2 Create the folder structures
Please copy the url of the respository as highlighted on the image. The url is “file:///E:/Online Backups/Dropbox/DropboxSvnRepository” for mine. Please keep in mind that I will refer this url later.  



4. Checkout

Create a folder at where you want to put the project/projects. In my case I have named it as 'DropboxSvnClient' under my E drive.
4.1 Right click--SVN Check out



4.2 Put/paste the url--press ok



4.3 Default folders of the repository will be downloaded to the Client folder.



5. Create folders

Now we need to create the folders according to our need. I will make this simple.
5.1 Go to trunk and create a folder named 'Projects'



5.2 Right click on the folder--TortoiseSVN—Add



Then press OK and OK on the following two windows.
5.3 You should see the following overlay icon with the Projects folder.



5.4 Right click on the same Projects folder--Commit
Press OK and OK to the next windows. The Projects folder should be green in color.



5.5 Now the Projects folder is copied to the repository folder. Since the repository folder is in Dropbox, it is syncing with online.




6. Create project

6.1 Open your visual studio. Create a console application. In my case it is 'CalculatorApplication' and put it inside of the Projects folder.



6.2 Make your Program class as given below.



7. Commit project

After successful compiling, you need to add this project to the svn.
7.1 Right click on the CalculatorApplication folder--TortoiseSVN—Add



7.2 Select the necessary folder. You can see below screenshot for help.
7.2.1 Uncheck the Resharper generated folders.



7.2.2 Uncheck Bin and Obj folders



7.2.3 Click OK and you should see the window confirming that the files is being added.



7.2.4 Press OK and you should see the following overlay icon with the CalculatorApplication folder.



7.3 Now we will commit the newly added files and folders.
7.3.1 Right click on the CalculatorApplication folder--SVN Commit
7.3.2 Write some notes for the commit you are doing and then press OK.



7.3.3 After successful commit, you should see your repository will syncing.

What is happening after you press commit?
This is a tricky part. Your files are copying from CalculatorApplication folder to your 'DropboxSvnRepository' folder. Since the later folder is inside of the Dropbox, the files will be uploaded to the Internet. 
If anyone gets this DropboxSvnRepository folder and also try to use that folder as a repository, he/she will get the files you have commit on your local repository. Thus any number of SVN user can work simultaneously using this repository under the SVN concept if he just gets the DropboxSvnRepository folder. They don't need a Online SVN Server to manage their code over internet.

[Now i am in my brother's computer.]

8. Remote machine

8.1 Now we should test it. I have shared the DropboxSvnRepository folder to my brother's account which Dropbox is installed in another computer. I downloaded the folder there.



9. Copy Repository Url

9.1 Right click on the repository—TortoiseSVN—Repo-browser



9.2 Copy the selected text and keep in mind it is the repository url.



10. Checkout

10.1 Create a folder at where you want to put the project/projects. In my case I have named it as 'FahimsDropboxClient'.



10.2 Right click--SVN Check out--put the appropriate url--press ok



11. Open project

11.1 You should see the similar folder structure created in the previous machine. Open the project you find on the Projects folder.



12. Change project

12.1 You should see the similar code written in the previous machine.



12.2 Change some code. Create a method like below.



13. Commit project

13.1 Build and Run the project. Since the only file we changed now is Program.cs file, so this file is now turned into red. 



13.2 Right click on the Program.cs file--SVN Commit
13.2.1 Since the latest file is now copying to the repository, the repository is syncing and the overlay icon turned into blue.



13.2.2 After syncing is complete, the file will be having green overlay icon.



14. Update my machine

Now we go to 1st location (my machine). The changed files are going to my machine’s local repository. Since this folder is in Dropbox, my brother’s folder will be synced with other shared Dropbox accounts like me or any others.

15. Update folders

15.1 Right click on the DropboxSvnClient--SVN Update.



15.2 The files which are changed on remote machines, will be updated by SVN in your local computer.



16. Test

Open the project and you should see the method which is added in the another machine.


Thus we can use a single Dropbox folder as a SVN repository without having any online SVN server.

Sunday, September 16, 2012

Using Dependency Injection (Unity) to Launch Windows Form Application Part 2

We have successfully removed the dependency between the Form to be launched with the Application.Run method of the Program.cs file of another project in my previous post.
Now we will do some testing on how can we add the other forms with this existing compiled executable file (exe) without rebuilding it. It is important because there will be many cases where you will be sitting on the client side for deployment and you don't have access to your development environment. You will just get the updated DLL of the new form. So how can we do that smartly?

Lets create another project (not inside of the solution we made in the first part) added the word External in the name.


As we did in the previous post, Delete the Program.cs file, Rename the Form's name, and make the project as Class Library type instead of Windows Forms Application on Start Up. 
Then add a Label on the Form and write some text in it. After doing these steps, the project will be look like the following image.


Build this project and copy the DLL of this project.


Paste that DLL in our Main Project's Debug Folder shown in the below image.


Since in this stage, we want to open the Windows Form under the External DLL file, we should change the MainProject's config file using a text editor e.g. notepad. 


Change the config file as below.


Now double click the exe we made before. The newly added Form will be opened.


I hope you all understand what we did. If you find any kinds of problem in understanding any step I mentioned, feel free to add comment under the blog. 

Using Dependency Injection (Unity) to Launch Windows Form Application Part 1


Suppose you have three different clients and they want to use different Form/UI for a single specific task. Let’s say it’s about calculating their bills. But those Forms are developed in different Windows Form Projects and there is no way to add those forms in your project at the time of development (Quite hard to believe, but we are guessing..right?) So, how can you incorporate/open one of those Forms according to the user’s choice as a startup of Your project?
Or, let say it differently, you need to add/decide which Form to open using your configuration file without using the if/else block in your code. Then what’s the idea?

If we look deeper, let’s say, the project is highly extensible and any form (with specific type) can be added as a startup form without rebuilding your whole application solution. How can you do that?

We can do it using Dependency Injection. From Wikipedia:

"Dependency injection is a software design pattern that allows a choice of component to be made at run-time rather than compile time. This can be used, for example, as a simple way to load plugins dynamically or to choose mock objects in test environments vs. real objects in production environments. This software design pattern injects the dependent element (object or value etc) to the destination automatically by knowing the requirement of the destination. There is another pattern called Dependency Lookup, which is a regular process and reverse process to Dependency injection."

We will apply some very basics of Dependency Injection in this part. 
  • First we need to make the starting point of our application independent of any specific Form name or class referenced in our project.
  • Then we will make our starting point to read the Form name from the configuration file, instantiate that Form and open that Form.
Lets do it using the below steps. I will describe necessary information in the relevant steps. My target audience is almost anyone who can understand the C# language, so it will be very elaborate in some cases. 

I have given the download link of the project at the bottom of the post.

We will create a Blank Solution using Visual Studio 2012. Naming it as Calculator.


Next, we will add our start up Windows Form project to this solution.



Delete the Form1.cs File from the Solution Explorer and remove the code from the Main method of the Program.cs class. After these tasks, the solution will look like below.


Next build the solution and make up to this state error free.

Now, we can see Calculator.MainProject solution doesn't have any Form, and we need to create another Windows Form project to use the Form from another DLL.
Lets add another project Calculator.Standard into this solution. 



Now delete the Program.cs file from the Calculator.Standard project. Why are we doing this? It is because we will create a DLL which will have the Forms in it. We don't want to execute the Forms from the Calculator.Standard project. We will use the DLL to another project. In this case, the 'another project' is Calculator.MainProject project. We need also specify the Calculator.Standard project to act as normal class library project instead of Windows Forms project. Below are the steps.


Some of you might question why are we adding another project into the solution instead of creating a separate project? Well, as long as we are not adding the reference of the Calculator.Standard's DLL into the Calculator.MainProject, it makes no difference whether we make separate project or using the same solution of the two projects. I hope it is clear with you. 

Next, we should rename the Form1.cs and name it as StandardForm.cs

Now we again build the solution and make sure there is no error left in the solution.

Next, the big task, add the Unity Container's DLL into the project. To do so, we should use Nuget Package. Below are the steps how can we do that.


Click Install.


Select Accept.


There will be a green icon after successful installation/addition of the Unity into our project.


You will see the DLLs under the References Tree of the Calculator.MainProject.


So far, we have created a MainProject without any kinds of Windows Forms in it, a Standard project as a class library project. And we have also installed the Unity into our MainProject.

Now we will create a configuration file where we will define the necessary configuration information into the MainProject so that our Program.cs file can read those information and use those at the time of running. 
Below are the steps to create and add the information into the configuration file.

We will add a '.config' file into the Calculator.MainProject.


To add the unity part into the config file, we need to know the version of the DLL of the unity we are using.


The version i got is 2.1.505.0


Now lets create a section in the App.config file like the following image. Change the boxed value according to your version value.


Then we will create the main part of the configuration file which will contain the information of the DLLs. 
The tag name must be the same as the section name of the unity declaration section. This main section will contain two sub part. One is TypeAlias and the other is Container.
In the TypeAlias section we will create individual alias of the DLLs we want to use in our project. 
In the Container section we will define which DLL should be called if call a class from our MainProject. We will elaborate this more shortly. So, the brief of this part is below.


And with attributes the section will looks like below.


Now we need to fill the values in the attributes.
We can see, in the typeAlias tag, there are two attributes. One is alias and the other is type. Alias is the value which we will call as a key and the type is the value of that key. In the 'key', we will add the fully qualified class name and the Assembly Information of that DLL so that we can call and instantiate that class using that DLL.
So, since we have Calculator.Standard as a class library, so we will use its DLL in our project and add the necessary information in the config file.
Fully qualified class name is "Calculator.Standard.StandardForm"

Assembly information will be seen from the following image.


And the values will be added in the config file as below.


Please check that we also added a value in the mapTo attribute inside of the type tag under types section of the container section. It is because we want to call the alias 'Standard' if the type attribute is called from the code.
For example, lets say the tag type is 
This means, there will be a alias of Gateway in the typeAlias section of the config file. Suppose the Gateway alias's DLL is added into the project as reference. So, what the Unity will do is, it will check for the mapTo attribute where the type is written as Gateway. It will return the instance of the StudentGatway alias' class.

So, we should select a class which will be used in our Calculator.MainProject and we will map that class with our Calculator.Standard project's class using Unity.

Since, System.Windows.Forms.Form class is the base of every Windows Form class, we can use this in our MainProject. So the config file will be like the following.


Next we will define the names of the container and the type so that we can call those names from our code. In this case, it is CalculatorContainer and CalculatorForm as displayed in the image.


So, the total code of the config file will looks like the following.


Now our task is to create a method which will read the config file, instantiate an object which will be written in the mapTo attribute and return it to the caller. Please keep in mind that, at the development time, we don't know which Form will come at the run time. So, we should apply the Inheritance idea here. If we cast the object we got at runtime (of course it will be a Windows Form, and all Windows Form is a Form according to the Inheritance) in Form and return that object to the Main method it should open that Form successfully.
The code will be like below.


The required namespaces we will use using are below.



The above code is self explanatory. But the string values will come from according to the following picture.


So, the total code we will be using are given below.


If we press Debug at this stage, the following exception should be occurred.


Its because we don't have the Standard project's DLL inside of the Debug folder of the MainProject nor having the Windows Forms DLL as well. 



So, what we do is, we should copy those DLLs and paste it in the Debug folder of the MainProject. Please keep in mind that we will not Build any project from now so that we can understand we can open a external Windows Form without rebuilding the executable file.

To copy the Windows Forms DLL, we should find it first. We can copy that using the following steps.




To copy the Calculator.StandardProject's DLL we will do the followings.


After pasting those DLLs, the Debug folder of the MainProject will looks like below.

Now we will double click on the Exe to open the external Windows Form which is pasted in the same folder. The following output should appear if we all follow exactly the same instruction.