Wednesday, March 6, 2013

Conversion Operators Pros and Cons

C# enables programmers to declare conversions on classes so they can be converted to and/or from other types. These conversions can be explicit or implicit.

Below is an example from Microsoft:
class SampleClass
{
    public static explicit operator SampleClass(int i)
    {
        SampleClass temp = new SampleClass();
        // code to convert from int to SampleClass...

        return temp;
    }
}
Conversion operators are a great way of simplifying business logic, however they have some important limitations. One of these is that the operator may only have one parameter (the source type). This means that the only values available when the target type is initialized are those provided by the source type and dependency injection cannot be used. It's also worth noting that these conversions will not chain automatically. Declaring conversions from A->B and B->C does not provide a way to directly convert A->C.

This is all fine in theory, but what I find useful in these types of articles are real world examples, so here it is.

We're using Microsoft Service Bus for Windows to pass events between an e-commerce solution based on nopCommerce and a custom back office application. We needed some basic objects to pass relevant information through the bus and did not want to be too tightly coupled to the nopCommerce domain objects. My initial solution was to create classes and implement conversion operators to cast objects from nopCommerce to their POCO counterparts.

In the example below, you can see an implicit conversion operator for the Order class which uses the Shipment conversion operator.
public static implicit operator Order(Core.Domain.Orders.Order o)
{
  return new Order
  {
    OrderId = o.Id,
    Shipments = o.Shipments.Select(x => (Shipment)x)
  };
}
Since we needed to add more information to our Order object than was contained in the version from nopCommerce, I got rid of the conversion operator and created a separate conversion class with the following method:
public Order ToOrder(Core.Domain.Orders.Order o)
{
  return new Order
  {
    OrderId = o.Id,
    Shipments = o.Shipments.Select(ToShipment).ToList()
  };
}
The method contents are nearly identical, however we are now free to inject any dependencies we like in the conversion class constructor and add parameters to the conversion method.

No matter which approach you choose, these are excellent ways to simplify conversion and consolidate code used for creating related types. If you have any suggestions or feedback on this article, please leave a comment below or contact me directly.

Wednesday, February 13, 2013

Specified RunAs Account is Invalid

Windows Service Bus Configuration requires a "RunAs" service account which seems like it should be a fairly simple requirement. Unfortunately, if the configuration does not like the user you've entered it displays the generic error message, "Specified RunAs account is invalid." without providing any indication of the problem.

In my case, the service account was fine. The problem was that I logged in with a local administrator account and was trying to use a domain service account, which is not supported. Once I figured out the issue and logged in with a domain account the error went away.

Below are the supported combinations:

EnvironmentLogged In AccountRunAs AccountSupport
Domain
Domain
Domain
Supported
Domain
Domain
Local
Not supported.
Domain
Local
Domain
Not supported.
Domain
Local
Local
Either in development environment (1-box) or by using SQL Authentication.
Workgroup
Domain
Domain
Not supported.
Workgroup
Domain
Local
Not supported.
Not possible.
Workgroup
Local
Domain
Not supported.
Not possible.
Workgroup
Local
Local
Either in development environment (1-box) or by using SQL Authentication.

Hopefully this article will save someone else the headache of wondering why their service account is considered "invalid".

Thursday, February 7, 2013

Building E-commerce Sites with MVC

For those of us who have spent most of our careers building business applications, the prospect of creating an e-commerce storefront can be quite daunting. Not only do you have to deal with calculating tax, estimating shipping and accepting payments, but employees need a way to manage products, discounts and marketing content. Thankfully, there's an open source e-commerce solution written in Microsoft ASP.NET MVC named nopCommerce that provides all this functionality and much more.

I was so impressed by both the functionality and the underlying architecture that I've created a presentation which I believe will serve as a great introduction to MVC as well as a tutorial on developing custom storefronts using nopCommerce.

We start off with a brief overview of basic MVC concepts accompanied by examples within the nopCommerce code. This includes strongly typed Razor views, HTML helpers, jQuery ajax calls, and fluent validation.

Once everyone understands how the website works, we dive into the infrastructure by introducing WCF services, using a generic repository with Entity Framework (EF4) and LINQ, as well as Dependency Injection (DI) with Automapper.

If you'd like to schedule this talk for your local user group, either request me through INETA or contact me directly.

Wednesday, December 19, 2012

DotNetDude joins Synergy Systems

I'm pleased to announce that DotNetDude Development and Synergy Systems have joined forces with big plans for 2013.

Synergy Systems has established itself as a leader in recruiting by focusing on the most sought-after talent with a critical eye towards both technical and soft skills. The folks at Synergy pride themselves in establishing relationships with the upper echelon of IT talent as well as the most desirable companies.

