Sunday, December 26, 2010

ASP.NET – Introduction and Concepts

What is ASP.NET?


ASP.NET is a server side scripting technology,  this technology allows the programmer to write scripts in a web page that will be executed in server side.

Terminology:
  • ASP, Active Server Pages
  • ASP.NET runs in the IIS server
  • IIS, Internet Information Services, is an internet server built by Microsoft
  • IIS is free of charge and comes with windows, but some features are not supported on client versions, to install you just need to access the turn windows features on or off section in windows.

ASP.NET is compiled and not interpreted:

ASP.NET, as well as all .NET applications, are compiled. Is impossible to run C# or VB code without compiling it.

Fortunately the ASP.NET applications doesn’t need to be compiled every time they are executed, instead the Interpreted Language code is created once and updated just when the source code is change. The files stay in the server cache, in the directory  C:\WINDOWS\MICROSOFT.NET\FRAMEWORK\[VERSION]\TEMPORARY ASP.NET FILES

This statement depends on the type of project you choose. If you will choose a Web Project, the source code is compiled when the project is, otherwise if you choose a Web Site, each page is compiled when the Request is made.

Web Forms


The ASP.NET pages, known as Web Forms, are a vital part of an ASP.NET application, because they provide the browser output after the client Request.


ASP.NET Page:
  • Same as a HTML page
  • Can contain HTML, XML and scripts
  • Scripts are executed in the server
  • ASPX extension
  • All the controls must be contained inside a
    tag
  • The tag must contain the attribute runat=server
  • The attribute runat=server means the form will be processed in server side


How does it work?


  • Applications are executed in server side, so imagine that you have a page that allows users to write comments. The user does this operations in the browser, but to save the comments is needed to make a update in the database, for that the code must run in server side. ASP.NET handles those events using a technique called PostBack, which sends the form when certain operations are executed. When the server receives the page it handles the corresponding events in server side.
  • Web Applications are Stateless, it means they don’t keep the state. When the page is rendered to html, the objects are destroyed and all the client information is discarded. This model is good to increase the scalability and application traffic, but is hard to offer an experience similar to a windows application.
ASP.NET is based in a event model, where the programmer adds a control to the page and choose which event wants to handle.  Each Event Handler is a discrete method that keeps the code clean and tidy.


Events, how do they work:

  1. Page and associated controls are created, initialization code is executed, the html page is rendered and returned to the client and the objects are released from server memory.
  2. At some point the user performs an action that causes a PostBack, such as clicking a button, and every page information is sent to the server.
  3. The server recreates the objects, putting them in the same state as the last time they were sent to the client.
  4. The event that caused the PostBack is handled and the associated code executed.
  5. The modified page is rendered to Html and sent to the client with the new data. The objects are released from server memory.


Page Lifecycle



There are two types of events that cause a PostBack:

  • Immediate Response Events: Executed whenever the user clicks in a button or the javascript function, __DoPostBack() , is used.
  • Change Events: When a control state is changed, like the value of a checkbox or textbox. If the property AutoPostBack is active, the event is executed immediately, otherwise is executed in the next PostBack.
Imagine a page with a submit button and a textbox without the AutoPostBack property active. The user change the text and click in the button, the events execution order is:

  1. Page.Init
  2. Page.Load
  3. TextBox.TextChanged
  4. Button.Click
  5. Page.PreRender
  6. Page.Unload

As for now as introduction this is everything you need to know, the next article will be about Application Anatomy. 


Tuesday, December 14, 2010

No geek

Because today i dont feel like writing about software, i will make a no geek post, and will start with the first thing that will come to my mind.

My girlfriend, i would like to thanks to her all the patience she has with me, when i am moody, when i am childish, when i am stressed and a bunch of other states, also everything she does for me, all the help she give me, even when she is pressuring me to study, because if she would not do it i wouldnt study at all, or at least much less, so thank you very much Moje Kochanie :)

