20 ASP.NET Questions: Answered. Part Three

Category: ASP.NET

20 ASP.Net Questions [Questions 11-15]

View Part Two here

Introduction

This article was created to be used as reference material for beginners through Intermediate developers. I have assembled 20 questions that I feel every developer should know when developing web applications and even Window’s application within the Visual Studio C# environment.
In this third installment of these articles we will answer questions eleven through fifteen in our list of 20 questions.

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

What we will learn in this article:

  • What is FTP?
  • What is Serialization?
  • What is Refactoring?
  • What is Unit Testing?
  • What is Generics?

Getting started with our list - What is FTP?

FTP [File Transfer Protocol] is a protocol that allows you to exchange files between your machine, and a remote one, usually one server to another. You can upload files (send the file from your server to the remote server), or you can download files (your server will receive the file from the remote server).
To further define FTP it can be used to upload/download any files to any machine running an FTP server as explained above. Another aspect to being able to use FTP is that in most cases [should be all cases] there will be a logon required. You will need to supply a valid username and password. Also in most cases when you logon you will have permissions to certain directories and will be re-directed to those directories when you logon.
In some cases the server will have an anonymous account anyone can use. This account usually does not have the same permissions as you find in a valid logon. Normally an anonymous account has these limited permissions by default, meaning an anonymous user can only access certain parts of the site and more often than not, can only download files; they wouldn't be able to upload them.

Here is an example of a third party wizard that helps you connect to FTP sites:


 

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Even though you can use third party tools like in our screen shot above you can easily write your own code that will accomplish the same thing as in our example below found at:
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

Code Snippet

 

public static bool DisplayFileFromServer(Uri serverUri)
{
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
WebClient request = new WebClient();

// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","[email protected]");
try
{
byte [] newFileData = request.DownloadData (serverUri.ToString());
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
Console.WriteLine(fileString);
}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
return true;
}

What is Serialization?

Serialization can be defined as “the process of storing the state of an object instance to some type of a storage medium"
In other words it is taking an object and then converting that object from its current form into some form for storage purposes, this can be a disk file or it might simply be in memory using MemoryStream for example, because you are simply copying the object. Or you may be sending an object between machines via .NET Remoting or a Web Service.
You would use serialization any place you need to take an "out of memory" snap shot of the object & persist it someplace else.
You have a few choices; you can either create you own "serialization” process that is using something like StreamReader, BinaryReader, or MemoryStream. For that matter you can use any object that can contain and persist the data.
Serialization is best implemented using the ISerializable interface.  XML Serialization using the default methods limits what you can serialize to public declared members of a given object.  You can serialize any object with XMLSerialization but you can customize the Serialization process using the ISerializable interface.
Here is an example of serialization:

FileStream fs = new FileStream("Test.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, somestruct);

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

What is Refactoring?

Refactoring can be defined as "improving the design of existing code". It's a kind of global "code cleaning" exercise. There are many tasks you can perform on some code to improve it, and Refactoring is one of those tasks. Other tasks might be combining methods and applying new methodologies to existing code. Performing refactoring can range from the extremely simple to the extremely complex.
There are basically two sides to refactoring: the first one is working out what you want to do (e.g. renaming a method) and doing it (renaming the method and fixing up all the calls to it). The latter part is the one which is relatively recent in terms of Visual Studio.
Here is an example of refactoring:

void TestMethods(){ ... }

becomes

void TestMethod(){ ... }

In Visual Studio if we were to try and rename a method as in the above code example we would be prompted to Refactor all code that references that method we are changing, or we can Preview all of the code changes first within the “Refactor Preview Changes” window as in the screen shot below:


[Click to see full-size]

What is Unit Testing?

 

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

Unit Testing is the testing of individual hardware or software “units of work”, or groups of related “units of work." You setup a Unit Test within Visual Studio and for lack of a better term it runs like a complex macro testing these units of work based on your configuration.
A unit test calls the methods of a class, passing suitable parameters, and verifies that the returned value is what you expect. You can code unit tests by hand.
A perfect example of when to use a Unit Test is for testing web services. You can easily setup multiple tests to call and validate that a web service is still up and running. I remember at one location the network guys were always taking down our servers for one reason or another and when we would get a ticket that the system was down we would first run the Unit Tests on the web service to make sure it was up before we looked into it any further.
In Visual Studio unit test tools are only a part of the “Team Suite” version of the development language. However, if you do not have that version of the software you can still create and unit test you code base by using a third party tool like NUnit.
NUnit – Is a .Net unit testing framework development software that is open source and is written in C# (for Visual Studio).
Here is a screen shot of creating a Unit Test within Visual Studio. This screen can be found under “Test” on the main menu of Visual Studio.


[Click to see full-size]

What is a Delegate?

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!


In simplistic terms think of a delegate as a template of a method:

 

public delegate void methodTemplate(string myString, int myInt);

In the above example we create a method which returns nothing but takes a string and an int as its parameters. In this example that is all this method does.
Within your application some class somewhere sends off a message containing a string and an int – this message can be described as it wants to tell whoever is listening that you have completed this portion of a process and you are ready to continue.
In that class you will have the following declaration:

public event methodTemplateDelegate waitingForCompletion;

That class has a process which is listening and decides that it's time to trigger the Delegate and begin executing the code.

if (waitingForCompletion != null)
{
 waitingForCompletion ("Completed", 0);
}


All the listening classes have to do is register by passing a method with the same signature.

 

What we have Learned

We have completed the first fifteen questions of our list of twenty questions. We have delved into the various topics within our list and hopefully have given you a little insight to some of the answers you were looking for? In our next article which is the last article in this series we will cover questions sixteen through twenty.

 

 

 

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Attachments

There is no attachment associated with this article.

View Part Four here