Dan Wahlin – Interface Technical Training https://www.interfacett.com Fri, 23 Jun 2017 23:40:06 +0000 en-US hourly 1 Video – JavaScript for C# Developers With Dan Wahlin https://www.interfacett.com/blogs/lunchtime-webinar-javascript-c-developers-dan-wahlin-sept-24-2014-1200psdmst/ https://www.interfacett.com/blogs/lunchtime-webinar-javascript-c-developers-dan-wahlin-sept-24-2014-1200psdmst/#respond Wed, 17 Sep 2014 17:26:35 +0000 http://www.interfacett.com/blogs/?p=?p=19160 Watch the complete video webinar   Is JavaScript worth taking the time to learn if I’m a server-side .NET developer? How much of C# carries over to JavaScript? In this Webinar Dan Wahlin will answer these questions and more while also discussing similarities between the languages, key differences, and the future of JavaScript (ES6). If … Continue reading Video – JavaScript for C# Developers With Dan Wahlin

The post Video – JavaScript for C# Developers With Dan Wahlin appeared first on Interface Technical Training.

]]>
JavaScript-Webinar-banner

Watch the complete video webinar

 


Is JavaScript worth taking the time to learn if I’m a server-side .NET developer? How much of C# carries over to JavaScript? In this Webinar Dan Wahlin will answer these questions and more while also discussing similarities between the languages, key differences, and the future of JavaScript (ES6). If you’ve been wanting to push more of your application’s functionality to the client by using JavaScript then this webinar will provide solid look at how you can get started.


Dan Wahlin teaches Web Development and .NET Visual Studio classes at Interface Technical Training. Dan’s instructor-led training includes JavaScript, AngularJS, jQuery, Node.js, HTML5, C#5 and Visual Studio 2012. Dan’s classes can be attended in Phoenix, AZ or online with Remote Live™.Dan Wahlin is the founder of Wahlin Consulting which specializes in Web technologies such as JavaScript, AngularJS, HTML5, Node.js, C#, ASP.NET MVC, Web API and SharePoint. He speaks at conferences and user groups around the world and has written several books on Web technologies. He has also published several courses covering a range of technologies including the new AngularJS JumpStart course. Dan blogs at weblogs.asp.net/dwahlin, runs the Web Weekly newsletter (a great way to stay up on the latest technologies), and writes columns for various technical magazines.

dan-wahlin-image

The post Video – JavaScript for C# Developers With Dan Wahlin appeared first on Interface Technical Training.

]]>
https://www.interfacett.com/blogs/lunchtime-webinar-javascript-c-developers-dan-wahlin-sept-24-2014-1200psdmst/feed/ 0
The Value of Adding AngularJS to your Development Stack – with Dan Wahlin https://www.interfacett.com/blogs/value-adding-angularjs-development-stack-dan-wahlin/ https://www.interfacett.com/blogs/value-adding-angularjs-development-stack-dan-wahlin/#respond Fri, 12 Sep 2014 17:51:33 +0000 http://www.interfacett.com/blogs/?p=?p=19125 AngularJS is one the hottest JavaScript group frameworks available today. Is it worth adding it to your development stack? That’s what we’re going to address in this video. AngularJS is one the hottest JavaScript group frameworks available today. Is it worth adding it to your development stack? That’s what we’re going to address in this … Continue reading The Value of Adding AngularJS to your Development Stack – with Dan Wahlin

The post The Value of Adding AngularJS to your Development Stack – with Dan Wahlin appeared first on Interface Technical Training.

]]>
AngularJS is one the hottest JavaScript group frameworks available today. Is it worth adding it to your development stack? That’s what we’re going to address in this video.


AngularJS is one the hottest JavaScript group frameworks available today. Is it worth adding it to your development stack? That’s what we’re going to address in this video.

What’s so compelling about AngularJS anyway? First, it’s a client-side JavaScript framework with one core script. You won’t have to integrate a ton of third‑party scripts into your applications and then worry about whether or not those scripts are going to be compatible in the future. AngularJS is supported by a full‑time team at Google and has over 800 individual open-source contributors as well.

With AngularJS you can send and retrieve data from RESTful services, minimize code significantly by using data binding, provide a way to organize code and allow teams to work together more efficiently, switch between application screens quickly without reloading the entire page, create unit tests to ensure your code is working properly, validate data submitted by users, and, well, I could go on and on. It has a lot of great features baked into the framework.

For me, the power of AngularJS is that you can create snappy, single-page applications very quickly and they perform like they were built for the desktop. The code is flexible. It’s easy to deploy, update, and reuse. A great side benefit is the any app can be used on multiple devices.

Let’s talk about three key scenarios where AngularJS can be leveraged in the real world. Also discuss the AngularJS End-to-End SPA Development class at Interface Technical Training and how it can help you learn this powerful framework quickly and efficiently and at a professional level.

The scenarios that we’ll discuss include how to maximize productivity, write less code, and make your code more powerful and easier to maintain. How to migrate from existing technologies and allow apps to run on multiple devices and finally, how you can leverage AngularJS features to replace desktop applications with cross‑device web applications.

Let’s jump right into scenario one.

I was recently teaching a small team from a large global electronics manufacturer who is building an enterprise‑scale lab that was going to be used by a lot of people throughout the company. They had previously done most of their web development using jQuery and needed more of a framework for the new application.

JQuery is arguably the most popular client-side JavaScript library in existence and for good reason. I’ve used it for many years and I love it. I love what it does as far as making AJAX calls, manipulating the DOM, handling events, and of course you can animate objects.

For all its strengths it does have a few weaknesses. For example, if you have a form with 10 controls in it, say eight text boxes and two dropdowns, then with jQuery you’re looking at writing a minimum of 20 lines of code just to get data in and out of the controls. What if the IDs are CSS class name changes on your controls? You end up having to modify your JavaScript code to accommodate the name changes. That’s a little bit challenging.

I call this a control oriented approach since your code relies heavily on the IDs defined on the controls. You’re always interacting with the controls to get data into and out of them. With AngularJS you can take advantage of a newer and more modern development technique, I like to call the data oriented approach.

Using the data binding functionality built into Angular you can wire up property values to controls and when the property values change the controls change. Likewise, if the controls change, the property values change. It’s quite magical actually. Once the data binding is in place you can modify the styles of controls, change their visibility, change their values, and do much more by changing property values.

By using the data oriented approach you don’t care about the ID or the CSS class on the control any longer. In fact in many situations you won’t need to give your controls an ID. Imagine how much code you can cut from your web applications. To top it off jQuery plays nicely with AngularJS in case you want to leverage specific jQuery features.

I know many companies have used Adobe Flex and Microsoft Silverlight so that they can have a 100 percent control over the application, but still have the ability to deploy it through a browser. It’s no secret that both Flex and Silverlight are getting less emphasis nowadays, especially since they don’t run on all devices.