Now coming back to me, just for you not to think i have stopped writing about geek stuff, just wanted to let   you know that i am going to start with a season about asp.net, i will translate some articles from http://www.pplware.com , and i will start also a review about the book Code Complete from Steve McConnel, and of course ill continue with the code snippets that are allways usefull. Probably you will be wondering why im writing direct to you, my readers, but it has a reason, in the last days even if i was talking with some people about it i received more visits than the ones i was expecting, so i decided to do something for you and start writing some set of articles about different subjects, and of course it will allways be a pleasure to have some feedback from you.

Saturday, December 11, 2010

Ubuntu - Magic Mouse

Today i am not writing about Microsoft. For some people it might look like a sin but for me it means i am open to other tecnologies, ideas.
I have been installing ubuntu in my machine today because i heard that the last version supports the Apple Magic Mouse out of the box, so i was curious about it, because i have a Magic Mouse and in Windows 7 the only thing i manage to put to work was the button left and right.

So if you are planning to install ubuntu in dual boot with windows, the only point where you dont have just to press the button forward is when you will need to create the partitions wich are:

Primary 500Mb /boot
Primary Free Space /
Primary 5000Mb swap area

after everything installed you just need to go to the bluetooth symbol and connect to your Magic Mouse, the pin is 0000.

Friday, December 10, 2010

Thinkings - Static Class vs Instance Class

When developing very often a developer wonder if to use a static class or a instance class. It is a decision that really depends on what you are going to do, and i think also from person to person, and because of that i would like you to tell me in wich situations you use each of them.

In my case, for example, if i have to create a business layer, typically i will use static classes. Imagine i have to develop a Content Management System, and i need the methods to write post, delete post, edit post and get posts, so my approach would be to create a static class like the following:

public static class PostsManager
{
    public static void WritePost(String user,String text) {...}
    public static void DeletePost(String user,String text) {...}
    public static void EditPost(String user,String text) {...}
    public static ICollection GetPosts(DateTime begin, DateTime end) {...}
}

and now you are wondering, what is that collection of Post?! That is my instance class, because the method GetPosts will return all the posts between two dates, and because ill need severall instances of Post than i am using a instance class.

public class Post
{
    public Post(String Author, String text) {...}

    public String Author { get; private set; }
    public String Text { get; private set; }

    public String TextTruncated(int size) {...}
}

Code Snippets C# - Strings

Sometimes you want to write a quote inside a string, but if you will write " " " it will not work, so the way to workaround it is writing \" .

Snippet:

String.Format("C:\\TestFiles\\\"{0}\"", fileName)

About MSI - Microsoft Installer

Developing a software application is funny, but more rewarding is to see that it helped someone in its daily work, that its usefull and not superfluous. At this point the developer achieves to a dilemma, how will i distribute my software?

Microsoft provide software developers a component called Microsoft Installer, or MSI to friends, wich contains all the functionalities needed to install, mantain or remove the application from a Windows System.

To start open the context menu of your solution node in the solution explorer dock: Add\New Project. Then you will see the figure shown below:


Now using the setup project context menu you can add several projects outputs and distribute your application.

So this is the basics about MSI, but what i want to write about is Custom Actions and what i learned about it today. So i needed to transfer information between two different custom actions and i thought that i could use the parameter stateSaver from the event install, but it did not workout because it is used to save installation parameters to be shared across all the installation events of the custom action in context. So i questioned my colleague Abel about a idea to solve the problem, and the answer could'nt be more simple, "try to use a static dictionary". With that in mind i created a static class with a property that is a Dictionary and other information, refactored the code to use my new class and went for testing. Result, the test worked out in the first try.

References:
http://en.wikipedia.org/wiki/Windows_Installer
http://devcity.net/Articles/339/3/article.aspx
http://msdn.microsoft.com/en-us/library/aa368066(VS.85).aspx

Thursday, December 9, 2010

How do you Phil?

Yesterday were 30 years since John Lenonn died.
Today is the first day of this blog.

Probably you are wondering what is the relation between those two events, and my answer is:

None.

The thing is that i decided to start writing a blog since some time ago with the objective to archive experiences, lessons learned, ideas, basically some of my life snippets.

Now i should say, i hope you will like what i will write here, but honestly, i am not doing this for you, but for me. The reason why i am sharing this with you is because maybe i will write something that one day can be usefull for you, and i believe that the knowledge should be shared and not keep inside people mind.