Great Tutorials for Learning ASP.NET MVC and my Thoughts

I’ve been digging into ASP.NET MVC more and more and the more I use it the more I love it. Never thought I’d say that.  At first glance I didn’t like the idea of MVC because it looks like ASP Classic when you first look at it.  Server side code nuggets <% Inside the Html %> like asp classic.  Though I was hesitant giving up classic when .net came out. 

MVC I realize after using it, is much more powerful.  You’re not really doing server side code on the client, it’s an html template, you only do logic that relates to the UI.  Real code and business logic is for the controllers and classes.  There’s no code-behind anymore, no viewstate, no more .net controls with ID and runat server tags.  It’s so light weight, I’m baffled. 

Good Starting Tutorials.

Intro To ASP.NET MVC Tutorial 1 – Using VS2010 – Some Basic MVC stuff

Once you complete that.. move on to the next one which is a more realistic world app.

MVC Music Store MVC Tutorial 2 – Using VS2010

After that you’ll understand it very well and have a good grasp.

More MVC stuff over at asp.net/mvc

Extending the ASP.NET MVC HtmlHelper in VB.NET Specifically

 

I’ve been reading up on MVC and understanding it’s framework. Of course everything out there is in C#, which is fine, however I prefer to write in VB.Net.  This causes some of the examples of lambda expressions and other things to alter, and can be a pain if you are unsure has how.  

This post is specific to the HtmlHelper, and extending it on MVC.

In mvc, you have access to an html. namespace, for links and other html controls.

  • Html.ActionLink()
  • Html.BeginForm()
  • Html.CheckBox()
  • Html.DropDownList()
  • Html.EndForm()
  • Html.Hidden()
  • Html.ListBox()
  • Html.Password()
  • Html.RadioButton()
  • Html.TextArea()
  • Html.TextBox()

 

If you want to know more about it you can read up on it on various posts, such as this here or here, or use it in the mvc music store tutorial. That will give you an good understanding of it.

This post will just show you the difference from the c# examples.   

In C#

Code Snippet
  1. using System.Web.Mvc;
  2. namespace MvcMusicStore.Helpers
  3. {
  4.     public static class HtmlHelpers
  5.     {
  6.         public static string Truncate(this HtmlHelper helper, string input, int length)
  7.         {
  8.             if (input.Length <= length)
  9.             {
  10.                 return input;
  11.             }
  12.             else
  13.             {
  14.                 return input.Substring(0, length) + "...";
  15.             }
  16.         }
  17.     }
  18. }

 

And then in VB.Net

Code Snippet
  1. Imports System.Web.Mvc
  2. Public Module HtmlHelpers
  3.     <System.Runtime.CompilerServices.Extension>    
  4.     Public Function Truncate(Helper As HtmlHelper, Input As String, Length As Integer) As String
  5.         If Input.Length <= Length Then
  6.             Return Input
  7.         Else
  8.             Return Input.Substring(0, Length) & "..."
  9.         End If
  10.     End Function
  11. End Module

You’ll notice that in the C# version it uses a “Static” keyword on the Class. In VB that equivalent keyword is “Shared”.  However, in VB you can’t use the “Shared” keyword on a Class. VB only supports “Shared” on Methods. So you should use a Module instead of a Class which is the same thing actually. It’s from the old VB days.

For your function you have to add the attribute

<System.Runtime.CompilerServices.Extension> 

Notice I don’t have the underscore _ line extending, because I’m in VB.Net 10. Previous versions or .Net 3.5 and earlier you’ll need an underscore to connect those two lines.

Other then that it’s about the same. Still have to add it to the web.config the same. 

Code Snippet
  1. <pages>
  2.   <namespaces>
  3.     <add namespace="System.Web.Mvc" />
  4.     <add namespace="System.Web.Mvc.Ajax" />
  5.     <add namespace="System.Web.Mvc.Html" />
  6.     <add namespace="System.Web.Routing" />
  7.     <add namespace="MvcMusicStore.HtmlHelpers"/>  
  8.   </namespaces>
  9. </pages>

 

So just remember, use a module instead of class, and add your attribute.

Works on My Machine

 

A common phrase we developers say when an application we develop breaks for someone else.

Found this image on DevExpress’s Code.Google.Com for plugin’s and found it amusing. Think I’ll add it to my QA releases. 

Stamped and Approved:

works-on-my-machine-starburst_3

UML Design and Architecture Diagrams Tools for Visual Studio 2010

 

Here’s a good post on an Introduction to the different Architecture Diagrams in Visual Studio 2010.

To sum up..

Architecture Diagrams

  • Use Case
  • Activity
  • Sequence
  • Component
  • Class
  • Layer Diagrams

 

They should be use in this order, except for the Layer one. Which can be done whenever I believe. There’s another one, Dependency Graphs, see original post, page 2 for more info.

Use Case:

…a summary of who is using the application, and how they plan on using it…

 

Activity:

…is used to show a software process or business activity as a workflow through a series of actions…

Sequence:

…used to show interactions between different objects in the software system…

Component:

…laying out the different components in your system, and how they interact…

Class:

…used to describe the different objects in the application being developed…

 

Layer Diagrams:

…allow you to describe the "logical" architecture of your application…

Control Your Windows Service from a Asp.NET Web App, Start, Stop, Run A Method

 