What’s the alternative? If you’re thinking about migrating from Adobe Flex or Microsoft Silverlight then AngularJS is a natural transition. You’ll actually be pleasantly surprised at how many concepts carryover such as data binding, a declarative syntax, animations, and much more.

Once you understand the core features provided by AngularJS you’ll be extremely productive in no time at all. Now keep in mind there are other alternatives out there such as Ember.js, Backbone, Durandal, et cetera. The good news is that many of the same concepts carryover. For me personally, I like AngularJS for all the reasons I’ve mentioned up to this point.

For the final scenario, I worked with an international financial company who has a large desktop application installed base. They want to simplify the process of deploying their apps due to the number of devices being used in the organization, especially since the desktop apps won’t run on all these devices.

Traditional applications weren’t a good fit for the web since every user action caused the entire page to reload. With AngularJS, however, you can build snappy single-page applications to feel much more like desktop applications, but they have the added benefit of being able to run on tablets, phones, and other devices that employees may be using.

Here’s the actual app that you’ll be building in the AngularJS End‑to‑End course at Interface Technical Training. You’ll actually put together all the pieces yourself. For instance, you’ll incorporate RESTful services and database interactions, AJAX calls from AngularJS to those services, and learn about all the great functionality provided by AngularJS on the client-side such as data binding, validation, factories and services, custom directives, controllers, scope and more.

The app that you’re building in class will perform much like a desktop application, but it’s going to be able to run on any device. The AngularJS End‑to‑End development course will provide you with all the skills needed to build single page applications, enhance productivity and code reuse, and provide that desktop‑like feel with a web deployment model.

You’ll be able to ask all the questions you want throughout class to ensure that you take away the best practices used by some of the biggest companies in the world as they build AngularJS applications.

There you have it, three real‑world examples where AngularJS can be used to streamline the development of web applications and a summary of interfaces in End‑to‑End class. You’ll actually build an app from start to finish and learn to leverage the power of AngularJS at a professional level.

Now, the course can be attended here in Phoenix, AZ or online using our amazing RemoteLive™ technology. Check out the course outline for more details and I hope to see you soon in class.

Dan Wahlin
Web Development Instructor
Interface Technical Training

The post The Value of Adding AngularJS to your Development Stack – with Dan Wahlin appeared first on Interface Technical Training.

]]>
https://www.interfacett.com/blogs/value-adding-angularjs-development-stack-dan-wahlin/feed/ 0
Video – The Fundamentals of C# Generics by Dan Wahlin https://www.interfacett.com/blogs/video-c-generics-fundamentals-by-dan-wahlin/ https://www.interfacett.com/blogs/video-c-generics-fundamentals-by-dan-wahlin/#comments Wed, 14 Aug 2013 16:11:56 +0000 http://www.interfacett.com/blogs/?p=?p=14681 In previous posts, we looked at getting started with C# Interfaces and understanding Polymorphic behaviors. In this post, we’ll look at using C# Generics and Custom Generics in your applications. Video transcription  Dan Wahlin: An important feature of the C# language is Generics. Generics are not necessarily new. They have actually been out since C# 2. They … Continue reading Video – The Fundamentals of C# Generics by Dan Wahlin

The post Video – The Fundamentals of C# Generics by Dan Wahlin appeared first on Interface Technical Training.

]]>

In previous posts, we looked at getting started with C# Interfaces and understanding Polymorphic behaviors. In this post, we’ll look at using C# Generics and Custom Generics in your applications.

Video transcription

 Dan Wahlin:

An important feature of the C# language is Generics. Generics are not necessarily new. They have actually been out since C# 2. They provide a way to make it a lot easier to work with code, but also to make your code more efficient, and definitely catch errors up front as you’re writing the code.

In this particular demonstration, I’m going to walk through some just fundamentals of what C# Generics are and how you can use them in applications.

To start, I have a little bit of code here already ready to go. You can see that I have a car class,

Add Class to # Generics Fundamentals by Dan Wahlin

This car class has an ID, and it has a make property. Now, we can certainly add others, such as make, model, year, things like that. But we’re just going to go with two simple ones.

[0:48] What I have here in my program is, we’re going to new up our car. Then I’m going to use, this is a special type of initializer, called an object initializer.

002-object0initializer--C#-Generics-Fundamentals-by-Dan-Wahlin

This is going to assign the properties for ID and make, and then we’re going to use these.

[1:04] One way I could put these two Cars into a collection, is to actually use an Array. The problem with Arrays in C# is they’re fixed length. If you want to keep adding to that, you’d have to make new Arrays, and then copy back and forth between them. It’s a little bit problematic.

What I’m going to do instead is use a Generic. You’ll notice that we’ve already included System.Collections.Generic.

System.Collection.Generics # Generics Fundamentals by Dan Wahlin

This is the default namespace for Generics in C#. If you’re new to them, this is one that’s actually added for you in the projects.

[1:36] What I’m going to do is create a List of Car like this:

List. # Generics Fundamentals by Dan Wahlin

We can definitely use the var then select a List of Car, like this.

varC in Visual Studio # Generics Fundamentals by Dan Wahlin

Or, I’m actually a pretty big fan of the var, which basically lets the compiler figure out the data type on the fly. We’ll say Cars is new List of Car. That’s how I think of the Generic marker brackets <>.

006-varC#-Generics-Fundamentals-by-Dan-Wahlin

[2:06] What a Generic is? It is a code template. What’s going to happen here is I want to create a List but a specific type. I could always create a List of Object. We could use the old ArrayList as an example. The problem with that is, when you add it, it gets what’s called “boxed”. Then, when you pull it out, you have to cast it from an Object or convert it back to a Car.

By doing a Generic, I get a strongly‑typed way to add it. In fact, you’ll see here, if I just do a new List and then say Cars., notice when I do add here that we can only add a Car.

Add list in Visual Studio. # Generics Fundamentals by Dan Wahlin

[2:40] If I try to add an INT or a STRING or a BOOL or an Engine, or something else, it’s not going to work properly. That’s as it should be. That catches us as we compile, and it makes it so we don’t add stuff we shouldn’t be adding.

What I’m going to do is I’m going to use a little default Array, or Collection Initializer, you’ll hear it called. I’m going to create a List of Car and then do a { and end } and a semicolon (;)

008-C#-Generics-Fundamentals-by-Dan-Wahlin

[3:06] In here, I can actually just put my Cars and just list them. I could call Cars.Add > Cars.Add > Cars.Add etc… But this provides an easy way, since I already have the Cars, to add them into the Array.

We’ll do: Car1, Car2. Just to prove this works, let’s go ahead and do a quick foreach then tab tab, get a code snippet going.

