.NET, web, community, architecture, practices

Yesterday I spoke at Utah Code Camp. The presentation is named “Healthy Code, Happy Code”. In this session, I discuss things you can do to ensure your code is healthy, other than running it.

Today I came across the results of a survey done by Smart Bear Software, a company that sells software for conducting code reviews. The survey, conducted earlier this year, had input from over 600 software professionals across 30 industries.

One question was “What do you believe is the number one thing a company can do to improve code quality?” 27.4 % of respondents said Code Reviews. In second place was Unit Testing with 18.7%.

Further, 90% said that the biggest benefit of code reviews was improved software quality.

This data reinforces the important point I make in the presentation. I’ll be incorporating the survey results for the next time I do this presentation next month at Nebraska.Code.

There are more interesting results in the survey. Check them out. And happy code reviews.

This week I’m at my first CodeMash. Yes, it’s 10 years in and I’ve heard great things over the years about this conference. I finally made it. And it was great! As any con, I reconnected with old friends, reconnected with longtime friends, and made new friends. The tech content was really, really top notch.

I was lucky enough to speak on Software Gardening. If you were at my session, thank you for being there. Here are the slides (PDF).

ASP.Net 5 is a major change to the Microsoft web development ecosystem. As I write this, Release Candidate 1 is the current version available. You can expect some minor changes to things, but it’s quite baked at this point. Much of what you know about ASP.Net will need to be relearned. The changes bring ASP.Net more on par with the rest of the web development world.

There are several changes you need to know about. First, WebForms are gone. Everything is now MVC-based. Also, web controllers and Web API are now combined. VB is not supported. Another big change is that you can develop ASP.NET web applications on Windows via Visual Studio or on Windows, Mac, or Linux using Visual Studio Code. You can also run your site on Windows, Mac, or Linux thanks to the .NET Execution Environment (DNX), .NET Core, and the new Kestrel server. You can still host on Windows Server in IIS or your Windows developer machine with IIS Express.

One big change is the removal of System.Web. Over the years, as new ASP.NET features have been added, they have been put in this assembly. It had become bloated. Many features were no longer used in modern web apps. And because of the bloat, it used a lot of memory. Everything is now modular and excluded by default.

Creating the project

But, let’s get on with what this post is about…creating your first web application with the new tooling.

Install ASP.NET 5. Instructions for Windows, Mac, and Linux are here. Even if you are running Visual Studio 2015, you will need to get the latest bits. This walk through uses VS 2015 on Windows.

Under ASP.NET 5 Templates, select Web Application. Click OK to create the project. Visual Studio will work for a bit, then you’ll see the new project. But it looks different than you’re used to.

You should now have a new ASP.NET 5 project, ready to use.

Understanding the project changes

Let’s pause for a moment to talk about these changes.

The src folder – This is the source folder. All the files needed to compile and build your web application.

What happened to .csproj? The project file is a Visual Studio file. Because Visual Studio doesn’t run on Mac or Linux, it has been removed and replaced with project.json. If you open this file you’ll see a list of dependencies, runtimes, scripts, and other files and settings.

Frameworks listed in project.json – Support of non-Windows platforms requires changes to the .NET runtime (CLR). The dnx451 is the full “Dot NET eXecution Environment”. Think of this as the full Windows runtime. To run on Mac, Linux, or even a reduced instruction set on Windows, use dnxcore50.

project.lock.json – This file contains a complete list of all the NuGet packages used in the project. In ASP.Net 5, only .Net assemblies are installed via NuGet. Javascript, css, html, and other types of files are installed and managed through a different mechanism explained in a moment.

Dependencies folder – Non .NET dependencies are stored here and installed and managed via Bower, an open source dependency management tool that is used across platforms and web tooling. In other words, it’s NuGet for web front end packages.

There is also an npm folder. This is the Node Package Manager. It’s the package manager for web backend packages.

gulp.js – Gulp is a widely used, open source build system. Think of this as being kind of like the old MSBuild file.

startup.cs – The entry point for your application.

Now that you know what the new files and folders are, it’s time to setup the database.

Creating the database

ASP.NET 5 uses Entity Framework 7 by default. The big change here is that the designer has been deprecated and is no longer available. It’s all EF Code First now.

You probably also noticed there is now a Models and View Models folder. For production systems, I move all the models to a Data project, but to make this introduction easier, I’ll leave the models in their current folder.

Open the appsettings.json file and you’ll see the connection string. As in earlier versions of ASP.NET MVC, localdb is used by default. I will leave these settings. I will also keep this introduction very simple by using a single table.

Right-click on the Models folder and create a new class named Customer.

Create the Customer class

1:publicclass Customer

2: {

3:publicint Id { get; set; }

4:publicstring FirstName { get; set; }

5:publicstring LastName { get; set; }

6:publicstring Street1 { get; set; }

7:publicstring Street2 { get; set; }

8:publicstring City { get; set; }

9:publicstring State { get; set; }

10:publicstring ZipCode { get; set; }

11: }

