Lorem ipsum dolor sit amet, consectetuer adipiscing.

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.

Microsoft .NET

Why abstraction can be detrimental.

The other day someone asked a question about the relation between the Socket class, and the UdpClient class. Specifically, what is the relation? While the answer is simple because UdpClient is merely wrapper around a UDP initialized socket, this also lead me to thinking more about abstraction. So what is abstraction? Take the following code […]

More
Microsoft .NET

Custom Method Names via Attribute Routing in ASP.NET Web API 2

I am using Web API 2 in my ASP.NET MVC 5 project, and I found myself needing custom routes very quickly. In my scenario I’m building a single-page application using AngularJS, so all the data is received and sent to and from web api’s from ajax calls.

In my scenario I have a collection projects, and each project has a collection of releases; ultimately my web api routing and controller looked like the following.

config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });
config.Routes.MapHttpRoute("ActionApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
[RoutePrefix("api/Projects")]
public class ProjectsController : ApiController
{
    public ProjectDTO[] Get()
    {
        ...
    }

    public ProjectDTO Get(int id)
    {
        ...
    }

    public HttpResponseMessage Post(Project project)
    {
        ...
    }

    public void Put(Project project)
    {
        ...
    }

    public void Delete(int id)
    {
        ...
    }
}

This is the default, or at least, most common method of using web api based on the HTTP verbs used when calling the service. While this is great for the most basic CRUD operations, I needed to facilitate relational data and have custom named methods. There are many ways to accomplish custom method names gracefully, each has their own pros and cons, but ultimately I’m just going to cut to the chase and demonstrate attribute routing, which I found to be the easiest, and most useful. I spent several days researching ways to do this, and no matter what I did I ran into some limitation, funky issue, or an approach that required custom routes or additional code, or even uninstalling WebDAV or re-configuring your web.config file – sigh. To say the least, I felt they were all terrible solutions.

Finally, I came across this article.
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

In short, it saved my sanity. I was able to add a web api as simple as this.

[HttpGet]
[Route("{id}/Releases")]
public ReleaseDTO[] Releases(int id)
{
    ...
}

No changes to my routing. No changes to my web.config file. No uninstalling WebDAV. No weird limitations. What this also gave me was the control over the routes that I wanted so I could have a consistent url scheme that made sense, since the data is relational. The url scheme would be ‘/api/projects/{projectId}/releases’, which makes complete hierarchical sense.

After all the research I did, this would be the approach I would recommend when you need to expand your web api past the basic HTTP verb support. I tossed the idea of using a lot of separate controllers, but felt it would be a maintenance nightmare, and a bit silly to have too many, albeit small controllers when I could logically group things a bit better.

Learning MVVM from the ground up for building real-world software

My two MVVM video series` on YouTube have become very popular, and I have every intention of continuing getting more information out there on MVVM through my Enterprise series. If you haven’t had a chance to check out the videos, here is the full list below.

MVVM Session 1 – http://youtu.be/EpGvqVtSYjs
MVVM Session 2 – http://youtu.be/Dzv8CtUCchY
MVVM Session 3 – http://youtu.be/OOHDie8BdGI

Enterprise MVVM Session 1 – http://youtu.be/lool8Ut58Xw
Enterprise MVVM Session 2 – http://youtu.be/CpVNMmefvDo
Enterprise MVVM Session 3 – http://youtu.be/RbfqNOSyYok
Enterprise MVVM Session 4 – http://youtu.be/7SwgqLJCLI8

MVVM Questions and Answers – http://youtu.be/j8Fke2cUTTc

I want to thank everyone who has followed my series and provided feedback. It has emboldened me to ensure I provide quality content and continue the effort of producing videos. MVVM in of itself is a simple concept, but a lot of questions stem from people trying to build real-world software, and finding material for that can be difficult. From the basic stock ticker demos to simple CRUD applications and Msdn articles, none of these really actually demonstrate a full stack MVVM application using dependency injection, service locater’s, messengers, unit testing, mocking; basically the full stack. This is what I intend to do through the enterprise MVVM series. Maybe I will write a book at some point. Who knows. Right now though, I’m enjoying producing the video content.

I also just ordered new sound equipement, the RODE PodCaster microphone, as my Blue Yeti one sort of, well, fizzled out I guess. Bummer, but looking forward to the new one already. Anyhoot, check out the videos if you haven’t already. And again, thank you to my followers!!

Why careers trap entrepranuers and why risk is necessary.

In my heart I always knew what I wanted to do – write code. Build software. Do big things. I have been lucky enough to do all of this. I have worked a normal nine to five job, I have done contracting, I have had a business, and now I have what I would consider a nice, comfortable, complacent career with a one terrible thing. A limited ceiling of success. I’ll begin by giving you context based on my own experience, and you will be able to extrapolate the rest on your own.

It’s hard to say when the exact time was that the transition happened. Life goes by quickly and especially if you do not keep a constant vision for yourself and your future, but alas the transition did happen for me, and it wasn’t a good one. Before I say anything else, I think careers are great and they have their place, but you must first differentiate between a career you create for yourself through entrepreneurial venture, and a career that you just fall into, which is what I consider to be a trap.

For me the transition happened when I took a big risk initially – I quit my normal nine to five day job to begin contracting software projects, which went well for a while. It allowed me to build my product which was going to pave my way to success. I had my eyes planted: I was going to make millions, make the next big thing, start a company, have employees, and thrive in the glory of it all. Reality is a kicker though, when you soon realize that you’re going to fail and while you can do something about it, failure is still inevitable. It was convenient when I was offered a job at a software company here in town, at a time when my venture failed and I was out of money. So convenient, that I gracefully and thankfully took it. You should always take advantage of opportunity, and I did, in a time of need, and it was the right thing to do. What I didn’t realize is what was about to happen started me on a long road, the wrong long road.

Two years later, here I am in what I now consider career. I have been promoted numerous times, treated with a pretty great salary for my age, and have brought many good things to the company I work for. Things fade. Eventually, all things; wither. We live in a world were everything after a period of time loses its luster and becomes null and void, empty and without much meaning, and careers often are just that. Mindless jobs where you are a drone making some other person much richer, sacrificing your dreams and all that you strive for. This my friends, is the trap.

Careers are great traps, and people don’t realize it. They are often comfortable too, which is where complacency settles in. You make a good salary, you have a big-cheese title that looks great on paper, you might even say that everything is going well for you. The problem though: you’re an entrepreneur, and you have just realized you’re stuck, and that your life actually sucks right now. The transition kind of happened like this, and you might relate.

  • You started your career doing exactly what you love to do
  • You have been promoted one or more times
  • You have a great salary
  • You have done great work

I talk about the “transition”, which happens when you least expect it to, and you don’t even realize it until after it is written in blood on stone. Those who realized what I have know exactly what I mean, and those who don’t you just read this next bulleted list, and hopefully you will realize that you need to do something about it.

  • You are no longer actively doing what you love to do.
  • You no longer get big promotions; you now get the normal yearly 1-3% raise.
  • You are no longer doing great work, but are just busy.
  • You no longer are passionate.
  • Your career also blends into your personal life

These are very small bulleted points, but important ones. You may actually realize that you still do what you love to do for example, but what if you also realize that it might only be five percent of your time instead of the majority? What about realizing that you have reached your salary limitations and will spend the next  ten  years trying to make more with little success, while prices of things continue to rise? What about realizing that the great work that allowed you to be where you are today is no longer a part of your life; you just don’t do great work anymore, not because you can’t, but because your just busy, often with office politics, meetings, or general busy work. You may have also realized that  you don’t find yourself thinking about, or being passionate about, what you used to love so dearly, whatever that may be, often because you are tired when you get home now, and you just don’t care, or have the energy. You may also realize that to try and get your work done it begins to creep into your personal life, where your career and personal life become one in the same – and you find yourself without time to dedicate to your dreams.

This is the trap, and why it is so detrimental to entrepreneurial personalities. You’re now limited and on a road that over the next ten years might gain you a ten thousand dollar increased salary – while prices of things and general living expense creeps up about the same, leaving you exactly where you were originally. The only difference is that you just spent ten years of your life doing exactly the opposite of what you want to do, but it took you until just now to realize that, and it might be ten years too late.

All of this is very different from why you might have fallen into the trap, because everyone’s reasons or life experiences are different, and that will ultimately be its own blog post. At least for now you can realize where you are at currently, and begin to think about what you might do about it. If I was to leave off with one very important piece of advice,  it would be this.

You cannot be a entrepreneur and have a successful career. To be one, means you are not the other, and to be an entrepreneur means to risk a career in favor of the potential success you can make for yourself, because when your entrepreneurial venture succeeds, it is at that time your career will fail.

Enterprise MVVM in WPF: Building ActionCommand and using it, the DAL, and BAL in your ViewModel

New video continuing the enterprise MVVM series.

WPF: How to Template, Style, and Animate a WPF Button Control

Having vacation time has been nice because it has put me on a roll with pumping out some video content for you guys. Another video I did on re-templating, styling, and animating a WPF button control. We will do other controls later on.

Source code: http://sdrv.ms/1eqrx2U

Enterprise MVVM in WPF: Adding Data and Business Layers using Entity Framework

Second video in the Enterprise MVVM in WPF series where I demonstrate the practice of adding a data layer and business layer using Entity Framework and test driven development (TDD). Learn how to make a unit of work business context that you can use to encapsulate your business rules and logic, but still be able to go a layer down to entity framework.

Grab the source code here, and the slide deck here. Don’t forget to like and subscribe if these videos helped you. :)

Series:
Part 1: https://www.youtube.com/watch?v=lool8Ut58Xw&hd=1
Part 2 (this video): https://www.youtube.com/watch?v=CpVNMmefvDo&hd=1

Enterprise MVVM in WPF: ViewModel Validation using Data Annotations

I finally had some time off from my busy work life to produce a few WPF MVVM videos on my YouTube channel. It is a bit of a relief to finally get some content up for you guys again. Prior, I believe the last video I had done was a bit over a year ago. The video is the irst video in a new series on what I consider to be enterprise MVVM development for WPF. We will begin covering practical application development scenarios that you will encounter in the real-world, and focus on building high quality, low maintenance, enterprise solutions.

Video:

You can grab the demo code here, and the slide deck here. Like and Subscribe for more videos!

Series:
Part 1 (this video): https://www.youtube.com/watch?v=lool8Ut58Xw&hd=1
Part 2 :https://www.youtube.com/watch?v=CpVNMmefvDo&hd=1

Video Notes:
– I apologize for the low audio, the new microphone (Blue Yeti) picks up pretty much all background noise, and my desktop PC is very loud. I will be looking into ways to resolve this, and if anyone has recommendations please let me know.
– When I originally created the projects I had Visual Studio set to .NET 4. If you are using Visual Studio 2012 or 2013, make sure you set your projects to target .NET 4.5 or .NET 4.5.1.
– There are two parts where the audio may seem like it was edited, and this was true. Sometimes noise from other people makes it into a video and I do my best to edit it out without ruining the video content.
– The downloadable source code will be much cleaner than what was demonstrated in the video. I will be focusing more on the approaches in my videos, and cleanup can always come later.
– The downloadable source code will also contain many more unit tests, including some unit tests for the view models that we were not able to do in the screencast originally.

Thank you everyone for all your support! I have been listening to the questions and feedback in the comments, and paying attention so that I will be focusing my attention on the questions and topics you guys are asking for.

Introduction to Test Driven Development in .NET

Testing has always interested me to some degree. Not from the perspective of manual testing or quality assurance, but from the perspective of the developer behind the desk who just wrote the code. How can a developer, who is accountable for their own code, ensure that they are producing the highest quality code with a low defect count and high maintainability rating? One answer for many years now has been the development process of TDD, or Test Driven Development.

TDD is not just about unit tests; it’s actually an entire thought process with a set of core fundamentals you should follow, and it really changes the way you think about your code, and eventually the way you write it.

I did a thirty minute screencast on TDD in an introductory manner. I go through the steps of designing and building a simplified inversion of control container through the process of TDD.

Video
Watch on YouTube now.

Source Code download:
Download from my Skydrive

Powerpoint Slides
Download from my Skydrive

MVVM Session 03 – Model Validation with IDataErrorInfo

In the third part of my MVVM Series on my YouTube channel I educate how you can use the IDataErrorInfo interface in WPF using MVVM to do validation. This approach applies equally to your models and view models, although in the video I focus primarily on the model.

Video
Watch on YouTube now.

Source Code download for Session 03:
Download from my Skydrive

Verbatim Identifiers and verbatim string literals in C#

Browsing through some of my old answers on StackOverflow, I came across this question I had answered regarding verbatim identifiers in the C# language. I want to take a moment to talk about this mysterious language feature, because it actually has multiple usages, one which is quite common, and you just might not be aware you are using it.

To start, I’ll describe to you what a verbatim identifier actually is by citing the C# language specification.

The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

This text comes from section 2.4.2 of the language specification. It is also interesting to note that this language feature has been available since the first version of C#, specifically version 1.0 and 1.2, but it is not a feature that you would normally seek out as you learn a language. Good programmers generally try to find better names for variables than ‘object’ or ‘string’, especially since they conflict with existing type identifiers.

A valid usage of this language feature would be the following.

public string ToString(object @object)
{
    string @string = @object as string;

    if (@string != null)
        return @string;

    throw new ArgumentException("@object is not of type System.String.");
}

This is a terrible piece of code, but it does demonstrate the usage of the language feature correctly. But the language feature itself is a bit more flexible than that, but I don’t believe by design. We would of course have to ask the language designers, but let’s look at the code from the question I answered on StackOverflow.

_scale_id_regex = @ConfigurationSettings.AppSettings["some_setting"];

The gentlemen asked what the purpose of this symbol was in this piece of code. What I described earlier is pretty much the extent of what is outlined by the language specification, but the short answer is that it does not serve any real purpose. It is simply allowed by the language rules, and thus allowed by the compiler implementation. You can look at it a bit deeper however. Almost everything in code that has a name is considered an identifier, in this case ConfigurationSettings is the identifier, and verbatim identifiers are allowed by the language rules.

You’re hopefully wondering by now what this has to do with string literals, aha! String literals. A string literal in C# is identified by the usage of @ at the beginning of zero or more characters enclosed in double quotes, as in @"The fox jumped over the hole.". Again, we should reference the C# language specification to describe a string literal.

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escaped-sequence. In particular, simple escape sequences, and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

We never really think about verbatim identifiers, and we don’t think about string literals as verbatim string literals as programmers. We just think of string literals, because that is the common terminology, but the usage of the @ symbol to specify something as verbatim dates back to C# 1.0 and 1.2 with verbatim identifiers and string literals. The language feature is still very much alive into C# 5.0 and while verbatim identifier usage is very rare (and should be), verbatim string literals are a daily routine for programmers.