Using Foreach in Visual Studio. C# Generics Fundamentals by Dan Wahlin

We’ll say foreach Car in Cars. Let’s write out Car.Make. We should get Ford and Toyota written out.

Foreach var C# Generics Fundamentals by Dan Wahlin

Let’s go ahead and run it. You can see that indeed it works.

Running foreach. C# Generics Fundamentals by Dan Wahlin

We get Ford and Toyota, and we’re ready to go.

[3:50] That’s one aspect of Generics. I’d argue that’s the aspect that you’ll probably use the most, just simply creating custom collections of a specific type. I prefer to go with List in many scenarios, where I just have a set of objects, and I want to add them into an Array structure, then we can use the List to do that.

Another aspect of Arrays is that you can actually control what types are added into a class. We did that here with the List. We said a Car can be added into the List, but now let’s make a custom Generic.

I have this Engine here, V8.

011-custom-generic-C#-Generics-Fundamentals-by-Dan-Wahlin

Let’s add a new property. I’m going to make a property we’ll call Engine.

I’ll just do a prop, tab, tab for the code snippet.

Custom Generic. C# Generics Fundamentals by Dan Wahlin

I’m going to have an unknown here.( I’ll come back to that.) I’m going to call this property Engine.

013-custom-generic-C#-Generics-Fundamentals-by-Dan-Wahlin

It’s not going to work as is because the Visual Studio IntelliSense is saying, “Whoa. What are you doing with the question mark?”

Error in Custom Generic. C# Generics Fundamentals by Dan Wahlin

What do I want to put here? If I wanted to hard‑code V8, I could. We could come in and say V8.

V8 Custom Generic. C# Generics Fundamentals by Dan Wahlin

The problem with that is now I’m locked into a V8.

[5:01] What if I want the user of my Car class to be able to supply the type of engine that they actually want to use? We could do that through Generics. What I’m going to do is put a little marker up here, and I’m going to name it <T>.

Custom Marker in VIsual Studio using C# Generics

This is something that Microsoft does. It just basically stands for what’s the type that you would like the user to be able to add in for this Car.

What I’m going to do is replace V8 with the T.

T Type custom C# Generics Fundamentals by Dan Wahlin

What I’m saying is that, at this point, I don’t know what the type of Engine is. It’s a Generic template. If I were to supply, for <T> V8, then the compiler, at compile time, will come in and put a V8 right here.

[5:44] If I said it was a V6 for T, then it would put in a V6. The compiler will take care of that automatically. Now, we have a way to be really flexible.

I’m allowing people that use my Car to supply the type of the Engine that they actually want to work with, as opposed to me hard‑coding that type of Engine. It makes our applications more flexible and easier to maintain.

Let’s go ahead and use this now.

We can see that our V8 just has a Horsepower, down here.

Public Class. Custom Generic in C# Visaul Studio.

What I’m going to do then is allow us to create a V8 and assign it, but I obviously have to supply what is the T, what’s the template marker, and what do I want to replace that with. Right now, I only have one Engine, V8. Imagine that I had several. Then we could go in, and use those others.

[6:37] Let’s come on back over to Program.cs and notice that Visual Studio now says, “Hey, I don’t really like what you’re doing here with Car.”

019-public-class-custom-generic-C#-Generics-Fundamentals-by-Dan-Wahlin

The reason is it doesn’t know the type of Engine. You’ll even see in the IntelliSense there, that it says, “1 type arguments” parameter is required.

[6:53] What I’m going to do is say I want a Car1 of a V8 Engine. Then, down here, I’ll say I want a Car2 of a V8 Engine.

Public Class custom C# Generics Fundamentals by Dan Wahlin

Going with the same concept here, we could say Engine = new V8.

021-public-class-custom-generic-C#-Generics-Fundamentals-by-Dan-Wahlin

Now, I’m able to add in a specific type of Car of V8. If I were to, here in the Engine, do V6, let’s see what happens, even though I don’t have a V6.

String parameter C# Generics Fundamentals by Dan Wahlin

That’s going to error out, and our Car is now also going to error out.

[7:37] It’s not going to allow that to be passed, because we’re now saying that you have to pass a V8, but yet I tried to pass a V6. Even if I had a V6, it wouldn’t take it, and it wouldn’t compile because we told it was a V8. It makes it really nice to catch errors and issues up front, as opposed to, say, at runtime.

You’ll notice that this looks good. We’re ready to go with our custom Generic, but with the List, it’s choking. If we mouse over it, it says it requires one type parameter.

New List C# Generics Fundamentals by Dan Wahlin

I need to say that the Car is a Car of V8 Engine.

024-new-list-C#-Generics-Fundamentals-by-Dan-Wahlin

[8:17] We can add a new List of Car. For the Car, I have to say what the T is because, again, look back at Cars.cs, we said there was a T.

Get Type T C# Generics Fundamentals by Dan Wahlin

I’m saying what the T is. It’s a V8, and it takes it. If we run it, we’re not going to see much different than when we initially ran it.

Run custom type C# Generics Fundamentals by Dan Wahlin

Let’s go ahead and iterate through as we write out the Car.

[8:42] I’m also going to include (car.Engine) and I’m going to write out just the (.ToString).

027-run-C#-Generics-Fundamentals-by-Dan-Wahlin

You can see that we do indeed have a V8 for the Ford and a V8 for the Toyota.

028-run-C#-Generics-Fundamentals-by-Dan-Wahlin

I have a custom Generic plus we’re using the .NET Generic to actually track our collection, and make it accessible and easy to work with.

That’s a quick introduction to some of the different things you can do with C# Generics.

Dan Wahlin Microsoft MVP
HTML5 and .NET Developer Instructor
Interface Technical Training

The post Video – The Fundamentals of C# Generics by Dan Wahlin appeared first on Interface Technical Training.

]]>
https://www.interfacett.com/blogs/video-c-generics-fundamentals-by-dan-wahlin/feed/ 2
Video – Understanding C# Interfaces and Polymorphic Behavior by Dan Wahlin https://www.interfacett.com/blogs/video-understanding-c-interfaces-and-polymorphic-behavior-by-dan-wahlin/ https://www.interfacett.com/blogs/video-understanding-c-interfaces-and-polymorphic-behavior-by-dan-wahlin/#comments Mon, 12 Aug 2013 17:27:41 +0000 http://www.interfacett.com/blogs/?p=?p=14590 Video transcription  Dan Wahlin: In a previous session Getting Started with C# Interfaces, I talked about how you can use C# Interfaces to drive consistency across different C# Classes. In this session, I’m going to walk through one of the cool features of Interfaces, which is Polymorphic Behavior. Polymorphism is not only a big word … Continue reading Video – Understanding C# Interfaces and Polymorphic Behavior by Dan Wahlin