Save the file

Open the ApplicationDbContext.cs file and add a DbSet line for the Customer class.

Notice the constructor beginning at line 5. The context is passed in rather than being created in the controller as it was in MVC 5. The big change MVC 6 is that Dependency Injection (DI) is used throughout. By default a simple DI system is used. But you can replace it with your favorite DI tool.

What happened to Razor?

If you created ASP.NET 5 forms, you were used to working with Razor syntax that looked something like this:

This code is used to create the Register form. Razor syntax is the @Html code. In ASP.NET 5 and MVC 6, Razor syntax has been replaced with “Tag Helpers”. Here’s the ASP.NET 5 version of Register.cshtml.

Tag Helpers are the asp- construct inside the HTML tags. This makes ASP.NET 5 look more like other HTML and Javascript libraries. It makes the code look more like HTML. Open the Customer Create.cshtml to see with it looks like in this new version.

Now you’re ready to run the application. Press F5 to compile and run. Then navigate to the Customers Index page. If you get an error screen about Database errors, follow the instructions on the error page. Once things are up and running, try adding a few customer records.

Startup.cs

Before wrapping up, I need to tell you about startup.cs. This is the entry point to the application. It’s important you know about this file because in ASP.NET 5, everything is turned off by default, even static pages (see line 73 below where static pages are enabled).

Wrap up

There is A LOT of new to learn about ASP.NET 5 and MVC 6. As I’m writing this, almost everything is documented at http://docs.asp.net. If you see a topic with a wrench icon next to it, there is still work to be done on that section.

If you’re thinking of migrating and MVC 5 application, make sure you read this post by my friend Paul Litwin where he goes through a migration step by step.

I’m sitting here at the Microsoft MVP Summit this week and watching many presenters do the same thing. The sad thing, these are all Microsoft employees, many with polished decks. Here’s what they do:

Launch PowerPoint

Open the presentation

Click the Slide Show button in PowerPoint

Talk a bit using slides

Press Escape to stop Slide Show mode. This returns PowerPoint to Design mode. Some even said something like, “I guess now you know what’s coming up” when we all can see upcoming slides

Click a button on the Windows or Mac toolbar or launch Visual Studio or some other application

Do the demo

Go back to PowerPoint

Click the Slide Show button

Lather, Rinse, Repeat

Lather, Rinse, Repeat

Lather, Rinse, Repeat

In other words, they do this over and over again during their presentation. Why? Why do they do this? One reason is once you launch PowerPoint and then into Slide Show mode, there are two instances of PowerPoint running. If you Alt+Tab, you can see them, and the presenter gets confused about which one to activate when they want to go back to slides. The biggest reason is, they don’t know there’s a better way.

The first thing presenters need to remember is DO NOT LAUNCH POWERPOINT. Instead, open File Explorer. Navigate to your slide deck. Right-click. Select Show. Now, the deck will be launched in Slide Show mode and there is only one instance of PowerPoint running. When you want to go to your demo just Alt+Tab or hit the Windows key to bring up the Task Bar.

One word of caution. Do not hit Escape when in Slide Show mode because it will actually shutdown the slide show.

Yesterday I had the opportunity to attend Utah Code Camp. As I live in Salt Lake City, it was kind of like camping out in your backyard. It was great to catch up with old friends and make new ones.

The past few years I was one of the organizers of this camp, but this year I decided to step back. I will admit, it was good not having to keep things running and just be able to attend. I haven’t heard final attendance numbers, but Friday night, they were expecting between 600 and 700. That’s down from last year, but that was expected.

My session was right before lunch. The title was “A Little Non-chat About SignalR”. The idea came from my observation that most SignalR articles and demos show you how to create a chat app. I wanted something more realistic, that you would actually use in a real application. I have an application in production that uses progress bars and notifications, so it was a no-brainer to show how to do those. The other demo shows real-time data updates from one application to another.

Despite some pretty serious hardware issues and trouble with the projector, we got through everything and I think those of you who remained to end saw just how easy SignalR is. The demo code and slides are now available.

I’ll be speaking at two upcoming Code Camps. The first is Utah Code Camp on March 14 in Salt Lake City. The other is Boise Code Camp the following week, March 21.

My topic at both camps is “A Little Non-Chat About SignalR”. Here’s the description: Web applications typically work by the browser sending a request to the server. But what if you need the server to send a request to the browser? SignalR will let you do that. But, it seems like every SignalR demo shows how to develop a chat application. How many of us are going to do that? In this session, we’ll explore real world uses for SignalR, including thermometers for long-running server tasks, alerts, SignalR in non-web apps, and more.

I’m breaking a rule. What happens in Vegas doesn’t always stay in Vegas.