DotNetDude Development has invested heavily in the IT community and Synergy Systems is helping to continue this push in the new year by sponsoring appearances at technical conferences. Be on the lookout for me at CodeMash in January!

Thursday, November 22, 2012

Microsoft Fakes Bait and Switch?

Is it possible Microsoft tricked developers into adopting their code isolation framework in an attempt to force upgrades to the Ultimate version of Visual Studio 2012?

When I first learned about the Moles framework from Microsoft Research, I was excited at the possibilities for unit testing and began presenting it at user groups and conferences. I was even more excited after Microsoft announced that a new version of this framework, named Fakes, would be included with Visual Studio 11 (now 2012).

Microsoft's documentation originally indicated that Fakes would be included with both Premium and Ultimate, but early adopters noticed the VS2012 Premium release candidate did not include Fakes and the documentation was later updated to indicate it would only ship with Ultimate.

Meanwhile, Moles is no longer supported in VS2012. So where does this leave those who adopted Moles and use the Premium version of Visual Studio? It appears they will be forced to upgrade to Ultimate or abandon the use of Microsoft's code isolation framework.

I choose to give Microsoft the benefit of the doubt. I'm sure the original intention was to ship Fakes with the same versions as were previously compatible with Moles. Discontinuing Moles made sense when a new-and-improved framework was slated to take it's place. The decision to restrict the use of Fakes to Ultimate was surely made without considering the ramifications to their existing customers.

The question remains of what, if anything, Microsoft will do to address the needs of those customers that have been left out in the cold.

Tuesday, October 23, 2012

Three Tips for Recruiters

The longer I work in the software development community, the more recognition I get from recruiters as someone who can help them find candidates and the more colleagues that approach me asking for career advice. I love helping good people on both sides of the equation, but there are a few things I expect from recruiters.

Be Candid

When I speak with a recruiter who is unwilling to share information about the client for fear they will be circumvented or will not provide a general range for the salary/rate, it says to me that they do not trust their own value and consider the candidate an adversary.

Experienced recruiters bring more to the table than a job description. They, or someone they work with, have a relationship with the client company and a reputation of bringing them solid candidates. A resume presented by such a recruiter may be given more credibility and often goes to the top of the interview list.

In my experience, providing a general range of possible compensation for a position is a win-win. It gives a potential candidate the information they need to decide if the opportunity is worth pursuing and keeps from wasting anyone's time.

Avoid Random Referral Solicitation

I can't count how many times some random recruiter has asked me to help them expand their network by recommending them to my colleagues or, even worse, asked for me to give them contact information for possible candidates. There is a short list of recruiters which I provide when someone I know is looking for a gig and these are people who I've known for years and have a solid reputation for taking care of their candidates. It baffles me that someone would expect me to recommend them when I cannot vouch for their character and abilities.

That said, I encourage recruiters to email me job openings that fit my niche as I may come across someone who would be a good fit. While I appreciate having these on hand in case a colleague expresses a need, I do not actively seek candidates for these positions unless there is significant motivation for me to do so. Motivation comes in many forms, but the most straightforward is a referral fee. If a recruiter who I do not have a relationship with expects someone else to spend time and energy helping them fill a position, they should expect to offer compensation in the same way that I would share part of my billable rate should I need help from another software developer who is not getting paid for the work.

Get Involved

Having served as Treasurer, Vice President, President and now Board Member for the Nashville .NET User Group, I often encourage others to drop by and find a way to participate in the community. It can be an important tool for software developers, but is absolutely vital for a recruiter.

Imagine taking all the juiciest most healthy fish in a lake and putting them in a swimming pool. Wouldn't that be a great opportunity for a fisherman to make a great catch? User group meetings are where the best and brightest gather and recruiters are welcome at most of them, so long as they respect the meeting and are not overbearing. Even if the recruiter doesn't speak to anyone about opportunities during the meeting, they have a connection to reference next time they call.

Attending community events is not just about networking. The bulk of a technical presentation might be over a recruiter's head, but over time they can pick up meaning behind the buzz words and witness individual and group reactions. Understanding these reactions can help identify the right candidates for a position and what motivations particular candidates might respond to.

There's a wealth of non-technical education that takes place as well. By watching how people interact one can learn quickly who people in the community respect and how they interact. Seeing how someone interacts with their peers can be a great indicator for how they might fit into a team.

Chances are, any recruiters who are taking the time to read this article are already on the right path. Hopefully these tips will help provide direction to someone I will, one day, have the pleasure of working with.

If you have any additional tips or thoughts on this topic, please feel free to leave a comment below.