The post Video – Understanding C# Interfaces and Polymorphic Behavior by Dan Wahlin appeared first on Interface Technical Training.

]]>

Video transcription

 Dan Wahlin:

In a previous session Getting Started with C# Interfaces, I talked about how you can use C# Interfaces to drive consistency across different C# Classes. In this session, I’m going to walk through one of the cool features of Interfaces, which is Polymorphic Behavior.

Polymorphism is not only a big word you can impress friends and family with, it’s actually really useful for what I call future-proofing your apps.

Let me show you an example. We currently have a car and a lawn mower and both of those implement “IMachine”, which simply has a start and a stop method.

C# Interfaces and Polymorphic Behavior

Both of them have to have those two methods or it won’t compile. We’ve already shown that earlier in a previous session Getting Started with C# Interfaces.

[0:42] What I’m going to do now is, let’s assume that this is our big program here.

002-program-cs-C-Interfaces-and-Polymorphic-Behavior

I want to add a particular method into this program that actually handles starting or maybe even stopping the car or the lawn mower. But I don’t want to hard-code that it only handles a car or a lawn mower.

[1:00] Let me show you what I mean. Let’s say that I had a static void start and it took a car.

Static Voide main C# Interfaces

I could obviously do some logging, so we’ll pretend that our app does a little bit of logging. Then we could call “car.start,” and we’d be off and running.

Static Voide Logging C# Interfaces and Polymorphic Behavior

That’ll work, and I could come in then and we could kind of cut and paste this. We could change this to a lawnmower. I’ll just change this to “m,” for “mower,” and we could say “m.Start.”

005-static-void-logging-mower-C-Interfaces-and-Polymorphic-Behavior

There’s nothing wrong with that. I now have an overload, and it would work, but now it would have to know which of these two to actually call.

[1:43] To demonstrate Polymorphic Behavior, we’re going to leverage the Interface, and what I’m going to do is allow someone to pass in an IMachine.

Implement Machine C# Interfaces and Polymorphic Behavior

Now, we’ll just call this “machine.” What the machine can do now is we know if it implements this Interface it has a start, or stop because, otherwise, it wouldn’t have compiled in the first place.

[2:06] We’ll go ahead and change this to “machine.start.” Imagine that we did some logging and then we’re going to start it. Now, what can it start? The answer is anything that implements IMachine.

007-machine-start-C-Interfaces-and-Polymorphic-Behavior

When I first learned this I thought this was useless because you can’t create an instance of an Interface. As I showed earlier, an Interface has no implementation.

Previouse code C# Interfaces and Polymorphic Behavior

Therefore, you can’t “new” it up or use it in any really useful way. You can just implement it, that’s it.

[2:37] The first time I saw this I said, “That’s useless code. Why would you ever do that?” The answer is I’m not saying “Pass me an IMachine.” I’m saying, “Pass me anything that implements IMachine.” We know that car implements IMachine and we know that lawnmower implements IMachine, therefore they are candidates to pass in.

Let’s come on down, we’ll do a new class and we’ll just call it “truck.”

New Class truck C# Interfaces and Polymorphic Behavior

In the truck, we could also have some type of methods. I’m not going to be able to pass, obviously, a truck into the start because it doesn’t implement IMachine. Of course, if I make it implement IMachine, and then implement the members, it would work.

Implement New Class C# Interfaces and Polymorphic Behavior

[3:22] Let’s assume that we just have car and lawnmower. Now what we can do is, let’s take out this extra line.

011-C-Interfaces-and-Polymorphic-Behavior

Now I’m going to call start and pass in the car. We’ll call start again and pass in the mower.

Start Interface C# Interfaces and Polymorphic Behavior

This is a console app, so I’m just going to do a console read real quick so it stays up.

Console Read C#

[3:43] Now let’s run and see what kind of output we get and you’ll notice it worked. We got car started and lawnmower started.

Run C# Interfaces and Polymorphic Behavior

That is Polymorphic Behavior, so let’s define that a little more specifically.

Polymorphism ‑‑ if you’re not familiar with the term ‑‑ is when an object implements a particular set of behaviors, such as start and stop. But the way it performs between the objects, the start or the stop is different.

A simple example I like to give in the classroom scenario is, animals speak. A dog speaks, a cat speaks, but the way they speak is different. A cat meows, a dog barks, a monkey…I don’t know…ooh, ooh, ah, ahs, or something like that.

[4:25] In the case of a car and a mower, they both start, but the way they start is going to be different. Obviously, it’s different because I typed some text. In real life, it’s very different because, in a car, you’re obviously turning a key, or maybe pushing a button whereas in a lawnmower, you’re probably pulling a string, or something like that. Unless it’s a riding lawnmower, I suppose.

This now illustrates how we can future-proof our app. Let me prove it by adding one more class into the mix.

Add Class C# Interface

Let’s say that we also add an Airplane.cs.

[4:59] To be consistent, I’ll make this public and I’ll implement IMachine, because an airplane would have a start and a stop.

016-add-class-airplane-C-Interfaces-and-Polymorphic-Behavior

017-implement-interface-airplane-C-Interfaces-and-Polymorphic-Behavior

Then we’ll just do the same type of thing here. We’ll say airplane started and we’ll return whatever we want, in this case “true“.

Airplane start C# Interfaces and Polymorphic Behavior

We’ll also make a console right line that airplane stops.

019-airplane-stop-C-Interfaces-and-Polymorphic-Behavior

[5:41] Assuming that this had real code in it that actually does something useful, now, without ever changing this method, I can pass in an airplane. Because airplane implements IMachine and so, now, I come in and say, “VARAIR=new airplane.”

Var New C# Interfaces and Polymorphic Behavior

Now we can call start. You’ll see an Intellisence, it says “pass me an IMachine.”

Intellisence C# Interfaces and Polymorphic Behavior

We can go ahead and pass it the air, for the airplane.

022-intellisence-airplane-C-Interfaces-and-Polymorphic-Behavior

[6:09] You’ll really see Polymorphism because we have three things that all start, but the way they start is different. The car writes car, lawnmower or airplane, as you see here.

023-polymorphic-behavior-C-Interfaces-and-Polymorphic-Behavior

That is a really powerful feature in programming. Because if you have methods in your code, like start that need to be able to accept different parameters, and you want to future-proof it, because you might not know all the objects that might be passed in the future to this method.

[6:35] Then, by implementing an Interface, I’m now writing very loosely coupled code, we call it because I can swap things in and out, and not have to rewrite this entire method. In fact, this method doesn’t even know what it’s doing. It just knows that I can call “start,” and whatever object you pass in, then it will invoke the start on that particular object.

That’s a quick and simple example of getting started with C# Interfaces and Polymorphic Behavior. I hope it helps you kind of rethink through how you architect your apps and how you can do some future proofing of some of your different methods in other Classes.