Yesterday was the first every Las Vegas Code Camp. You always expect bumps for a first time every camp. There were some things, but I think they were small. The organizers did a great job for a first ever camp. If I were to give out grades on the camp, here’s what they’d be: Networking: A, Sessions: A, Food A+++ (it wasn’t pizza). Those are the three things to look for in a code camp.

My session, “Software Gardening” was in the second time slot of the day. Thank you for attending. I heard some very good feedback, including one long-time, well-known .Net developer who told me the part about Katas made him think about their importance. Here is my slide deck.

I’m working on a project where the Bootstrap modal dialog is being used to popup data entry and other forms. I had it working using jQuery to handle the Ajax call to the Controller. It was a lot of work to gather the form data into the JSON packet, call the Controller and handle the return values, especially if the form did not pass validation.

I thought there had to be better way. Here are the requirements I had:

The Bootstrap dialog must be modal

The form should not be loaded into the browser with the page. It will be read from the server when needed and then displayed.

The data must be sent via Ajax to the server.

If possible, validation should be performed client-side, before sending data to the server.

The form should stay open if Model Validation fails and validation messages should be displayed.

I did what any good developer does…I hit a search engine and found several articles that I had to piece together to make it all work. The process is quite simple:

An Ajax request is sent from the browser.

The server returns a partial view to the browser.

Javascript code receives the form and it is placed as HTML into a specified div.

The form is displayed.

When the user clicks Save, the form data is sent to the Controller, just as any form data but with one exception…it’s sent as Ajax.

The Controller method validates the data. If it passes validation, the data is saved, a success message is sent back to the server and displayed to the user, then the form is closed. If the data fails validation, it is returned to the form and the validation messages are displayed.

First, I had to setup the initial request. I put an empty div in the _Layout.cshtml file. This way, I can display dialogs from anywhere in the application.

Look at the Ajax.BeginForm statement OnSuccess, OnFailure, OnBegin, OnComplete define Javascript functions that run when the specified event occurs. I created dummy methods that do nothing and placed them in genericDialog.js. Then, I can override each one, simply by providing a different Javascript function, for example updateAddressSuccess.

And that’s all there is to it. A simple dialog that is loaded on-demand. You can also use the same methodology to load ANY partial view on demand. For example, you may have a complex form with multiple tabs and only load the tab data when the user clicks on it. Finally, you can use this method to make your forms seem faster. Because you won’t have to load up everything in the form, it gets generated faster and less data is sent to the browser on initial load, making the application seem faster. Your users will thank you.

Four times a year, Microsoft sends out email announcements letting people know they have been awarded the Microsoft Most Valuable Professional (MVP). I look for this email to arrive on October 1. I’m humbled to say, I have been named an MVP for the 19th year. My original award was for FoxPro and now for Visual C#.

The MVP is given to people for providing community support. It can be through conference speaking, magazines, blogs, books, forum posts, etc. With about 4000 MVPs worldwide, just about every Microsoft product is covered. Recently, Microsoft started to give the award to supporters of open source projects.

One of the highlights of being an MVP is the annual MVP Summit. It’s three intense days meeting directly with Microsoft teams in Redmond. It’s a give and take where we get to discuss issues with Microsoft products and where we hope to see them go. The Microsoft teams also share their vision for the products. Because sessions are under a Non-disclosure Agreement (NDA), discussions are open and candid. This year the Summit will be the first week of November and highlighted with a visit from new Microsoft CEO Satya Nadella.

Thank you Microsoft for again giving me this award and thank you, community, for allowing me to visit your events and reading my column in DNC Magazine. I hope to see you at an event soon.

First, I’ll be at Denver Dev Day on June 14. This is a 1/2 day event on ASP.Net. There will be two sessions during the day, so sign up for either the morning or the afternoon. My presentation will be Unit Testing ASP.Net MVC. This is a topic I’ve been doing for a year or so now, but I have some new material that I will present for the first time in Denver.

Second, I’m excited to be returning to DevLink. The first time I attended, DevLink was in Nashville. It’s now held in Chatanooga. This year it runs August 27-29. I am preparing a new presentation, “Continuous Integration with TeamCity”. Here’s the synopsis:

In a good development process you check in your code often, sometimes multiple times per day. But what happens to that code once you’ve checked it in? With Continuous Integration or CI, that code is pulled from version control, compiled, unit tested, and more. In this session you will learn how to do CI with Team City. You’ll see how to configure Team City to do a complete build and test of your code and provide feedback to the team. You will learn – What is Continuous Integration and why should you care – How to configure Team City to pull code from version control – The difference between integration, daily, weekly, and other builds – What are artifacts and where they are stored – How to get feedback to your team.

I use TeamCity daily at work and am currently setting up Continuous Delivery with it. I don’t think I’ll have to get that far in this presentation, but if I touch on it, I will.