Maybe you find the need to; or you think of the possibilities of controlling a Windows Service from a Web App might be useful.  For me I like the idea of writing a Windows Service that does some server function but can be controlled or called by a user. This is a more detailed post on code examples from the previous post with some other considerations.

Examples Reasons?:

  1. Maybe a Administrator needs to start and stop services but doesn’t have access to the server by normal methods.
  2. A user that calls a Windows Service that performs some type of Emailing functionality such as a notifying users when they have a new Task.  Maybe this table is populated by many means. Win service would check the database every 30min and send off emails.  In your web app, your users are special, so every time they assign a task, you fire off the windows service right there and then. Or maybe sometimes you have the need to fire off a manual send and have written redundant code to do it.
  3. Windows service control center for developers? Or Support users?

 

Anyway, the process is quite easy.

 

Windows Service code, somewhere have a method like this…

Code Snippet
  1. Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
  2.     MyBase.OnCustomCommand(command)
  3.     Select Case command
  4.         Case 130
  5.             'Call one of your other methods from here
  6.         Case 150
  7.             'Call some other method or command
  8.         Case Else
  9.             'Do Nothing
  10.     End Select
  11. End Sub

You can only call one CustomCommand, so you send in Command Integers and from there decided what you want to do.

 

In your ASP.Net app on a button click event..

Code Snippet
  1. Using sc As System.ServiceProcess.ServiceController = New System.ServiceProcess.ServiceController()
  2.     sc.MachineName = "Mastro-PC"
  3.     sc.ServiceName = "ComplyTrackMainTimer"
  4.     sc.ExecuteCommand(130)
  5.     'sc.Stop()
  6.     'sc.Start()
  7. End Using

This will work on a web.app on a button click event. You have to set the MachineName and the ServiceName. You can also Stop() and Start() the Service or look up the Status.

You can get the MachineName of the server by opening a command prompt from the server and typing in “HostName”

image

You can get the ServiceName by opening up the server and looking at the name in Services. Don’t use the Display Name use the Service Name.

image

I have a previous post on this with more detailed information on valid Command Integers and Security.

Cyclomatic and Maintenance Complexity using CodeRush per Mark Miller

 

I’ve been using DevExpress’s Visual Studio Tool, CodeRush, for awhile now and I love it.

There’s been a feature though I was never familiar with, and that’s the little digits next to my methods. Choices of Cyclomatic Complexity, Maintenance Complexity and Line Count. Granted I figured out what Line Count was. 

image

So I did some research and found that Mike Miller from DevExpress has a Blog over there and goes over some CodeRush features that I found helpful.  I don’t know where his post on the Complexity count went but he did mention to another user on a forum post that there was this PDF which he describes the Complexity numbers.

Attachment Reference:

 

This has been helpful in alerting to me when I need to refractor my code and make changes, and break it out.

Here’s the charts from the PDF, which is the meat of it.

Cyclomatic Complexity

Probably the simplest measure of complexity. CC is the number of decision points for a method plus one. It also happens to represent the minimum number of test cases needed to travel through all branches of a method.

image

 

Maintenance Complexity

The purpose of Maintenance Complexity is to give you a picture of how much code you have in a given member.

 

image

 

Maintenance Complexity Points

Breakdown of how the Maintenance is counted

image

image

image

Configure TFS 2010 Email Server, Enable Email Alerts

 

If you get the error, “Sending email is not enabled”, its because you haven't configured this yet.

You can configure email settings using following command. 

TFSConfig ConfigureMail 
/FromEmailAddress:NoReplyTeamSystem@domain.com
/SmtpHost:DomainMailServer

 

Or just do it via the TFS Administrator Tool which you can also Enable Email Alerts.

 

image

$35 laptop prototype or something from India coming soon! (Update)

 

India is about to put out a computer that’s only $35!! Crazy!  Then drop the price to $10 a unit as it sells. Don’t know what the stats are other then it’s a Linux based machine but I’d buy 10 of them.

From the picture it kind of looks like a wallet and flexible.  Who’s been smudging up my screen!

“India has unveiled a $35 computer prototype as part of its program to provide connectivity to its students and teachers at affordable prices.”

“The ministry said the price would gradually fall to $10 a piece.”

“The Linux-based computer is equipped with an Internet browser, a PDF reader and several other facilities…”

Source: http://www.cnn.com/2010/WORLD/asiapcf/07/23/india.thirty.five.dollar.laptop/

 

Indian government minister Kapil Sibal unveils a prototype of the $35 computer.

 

Update:

Looks like it’s still in progress and more legit then the original post. http://news.cnet.com/8301-17938_105-20013370-1.html

 

  • OS: It was changed from Linux to Android.
  • Virtual Keyboard
  • Camera
  • Full Video Capability
  • Wi-Fi
  • 2GB Ram
  • Scheduled for Mid-2011

When Faced with the Facts, You’ll hold on to your misbelieves even more.

 

This interesting study, I wanted to post. As when I talk to someone about politics or religion, it’s interesting how when faced with factual information, someone’s views will not change.

"In a series of studies in 2005 and 2006, researchers at the University of Michigan found that misinformed people, particularly political partisans, rarely changed their minds when exposed to corrected facts in news stories. In fact, they often became even more strongly set in their beliefs. Facts, they found, were not curing misinformation. Like an underpowered antibiotic, facts could actually make misinformation even stronger."

 

“…we base our opinions on beliefs and when presented with contradictory facts, we adhere to our original belief even more strongly. “