Next we’ll look at using C# Generics in your applications.

Enjoy!

Dan Wahlin Microsoft MVP
HTML5 and .NET Developer Instructor
Interface Technical Training

The post Video – Understanding C# Interfaces and Polymorphic Behavior by Dan Wahlin appeared first on Interface Technical Training.

]]>
https://www.interfacett.com/blogs/video-understanding-c-interfaces-and-polymorphic-behavior-by-dan-wahlin/feed/ 4
Video – Getting Started with C# Interfaces by Dan Wahlin https://www.interfacett.com/blogs/video-getting-started-with-c-interfaces-by-dan-wahlin/ https://www.interfacett.com/blogs/video-getting-started-with-c-interfaces-by-dan-wahlin/#comments Wed, 07 Aug 2013 21:52:18 +0000 http://www.interfacett.com/blogs/?p=?p=14520 Video transcription  Dan Wahlin: One of the topics that I get the most feedback or comments or questions about is C# Interfaces. What are they? Why would I use them? Why are they important in our applications? What I’m going to do is try to break down C# Interfaces in just a real short little … Continue reading Video – Getting Started with C# Interfaces by Dan Wahlin

The post Video – Getting Started with C# Interfaces by Dan Wahlin appeared first on Interface Technical Training.

]]>

Video transcription

 Dan Wahlin:

One of the topics that I get the most feedback or comments or questions about is C# Interfaces. What are they? Why would I use them? Why are they important in our applications? What I’m going to do is try to break down C# Interfaces in just a real short little segment here and make some sense of why you might use those in your applications. The short answer is we’re going to use C# Interfaces to drive consistency.

Let me show you what I mean by that. Let’s take a look at a little visual here.

What is a C# Interface

Imagine that I have a factory and in this factory, we’ll say times are tough, and I have a single worker. I only want to train them on one methodology for starting and stopping machines.

[0:47] For example, I have a square machine, a whatever‑that‑shape‑is machine, because I honestly don’t remember, triangle machine, and all those types of things. Each one of these has a start and a stop and you’ll notice, visually, that they’re actually green for the start and red for the stop.

Now, I think you look at that and say, well, that’s pretty simple. I get that you only have to train the worker once to press green for start, red for stop, and that’s a consistent visual Interface.

[1:15] Now, let’s imagine that these, instead of being machines, are Classes, and the Classes need to have start and stop methods. How are you going to drive consistency across these three different Classes, and you might have more than that, and do so in a way that kind of adheres to the standard in the .NET framework? One of the ways we can do it is Abstract Classes. But in this particular segment, we’re going to focus on Interfaces.

Interfaces provide a way where, if you have objects that aren’t even similar to each other but you want to drive a consistent Interface, consistent set of methods that they support or properties, then you could write an Interface that they all implement. I’m going to demonstrate that here.

Programming with C# Interfaces

If we do a demonstration of this, which I’ll do in a moment, you’ll see that Interfaces provide a great way to drive consistency across objects. That’s kind of summed up here as far as the purpose of Interfaces.

[2:13] Number one, they just provide consistency. You’ll also see that they provide some Polymorph ic benefits, which I’ll talk about here at the end.

Interfaces, in a nutshell, are simply a code contract. If you go and buy a house, you’re going to assign some type of a mortgage contract that says you agree to pay whatever it is per month.

[2:33] Well, in the C# world, if I want to drive consistency and say that a set of objects has a start and a stop method, and I want all of my objects to have that, then one way I can do it is to implement an Interface. That’s a contract that says, “I hereby agree to put a start and a stop into my Class.”

Interfaces themselves, which we’ll see in a moment, do not have any implementation details. In fact, Interfaces are really simple and they don’t do anything, except find data types and names. I’ll show you that in just a moment. Then, Classes that implement the Interfaces, that’s like going to the mortgage broker or the bank and agreeing to pay whatever it is per month for that particular mortgage.

[3:19] In the case of coding, a Class that implements an Interface is signing their name on the dotted line that they hereby agree to put in certain methods or properties, as defined by the Interface.

Let me show you an example here in Visual Studio. I’m just going to come in and let’s create a real simple project here.

I’m going to do a console project. Let’s go ahead and select “C#,” “Windows Console.” I’m going to call this, “Interfaces Demo.”

Opening a new project in Visual Stdio

We’ll go ahead and save that away. We now have a Class called “Program.”

Program.cs in new Visual Studio project

What I want to do is add some other Classes in. These other Classes actually support maybe an Interface.

[4:07] Let’s define what the Interface is and let’s do exactly what I showed you earlier. We’ll come in and we’ll add a new item. One of the items I can add is an Interface you can see here.

006-add-new-item-interface-visual-studio-console-application-c-interface

That Interface, I can give it whatever I want, let’s just call this “IMachine.cs”

You’ll notice that I started it with a capital “I.” That is a convention in .NET that you should follow. You certainly don’t have to, but I’m a big fan of it, and you’ll see that throughout most .NET programs.

[4:40] What an Interface defines is simply some basic members. Let’s say that there’s a Boolean return from a start and from a stop.

Bollean return in Visual Studio creating C# Interfaces

There you have it. That’s an Interface.

[4:51] You might look at and say, “That doesn’t even do anything.” You’re right. Interfaces do nothing. They simply define different members, start and stop in this case, and what they return, but they don’t actually have an implementation, as I mentioned earlier.

What we can do with this Interface, though, is, let’s say that we have a car.

008-car-class-visual-studio-console-application-c-interface

That car might have maybe a start and stop. We’ll come on in to “Class,” we’ll do a car, and we’ll say this car’s also public.

Puplic Class attribute in C# Interface

I’m now going to implement that Interface by saying, “IMachine.”

010-car-class-public-visual-studio-console-application-c-interface

[5:30] Typically, you’ll see a lot of names in Interfaces that are actions. I could have called this “I Startable Stoppable,” or some goofy name like that, but we’ll go ahead with something simple, like “IMachine.”

If I try to build, watch what happens here.

Visual Studio build error in C# Interface

We get two errors. That’s because it says, “Car does not implement.” Of course, we have to implement “Start” and “Stop.” Otherwise, it’s not going to work properly. I could go to the trouble to write all that code to make this Class implement the contract that we signed, now. This is like signing the dotted line, right here.

[6:05] But instead we can just right‑click on it and say, “Implement Interface,” and then choose this first one, “Implement Interface Again.”

Implementing the C# Interface

That pops in the code for us.

013-implement-interface-visual-studio-console-application-c-interface

Then we can come in and do something simple here. We’ll say, “Car Started.” And we’ll take this one out, and we’ll do the same thing, and we’ll say, “Car Stopped.”

