Sunday, September 30, 2012

Session Spotlight - IEnumerable, ISaveable, IDontGetIt: Understanding .NET Interfaces

I'll be speaking at So Cal Code Camp (Los Angeles) on October 13 & 14. If you're not signed up yet, head over to the website to let them know you're coming:

Back again: IEnumerable, ISaveable, IDontGetIt: Understanding .NET Interfaces.

Abstraction Through Interfaces
When people talk about interfaces, they often refer to the User Interface -- the screens and controls that allow the user to interact with the application.  But "interface" also refers to a very powerful abstraction that lets us add extensibility and loose coupling to our code.

My first encounter with interfaces was in Delphi (Object Pascal) as a junior developer.  I understood what they were from a technical standpoint, but I didn't understand why I would want to use them.  We went to the Borland conference every year; and as a new developer, I took the opportunity to absorb as much information as I could, even if I didn't understand most of it (this is also a great way to use Code Camp -- to experience technologies you haven't taken the time to look into yet).  I was very excited because there was a session in Interfaces at the conference.

"Great," I thought, "I'll go and get some practical examples of how to use interfaces and find out why I would want to use them."  So, I get to the session, sit down, and grab my notepad -- ready to spend the next hour getting a practical introduction.

The speaker gets up and starts the presentation.  "So, let's say that we have an Foo class.  And we also have an IBar interface."

"Noooooooooooooooo!" I screamed (well, screamed inwardly anyway).  "I need practical examples.  You can't use Foo / Bar / Baz examples."  But that's the way it was, and I didn't get anything new out of the session.  (I also talked to some of my senior developer coworkers who attended, and they didn't get anything out of it either.)

It was several more years before I had a good grounding in object oriented design and the hows and whys of abstraction.  The goal of "IEnumerable, ISaveable, IDontGetIt" is to give you a jump-start to understanding interfaces.  We use real examples from the .NET framework and from actual application abstractions that I have coded in my professional career.

In addition to the So Cal Code Camp on Oct 13/14, I'll also be presenting this session at the Desert Code Camp (Phoenix) on Nov 17.  And if you're quick, you can also see me present this information this week at a couple of user groups.

Hope to see you at an upcoming event.

Happy Coding!

Tuesday, September 25, 2012

Upcoming Speaking Engagements - October 2012

October is shaping up to be a busy month.  I have several speaking engagements which will be a lot of fun.  Code Camps and User Groups are a great place to get out and talk to other developers -- find out what works and what doesn't work, and learn a lot of great new techniques.

Tuesday, October 2, 2012
LA C# User Group - Manhattan Beach, CA
Topic: IEnumerable, ISaveable, IDontGetIt: Understanding .NET Interfaces

Wednesday, October 3, 2012
So Cal .NET - Buena Park, CA
Topic: IEnumerable, ISaveable, IDontGetIt: Understanding .NET Interfaces

Saturday / Sunday, October 6 / 7, 2012
Silicon Valley Code Camp - Los Altos Hills, CA
Get Func<>-y: Delegates in .NET
Learn to Love Lambdas

Saturday / Sunday, October 13 / 14, 2012
SoCal Code Camp - Los Angeles, CA
Dependency Injection: A Practical Introduction
IEnumerable, ISaveable, IDontGetIt: Understanding .NET Interfaces
Learn to Love Lambdas
T, Earl Grey, Hot: Generics in .NET

Stop by if you can; the more, the merrier.

Happy Coding!

Tuesday, September 18, 2012

Session Spotlight - Learn to Love Lambdas

I'll be speaking at SoCal Code Camp (Los Angeles) on October 13 & 14. If you're not signed up yet, head over to the website and let them know that you're coming:

Back by popular demand: Learn to Love Lambdas

Lambda Expressions
Lambda expressions are a very powerful tool that can add elegance to our code.  The problem is that they look like a secret code the first time you see them.  You've probably come across something that looks like this:

It looks daunting, but it's fairly easy to learn.  The lambda expression that we have here is just an anonymous delegate.  If we were to use a more verbose syntax, we get something like this:

We are probably a bit more comfortable with this syntax.  Here, we can tell that we're hooking up some sort of event handler, and it has standard event handler arguments: object and some type of EventArgs.  Inside the method body, we are assigning the Result property of the EventArgs to display in a list box.

This anonymous delegate is equivalent to the lambda expression above.  To create the lambda expression, we just replace the "delegate" keyword (before the parameters) with the "goes to" operator (=>) after the parameters.  The parameter types are optional, so we can get rid of those.  Also, since the method body only has one statement, we can remove the curly braces.  This leaves us with a compact and readable syntax.

Delegates abound in the .NET framework (and in add-on frameworks like the Prism library).  Whenever we have LINQ extension methods, event handlers, callbacks, or tasks, we can use lambda expressions to add compact blocks of code right where they are most useful.  And in some instances (like when working with the Prism variants of RaisePropertyChanged) we can make our code more conducive to refactoring by replacing strings with compiler-checked pieces of code.

Once you get used to lambda expressions, they are extremely readable.  In this session, we'll learn the lambda syntax, use lambdas for asynchronous callbacks, see a really cool feature that makes code safer, and see how lambdas were designed to be used extensively in LINQ.  If you'd like to Learn to Love Lambdas, be sure to stop by my session at a code camp near you.

In addition to the SoCal Code Camp on Oct 13/14, I'll also be presenting this session at the Silicon Valley Code Camp on Oct 6/7 (this session is scheduled for 2:45 p.m. Sunday afternoon -- last session of the day).

Also be sure to check out the walkthrough and code samples here: Jeremy Bytes.

Hope to see you at an upcoming event.

Happy Coding!

Wednesday, September 12, 2012

Session Spotlight - T, Earl Grey, Hot: Generics in .NET

I'll be speaking at SoCal Code Camp (Los Angeles) on October 13 & 14.  If you're not signed up yet, head over to the website and let them know that you're coming:

Also, I've got a brand new session: T, Earl Grey, Hot: Generics in .NET

Generics in .NET
Most C# developers have worked with generics to a certain extent (often by using classes from the base class library like List<T>).  But we don't have to stop by merely consuming classes with generics; we can also create our own classes and methods that take advantage of this great framework feature.

We got generics way back in .NET 2.0, and they really were transformational at the time (now we're hopefully used to seeing them).  Using generics, we can add type-safety to our code while still maintaining extensibility.  By making our code type-safe, we are more likely to catch errors at compile time and also avoid strange behavior that might come from casting objects to different types.  Our code also becomes more extensible -- it can work with types that it may not have been originally intended to work with.

Generics also offer us some performance enhancements (which, granted, seem fairly minor when talking about the processing resources of modern devices) by reducing casting calls and boxing/unboxing of value types.

In the session "T, Earl Grey, Hot: Generics in .NET", we'll take a look at the basic features of generics (by comparing non-generic and generic versions of classes from the base class library), see how we can incorporate generics into our own classes and methods in a practical way, and explore the various options to take full advantage of generics in our code (like "default" and generic constraints).

If you can't make it out to the SoCal Code Camp, you have another chance to see this session at the Desert Code Camp (in the Phoenix, AZ area) in November.

Hope to see you at an upcoming event.

Happy Coding!

Monday, September 3, 2012

Book Review: Dependency Injection in .NET

I just finished reading Dependency Injection in .NET by Mark Seemann (Amazon link).  This is an excellent description of Dependency Injection (DI), the associated patterns, and several of the main DI frameworks.  Seemann has pulled together a wide range of resources (books, magazine articles, blog posts) and created a comprehensive work.  It is apparent that this is based on a ton of research and personal experience in the DI world.

This turned out to be a good and timely read for me.  A couple months ago, I started working on a WPF project using Prism, and Dependency Injection plays a big role in that.  A colleague of mine (and former co-worker) highly recommended this book, and so I picked up a copy.  (Disclaimer: I am a "book person"; that is how I learn best.  Other people have different learning methods, so feel free to take this from a "book person" perspective.)

Part 1
Part 1 is an overview of Dependency Injection.  Seemann describes what DI is, and also dispels some misconceptions about DI.  He covers the benefits of DI including late binding, extensibility, parallel development, maintainability, and testability.  He also lays a foundation by showing good and bad examples of implementing DI and how each impacts achieving the desired benefits.  Finally, he introduces the topics that will be used in the rest of the book, including the design patterns, DI features, and the DI containers that are available.

Part 2
Part 2 covers the patterns and anti-patterns in DI.  The patterns include Constructor Injection, Property Injection, Method Injection, and Ambient Context.  Seemann does a good job of covering these patterns including both the benefits and the drawbacks of each.  It becomes apparent very quickly that he favors Constructor Injection wherever possible and that the other patterns should be used only when Constructor Injection is not possible.  (As a side note: I came to this same opinion in my fairly-short time working with DI.)

The anti-patterns include Control Freak, Bastard Injection, Constrained Construction, and Service Locator. Of these, the Service Locator is the most controversial.  Many people consider Service Locator to be a valid DI pattern (rather than an anti-pattern).  Seemann himself admits that he promoted the use of Service Locator for many years. He eventually came to the point where the shortcomings of the pattern outweighed the benefits.  Prism (the Microsoft Patterns and Practices application guidance) has a service locator baked in (to reference either Unity or MEF for DI out-of-the-box).  In my experience with Prism, we have used the service locator pattern, and I can see the benefits and shortcomings.  At this point in the project, the scale leans towards the benefits, and we are willing to work with the drawbacks.

Part 2 also has a chapter on "DI Refactorings".  This is a very practical review of what types of DI situations you'll run across in the real world -- like dealing with cyclic dependencies and short-lived dependencies.  These are great topics to cover.  Because the DI containers that we normally work with add the layer of abstraction, sometimes we forget about some potential issues (by assuming that the container is dealing with it).

Part 3
Part 3 covers some big DI topics: Object Composition, Object Lifetime, and Interception.  Object Composition has to do with how we use DI to compose our objects.  Seemann shows examples for how to compose objects with various technologies -- from easy implementations with console and WPF applications, to difficult implementations with ASP.NET applications and PowerShell commandlets.

Object Lifetime has to do with how the dependencies themselves are managed.  Do you always return the same instance of a dependency no matter who asks for it (Singleton)?  Or do you return a new instance of a dependency each time (Transient)?  Or do something inbetween?  Seemann covers the various lifetimes and the pros and cons for each.  As with everything, we need to consider our particular situation and select the right answer for the task at hand.

Finally, Seemann covers Interception.  This is the idea of using the DI container to inject cross-cutting concerns into our application.  For example, we could have the container inject logging or error handling into each of our dependencies.  This was a very interesting topic to read about.  He also covers Aspect Oriented Programming (AOP) and compares/contrasts it with using DI to cover the same concerns.

Part 4
Part 4 covers several DI containers.  What I really liked about this section is that Seemann uses the same examples in each chapter.  This means that instead of focusing on the example itself, we can focus on the differences in the container implementations.  The containers covered are not exhaustive (5 containers), but they are some of the most widely-used ones.

The containers include Castle Windsor, StructureMap, Spring.NET, Autofac, and Unity.  Using each container, examples cover configuration, managing lifetime, interception, and how working with difficult APIs (the difficult APIs refers to the dependencies that we are registering/resolving, not the container APIs themselves).  Not all containers support all features out-of-the-box, and Seemann shows some possible work-arounds for the features that are not implemented directly by the container.  The end result is that most of the containers work pretty much the same (which is good), but there are slight variations.  The container you select can be based on personal preference or particular needs (for example Spring.NET is actually a much larger framework that includes DI functionality; you may want to use Spring.NET for those other features or because you also use Spring in the Java world).

Lastly, Part 4 covers MEF (Managed Extensibility Framework).  While MEF is not a DI container, it is often used for DI purposes.  (This is true of the Prism framework -- it supports using MEF as a DI container.)  Seemann shows that while MEF can be used for DI, it probably should not be.  Again, this is a controversial topic: I have seen blog articles where people moved from Unity to MEF for DI and see no reason why anyone would want to start a new project with Unity.  Seemann lays out some very good points regarding how DI is implemented using MEF and how it varies greatly from the other containers.

Where Dependency Injection in .NET really shines is in its use of external references.  Many sections point to books, magazine articles or blog articles where you can research topics further.  Seemann has gone to a lot of work to collect these resources together in one place.  I will be spending a lot of time going through the links collected at the back of the book.  On a good note (personally), I found that several of the books mentioned are already in my collection.

Wrap Up
Dependency Injection in .NET is an excellent resource.  There are not very many DI books on the market, and it is great to see that this particular book is so well executed.  I would recommend this to any developer who is interested in improving his/her use of Dependency Injection.

Happy Coding!