014-implement-interface-visual-studio-console-application-c-interface

[6:28] Of course, you would go in and write a real implementation and do something a little more official, and then we have to return a boolean. I’m going to assume our car always works, because ours is a very good car, and we’ll just return “True.”

015-implement-interface-visual-studio-console-application-c-interface

But in a real app this is of course where you would go in and figure out what should be returned based on the database update succeeded or whatever it was you were doing.

Now let’s try to build, and you can see we built.

Run the new C# Interface in Visual Studio

Now we have implemented an Interface.

[6:56] At this point, though, you might go, “I don’t know what that’s really buying us.” Let’s say that we also had a lawnmower. A lawnmower obviously is not an automobile, or something like an automobile, so we can’t really inherit or use abstract Classes, but I can drive consistency across the car and a lawnmower by using Interfaces. What we can do is do a lawnmower, and we can do the same thing.

Building the lawnmower class in Visual Studio for C# Interfaces

I’m going to again say that we’ll assume this is public, and we’ll implement “IMachine.”

018-public-class-lawnmower-interface-visual-studio-console-application-c-interface

I’ll then right‑click, implement the Interface, and we’re off and running.

Now I can basically do the same thing. Let me just write a little bit of code real quick, “Lawnmower Started.” And we will say that we also have very trustworthy lawnmowers that never go down.

019-public-class-lawnmower-interface-visual-studio-console-application-c-interface

And we’ll do a Console.WriteLine here, “Lawnmower Stopped.”

020-public-class-lawnmower-interface-visual-studio-console-application-c-interface

At this point, we have now driven consistency across our app, and we can now use this to actually do something. Now let’s come into Main of our program.

021-public-class-lawnmower-interface-visual-studio-console-application-c-interface

[8:13] We can fire up a car. Notice when I do “Car.” you’ll see “Start” and “Stop” in there.

Start / Stop attributes in the C# Interface

Then we could also fire up a lawnmower. When I do “Mower.” you’ll also see “Start” and “Stop.”

Start and Stop commands in C# Interface

At this point, you might say, “I don’t really see the benefit.” Really, all we’ve done at this point is driven consistency across two objects that wouldn’t necessarily inherit from the same base Class.

[8:46] It’s possible they could. I suppose you could argue that you might have an abstract Class called “Machine,” or something like that, that has “Start” and “Stop” that you inherit down. But by using Interfaces, the objects don’t even have to be directly related through inheritance, and yet I’m able to drive consistency across these objects. Pretty cool.

What we’ll do in a follow‑up to this is show where this gets really useful with Polymorphic Behavior, but we’ll end here by talking about, This is the role of Interfaces, what you can do with them, and how you can drive consistency across Classes with Interfaces.

In the next video post, we’ll see how to “future-proof” our apps using Polymorphic Behaviors in our C# Interfaces.

Dan Wahlin Microsoft MVP
.NET Developer Instructor
Interface Technical Training

The post Video – Getting Started with C# Interfaces by Dan Wahlin appeared first on Interface Technical Training.

]]>
https://www.interfacett.com/blogs/video-getting-started-with-c-interfaces-by-dan-wahlin/feed/ 3
AngularJS Video Series Part III – Views, Controllers and Scope https://www.interfacett.com/blogs/angularjs-video-series-part-iii-views-controllers-and-scope/ https://www.interfacett.com/blogs/angularjs-video-series-part-iii-views-controllers-and-scope/#comments Thu, 11 Jul 2013 17:29:55 +0000 http://www.interfacett.com/blogs/?p=?p=14196   Previous videos in this series: Part I: Getting Started with AngularJS Video Series Part II: Directives, Filters and Data Binding   AngularJS follows the MVC (Model-View-Controller) pattern and allows controllers to be defined that can act as the “brain” for a given view. In this video you’ll see how to define views and hook … Continue reading AngularJS Video Series Part III – Views, Controllers and Scope

The post AngularJS Video Series Part III – Views, Controllers and Scope appeared first on Interface Technical Training.

]]>

 

Previous videos in this series:

 

AngularJS follows the MVC (Model-View-Controller) pattern and allows controllers to be defined that can act as the “brain” for a given view. In this video you’ll see how to define views and hook them to controllers. You’ll also see the role that scope plays and how it acts as the glue between a view and a controller. If you’ve ever dealt with “view models” before then you’ll feel right at home with scope since it acts as the “model” for a given view. Check out the video for more details!

Views, Controllers and Scope

 

 

If you’re interested in learning more about AngularJS as well as several other libraries/frameworks such as jQuery, Knockout, Node.js, and Grunt check out the new JavaScript and SPA Programming with jQuery, Knockout, AngularJS, Node.js and Grunt course at Interface Technical Training.
Enjoy!
Dan Wahlin Microsoft MVP
.NET Developer Instructor
Interface Technical Training

The post AngularJS Video Series Part III – Views, Controllers and Scope appeared first on Interface Technical Training.

]]>
https://www.interfacett.com/blogs/angularjs-video-series-part-iii-views-controllers-and-scope/feed/ 1
AngularJS Video Series Part II: Directives, Filters and Data Binding https://www.interfacett.com/blogs/angularjs-video-series-part-ii-directives-filters-and-data-binding/ https://www.interfacett.com/blogs/angularjs-video-series-part-ii-directives-filters-and-data-binding/#respond Fri, 07 Jun 2013 14:20:18 +0000 http://www.interfacett.com/blogs/?p=?p=13049   Previous videos in this series: Part I: Getting Started with AngularJS Video Series One of the features that really sets AngularJS apart from other client-side frameworks is its built-in support for directives and data binding. In a nutshell, directives extend HTML and teach it new tricks that it can’t perform natively. Many directives are … Continue reading AngularJS Video Series Part II: Directives, Filters and Data Binding

The post AngularJS Video Series Part II: Directives, Filters and Data Binding appeared first on Interface Technical Training.

]]>

 

Previous videos in this series:

One of the features that really sets AngularJS apart from other client-side frameworks is its built-in support for directives and data binding. In a nutshell, directives extend HTML and teach it new tricks that it can’t perform natively. Many directives are included out-of-the-box such as ng-repeat, ng-model, ng-class, and ng-app to name just a few (see the complete list here).  Data binding functionality can be combined with directives (and often is) to allow controls to be updated as object properties change and vice versa. By leveraging the data binding features in AngularJS you can significantly reduce the amount of code required to build a data-driven application.

In the next video in the AngularJS video series I discuss how directives can be used along with data binding and filtering in AngularJS.

Directives, Filters and Data Binding

 

If you’re interested in learning more about AngularJS as well as several other libraries/frameworks such as jQuery, Knockout, Node.js, and Grunt check out the new JavaScript and SPA Programming with jQuery, Knockout, AngularJS, Node.js and Grunt course at Interface Technical Training.

Enjoy!
Dan Wahlin Microsoft MVP
.NET Developer Instructor
Interface Technical Training

 

The post AngularJS Video Series Part II: Directives, Filters and Data Binding appeared first on Interface Technical Training.

]]>
https://www.interfacett.com/blogs/angularjs-video-series-part-ii-directives-filters-and-data-binding/feed/ 0
Getting Started with AngularJS Video Series https://www.interfacett.com/blogs/getting-started-with-angularjs-video-series/ https://www.interfacett.com/blogs/getting-started-with-angularjs-video-series/#comments Wed, 15 May 2013 15:32:51 +0000 http://www.interfacett.com/blogs/?p=?p=12789 As more and more functionality moves to the client developers are finding that writing JavaScript-centric applications from scratch can be challenging and frustrating. As a result, numerous JavaScript libraries and frameworks have sprouted up to help simplify the development process such as Knockout, Backbone, Ember, Durandal, AngularJS, Meteor and many more. I’ve blogged about some … Continue reading Getting Started with AngularJS Video Series

The post Getting Started with AngularJS Video Series appeared first on Interface Technical Training.

]]>
AngularJS logo

As more and more functionality moves to the client developers are finding that writing JavaScript-centric applications from scratch can be challenging and frustrating. As a result, numerous JavaScript libraries and frameworks have sprouted up to help simplify the development process such as Knockout, Backbone, Ember, Durandal, AngularJS, Meteor and many more. I’ve blogged about some of these libraries and frameworks in the past if you’d like more information about some of the options out there and the benefits they offer:

One of my favorite frameworks currently available is AngularJS. I’ve spent a lot of time working with it, building different applications and also blogged about it from time to time. It’s a full-fledged Single Page Application (SPA) framework that provides data binding, MVC/MVVM style coding, routing, dependency injection, history management and much more.

I recently had the opportunity to give a talk about AngularJS at the AngleBrackets conference in Last Vegas and decided to convert the talk into a series of videos. The goal is to provide an overview of the framework in around 60 minutes. Over the next few weeks and months I’ll be posting the video tutorials that I created here. The outline of the course is shown next:

  • Introduction to AngularJS
  • Getting Started with AngularJS Features
  • Directives, Filters and Data Binding
  • Views, Controllers and Scope
  • Modules, Routes and Factories/Services
  • Customer Manager App Demo

In this post I’ll kick things off with the Introduction to AngularJS and Getting Started with AngularJS Features videos. Check back in the near future for additional videos that will follow.

 

Introduction to AngularJS

Getting Started with AngularJS Features

 

If you’re interested in learning more about AngularJS as well as several other libraries/frameworks such as jQuery, Knockout, Node.js, and Grunt check out the new JavaScript and SPA Programming with jQuery, Knockout, AngularJS, Node.js and Grunt course at Interface Technical Training.

Enjoy!
Dan Wahlin Microsoft MVP
.NET Developer Instructor
Interface Technical Training

The post Getting Started with AngularJS Video Series appeared first on Interface Technical Training.

]]>
https://www.interfacett.com/blogs/getting-started-with-angularjs-video-series/feed/ 3
Single Page Application (SPA) Frameworks https://www.interfacett.com/blogs/single-page-application-spa-frameworks/ https://www.interfacett.com/blogs/single-page-application-spa-frameworks/#comments Fri, 26 Apr 2013 15:54:50 +0000 http://www.interfacett.com/blogs/?p=?p=12541 One of the more popular acronyms being thrown around now days in the Web world is SPA which stands for Single Page Application. The acronym is a bit misleading since it infers that an application is built with only a single page. At first glance the concept sounds fairly useless since there aren’t many applications out … Continue reading Single Page Application (SPA) Frameworks

The post Single Page Application (SPA) Frameworks appeared first on Interface Technical Training.

]]>
One of the more popular acronyms being thrown around now days in the Web world is SPA which stands for Single Page Application. The acronym is a bit misleading since it infers that an application is built with only a single page. At first glance the concept sounds fairly useless since there aren’t many applications out there that only have a single page after all!

In reality, SPA means that an application has a single shell page that loads initially in the browser and that subsequent pages are loaded dynamically without leaving the initial shell page. As a result, the browser isn’t loading completely new pages but is instead replacing parts of the initial shell page as users interact with the application. This is accomplished using a combination of technologies such as Ajax, local storage, the history API, asynchronous module definition (AMD) and others and when done right leads to a reduction of bytes transferred between a client and server, a minimized number of HTTP calls and an overall better user experience that feels more fluid and responsive. SPA applications tend to feel more like traditional desktop applications since data can be accessed quickly without waiting for the browser to reload the entire page.

While the concept of SPA is great on the surface, building SPAs from scratch can be challenging and often result in an army of JavaScript files being used such as jQuery, Knockout, Q.js, Underscore.js, Sammy.js, Handlebars, Require.js and others. Building a SPA from scratch can certainly be done but having managed several development teams over the years I personally worry about the long-term maintenance issues that may crop up as script versions change and dependencies between scripts break. After all, JavaScript isn’t a compiled language so it’s not quite as simple to figure out when a new version of a script breaks an API or causes problems with other scripts (that’s where unit tests can play a big role).

So what do you do then if you want to build a SPA application? You can certainly build a SPA from scratch although I wouldn’t recommend it unless you consider yourself an expert in JavaScript and client-side technologies. If the application is being built and maintained by a team of developers then I’m even less compelled to recommend building a SPA from scratch given the different levels of technical knowledge typically found on a team and the struggles this can lead to down the road as an application is modified.  If you do decide to travel down the custom SPA road make sure you’re aware of the long-term implications of having a plethora of scripts in the application. If you want a more structured approach for building SPA applications then I highly recommend looking at some of the SPA frameworks that are available.

Here’s a quick look at some of the different SPA frameworks that I’ve been exploring recently and find compelling. Keep in mind that this is still a relatively new space and that new frameworks/libraries are coming out on a fairly frequent basis. Take the time to research the latest trends in this area.

AngularJS

AngularJS is becoming more and more popular when it comes to building SPA applications. The learning curve can be a little steep for some but once the concepts become familiar it provides an enormous amount of built-in functionality such as routing, controllers, views, navigation, dependency injection, event management, data binding, history management, and much more. AngularJS is an MVC type of framework that allows you to build views and bind them to controllers which makes it easy to maintain the application down the road. Because it has so many features built-in out of the box you don’t have to rely on the army of scripts I mentioned earlier which can simplify maintenance long-term. I’m currently working on an AngularJS application and like the power and flexibility that the framework offers for building SPAs.

Durandal

Durandal is an MVVM type of framework that also provides a rich set of features and a lot of flexibility for building SPA applications. It relies on a few core scripts such as jQuery, Knockout and Require.js for some of its features and provides navigation and screen management, modularity, history management, data binding, eventing, app bundling/optimization and a simple app lifecycle that makes it easy to get started with. By using Durandal you can significantly minimize the amount of code you have to write to build a SPA application because it handles the majority of SPA technologies for you.

Meteor 

Meteor provides a unique approach to building SPA applications and Web applications in general. It provides a framework that allows JavaScript code to be written on both the client and server. With this approach, data changes in the database or on the server are automatically pushed down to the client and bound into views without having to write polling or other Ajax-style code. It uses a mustache/handlerbars type of data binding approach (you’ll also find this in other frameworks) which is quite common when binding data to client-side templates. One of the key principles touted by Meteor is “data on the wire” meaning that only data is sent back and forth after the initial HTML content has been loaded. This allows the client to determine how to render the data and results in fluid and reactive applications in the browser and on mobile.

Conclusion

Additional libraries such as Backbone.js, Ember.js, Sammy.js (to name a few – I can’t list all of them here so sorry if your favorite isn’t mentioned) can be used to help build SPA applications as well. If you’re going to build a Single Page Application (SPA) then I highly recommend spending the necessary time to research the options out there and find a framework or library that fits well with your application’s requirements. As with any technology, there’s not one framework that fits every scenario so research is key.

We’re considering building a SPA course that’ll be offered at Interface Technical Training through our classroom and Remote Live offerings. If that’s of interest to you or your company let us know!

Enjoy!
Dan Wahlin Microsoft MVP
.NET Developer Instructor
Interface Technical Training

The post Single Page Application (SPA) Frameworks appeared first on Interface Technical Training.

]]>
https://www.interfacett.com/blogs/single-page-application-spa-frameworks/feed/ 1
Getting Started with Grunt a JavaScript task build tool https://www.interfacett.com/blogs/getting-started-with-grunt-a-javascript-task-build-tool/ https://www.interfacett.com/blogs/getting-started-with-grunt-a-javascript-task-build-tool/#comments Tue, 26 Mar 2013 15:57:20 +0000 http://www.interfacett.com/blogs/?p=?p=12112 JavaScript, JavaScript, JavaScript! It seems that just about everywhere you turn now days you here about “this framework” or “that library” being based on JavaScript. If you like JavaScript (and I do) then that’s a good thing but it also presents several unique challenges. Since JavaScript isn’t compiled it needs to be minified for production … Continue reading Getting Started with Grunt a JavaScript task build tool

The post Getting Started with Grunt a JavaScript task build tool appeared first on Interface Technical Training.

]]>
JavaScript, JavaScript, JavaScript! It seems that just about everywhere you turn now days you here about “this framework” or “that library” being based on JavaScript. If you like JavaScript (and I do) then that’s a good thing but it also presents several unique challenges. Since JavaScript isn’t compiled it needs to be minified for production applications, concatenated to reduce HTTP requests, linted (with a tool such as JSHint) to drive consistency and catch issues, and have a variety of other tasks/actions applied to it. That can be daunting and quite time consuming if you’re doing it by hand. Fortunately, there are several tools out there that can take away the pain and simplify your life. In this post I’m going to provide a quick introduction to a JavaScript task build tool called Grunt which is available at Grunt The JavaScript Task Runner.

Grunt provides a way to automate several tasks that you may need to run against one or more JavaScript files. Here’s a list of the built-in tasks supported by Grunt:

  • concat – Concatenate files together to reduce HTTP calls from a client to a server
  • init – Generate project scaffolding from a predefined template
  • lint – Validate files with JSHint
  • min – Minify files with UglifyJS
  • qunit – Run QUnit unit tests in a headless PhantomJS instance
  • server – Start a static web server
  • test – Run unit tests with nodeunit
  • watch – Run predefined tasks whenever watched files change

To get started with Grunt you’ll need to perform the following steps (note that these steps involve Node.js commands):

  1. Install Node.js
  2. Run the following at the command prompt to install Grunt globally using the Node Package Manager: npm install -g grunt
  3. To make the Grunt command line interface (CLI) available globally run the following command: npm install -g grunt-cli. Note that you may need to use sudo (for OSX, *nix, BSD) or run your command shell as Administrator (for Windows) to do this.
  4. Create a Gruntfile.js file that defines the tasks to run and place it in your project folder (the root folder containing your JavaScript files for example).
  5. Open a command prompt and navigate to the folder where the Gruntfile lives.
  6. Run grunt using the command line.
  7. Watch the magic happen!

You can see that Grunt relies on Node.js to do all of its heavy lifting. This makes it easy for Grunt to integrate multiple JavaScript tasks into its build process. In addition to the built-in tasks available with Grunt, hundreds of additional tasks can be accessed at GruntThe JavaScript Task Runner.

Now that you’ve seen what Grunt can do and how to get it installed, let’s walk through a simple Gruntfile.js file and explain the tasks that it contains and what they do.

Here’s a basic example of the shell code used in a Gruntfile:

module.exports = function(grunt) {
  grunt.initConfig({
     //Tasks defined here
  });
  
  // Default task
  grunt.registerTask(&#39;TaskName&#39;, &#39;Task1 Task2 Task3&#39;);
};

The initConfig() function is used to define the tasks that can be run as well as any settings that have to be defined. The registerTask() function is used to define a task name and the task or tasks that will be run when it’s executed. Here’s an example of a complete Gruntfile with 2 task definitions and one registered task named “default”:

module.exports = function(grunt) {
grunt.initConfig({
  concat: {
    dist: {
      src: ['src/intro.js', 'src/project.js', 'src/outro.js'],
      dest: &#39;dist/built.js&#39;
    }
  },
  min: {
    dist: {
      src: ['dist/built.js'],
      dest: &#39;dist/built.min.js&#39;
    }
  }
});  
  	// Default task
  	task.registerTask(&#39;default&#39;, &#39;concat min&#39;);
};

To execute the Gruntfile you can open a command prompt, navigate to the folder where the Gruntfile lives and type grunt. Once the grunt command is executed the default task will be run. Only 1 task is registered in this example named “default” but it will cause the concat and min task definitions to be run which will cause JavaScript files to be concatenated and then minified.

There’s much more that you can do with Grunt if you’re interested in automating your JavaScript build process. Check out Grunt JS Sample Gruntifile for additional examples of defining tasks in a Gruntfile.

Enjoy!
Dan Wahlin Microsoft MVP
.NET Developer Instructor
Interface Technical Training

The post Getting Started with Grunt a JavaScript task build tool appeared first on Interface Technical Training.

]]>
https://www.interfacett.com/blogs/getting-started-with-grunt-a-javascript-task-build-tool/feed/ 2