Taswar Bhatti
The synonyms of software simplicity
automapper

In Part 5, we will use a powerful feature that automapper allows us, which is CustomResolver.

Lets look at our Domain Object which contains a boolean field called ShipToHomeAddress, what we will do is map those boolean to a string value with “Yes” or “No”.

For our mapping code we will introduce CustomResolver class which inherits from ValueResolver and would map the boolean field to string

Now our mapping code would look like

The end result of the view looks like

In Part6, I will show how to use a CustomerFormatter with a Resolver to format the data in a certain way.

Previous Post:
Part 1 (NullSubsitution)
Part 2 (Flattening by Convention)
Part 3 (Nested Mapping)
Part 4 (Projection)

automapper

In Part 4 we will see how we can use projection to map inner objects for example a DateTime field.

Again our domain and Dto (Data Transfer Object) looks like

We will map the PurchaseDate in the Domain Object to our Dto PurchaseHour and Purchase Minute.

As you can see the View now contains the flatten Hour and Minute, from our previous posting we also see that the other way of nested projection/flattening is by CustomerName.

Here is how our view would display

In part 5, I will show how to use CustomerResolver to resolve and map values to different types.

Previous Post:
Part 1 (NullSubsitution)
Part 2 (Flattening by Convention)
Part 3 (Nested Mapping)

automapper

In this part we learn about Nested Mapping in Automapper and we will use the same OrderDto Object that we had previously but we will let Automapper to map the inner objects.

Once again here is the Domain Objects First

The Dto (Data Transfer Object) look like

This time when we map Order to OrderDto we will also provide the mapping of OrderItems to OrderItemsDto.

As you can see I have taught automapper about its inner child/object mapping before calling Map, thus it knows about the inner mapping, our result would show like

In the next part I will be showing projection in Automapper.

Previous Post:
Part 1 NullSubsitution
Part 2 Flattening by Convention

automapper

In this part I will be showing how Automapper uses flattening by convention.

If we look at our OrderDto we will see that it has a IEnumerable of OrderItemsDto and also something called OrderNumber. This is the type of object we want to send to our presentation layer.

Compare that to our domain object where it has a GetTotal method, OrderNo and a Customer Object

In Automapper there is a convention where it sees the method has a Get in front it. It will then auto flatten it by convention. See how in the OrderDto I have Total as a property and also CustomerName.

Our mapping code would look something like this where we are mapping OrderNo to OrderNumber

With this mapping will not get the inner mapping of OrderItems since we have told Automapper to Ignore those guys don’t map the OrderItems, as it stands OrderItemsDto would be null

This would produce a view looking like

As you can see Automapper automatically has pulled out GetTotal and populated Total Property and so is Customer object with GetName method with CustomerName. By using these simple conventions it automatically does the work for you without explicitly specifying it.

In next part we will talk about Nested mapping.

Previous Post:
Part 1 Null Subsitution

automapper

Recently I gave a talk on Automapper in Ottawa .Net Community, so I though I would post the main ideas of the presentation here also.

The demo was based on an Order System where there were just 3 tables/classes, “Order”, “Customer” and “OrderItems”

Object diagram

From the diagram we can see that a Customer has a Bio and if we pull out all the customer from a repository and display it, we may have some of them whom are missing the Bio.

Here is the code of how we are mapping our domain objects to a Dto or ViewModel, we are basically just passing the data to the View to render

Now lets say you want to replace the Null Bio with something like “N/A”, some people may go directly to the razor view code and add that but we can do better with automapper, we can tell automapper that when there is a Null just substitute (i.e NullSubsitution) it with something else, our code would then look like

In doing so we will use the same View but now we have “N/A” for “Uncle Leo” bio.

In part2 we will be talking about how to flatten objects by convention.

TEAM

Here are some recommendations to any team lead or development manager to have effective agile teams.

Note: these are just recommendations

  1. Get a white board for your Kanban or Scrum task and stick iteration tasks on the board publicly not in your office or cubicle
  2. Get every developer 2 monitors, that includes the QA since they should be part of your team. For team lead get them 3. One for monitoring the build or Nabaztag build bunny. Hold people accountable for breaking daily builds (buy donuts, pay a dollar into the pool for pizza, etc)
  3. Every developer machine should have 16G of ram or min 8G, anything less is unacceptable.
  4. Use a source control, bug tracker, and continuous build/integration
  5. Get a Powerful Build machine, install TeamCity or Hudson or CruiseControl or get TFS build agent running, or use the cloud to build.
  6. Get tools like Reflector, Resharper or CodeRush, Test Driver .Net, Code Coverage (dotCover, NCover) and “teach, guide” them how to use it effectively, shortcuts etc
  7. Get a wiki up and running for documentation of how to’s (local build, server build, etc )
  8. Teach the team proper unit testing, whichever framework you choose (NUnit, MbUnit, etc etc)
  9. Teach them mocking, whichever framework you choose (Rhino Mocks, Typemock Isolator, Moq, JustMock, etc) pick one and stick to it
  10. Teach the entire team how to debug effectively, using your IDE, teach them what pdb files are and how to remote debug if in .NET world, if you are in Java teach them how to debug a JVM, in Ruby just have them write unit test 🙂
  11. Have the entire team know about your build and document it on a wiki, don’t just have one person who is the build master due to the bus factor (If build master gets hit by a bus tomorrow who can take over the build?)
  12. Have only max 15 minutes daily stand-ups with format of “What I worked on yesterday, What I am working on today, and are there any impediments.”, there should be no sitting or resting or leaning on an object/wall. If there are extra things that come up, have those people with the team lead meet after the stand-up to resolve those issues.
  13. Provide an area for collaboration, pair programming
  14. Have a code clean up iteration for the team don’t just keep pushing features out. Let the team clean up the mess they have created.
  15. Use a runtime analyzer to see what features users are using, tools like Gibraltar or Runtime Intelligent Service definitely helps, or if you are cheap log things into a logging database with NLog or Log4Net.
  16. Automated! Automated! Automated!, builds, unit test, regression testing, performance testing, everyday there should be reports on what has build, passed and failed.
  17. Provide a learning atmosphere for the team have them showcase some technology, what they have learned, or create knowledge transfer brown bag lunches.
  18. Last but not least make it a fun learning environment for everyone involved, celebrate whenever milestones are reached, take the team out to eat to build trust. (There is something about breaking bread with someone, it builds trust.)

By the end you will have an Agile team that looks like the A Team 🙂

(hidden link in image)

javascript patterns

Over the holidays I read Javascript Patterns by Stoyan Stefanov.
Its a pretty light book consisting only of 240 pages, which is light in computer reading, where majority of the books are like yellow pages phone book over 1000 pages. It was quite enjoyable to have a concise book written by Stefanov, another highly recommended book is his Object Oriented Javascript.

Anywayz here is the low down.

Chapter 1. Introduction
This chapter is more of an introduction of using firebug (console.log), jslint, object orientation and prototypes in javascript.

Chapter 2 Essentials
This chapter talks about styles and coding standards, a must read for anyone doing javascript, patterns consisting of the single var variable and minimizing global variables, commenting code, api’s etc

Chapter 3 Literals and Constructors
Its about object literal notation, constructors functions using new, array notations, json, regular expressions and build in types e.g Error consturctor.

Chapter 4 Functions
Contains an indepth use of functions in javascript as first class objects, topics covered were API Patterns (Callback, Configuration objects, returning functions and currying), Initialization patterns (immediate functions, immediate object initalization, init-time branching) and last Performance patterns (Memorization and self-defining functions)

Chapter 5 Object Creation Patterns
One learns about the namespacing pattens, declaring dependencies, private, privileged method. The module patterns, sandbox pattern and last the object constants, static methods, chaining and lastly the method() method.

Chapter 6 Code Reuse Patterns
This chapter talks about inheritance by using borrowing methods, copying properties and mixin in javascript, it also covers classical inheritance but how there are more elegant ways in javascript rather than using the classical way.

Chapter 7 Design Patterns
This chapter goes through 9 GoF patterns and how to implement them in javascript namely Singleton, Factory, Iterator, Decorator, Strategy, Facade, Mediator, Proxy and lastly Observer. The author goes about to show how one can create these patterns in javascript in various ways, it takes a different mind set then the GoF book since javascript is dynamic and functions are first class (GoF was C++ examples in static languagues), if you are a GoF head u should read this chapter.

Chapter 8 DOM and Browser Patterns
The last chapter talks about using javascript in the browser, the author uses mostly YUI, although most of the things covered are already in jquery built in, I wish he covered more on jquery but ohh well. The bonus is he talks about loading javascript how to lazy load script tags and using setTimeOut for long running operations.

Conclusion
All in all I would definitely recommend this book, its not really a beginner book but if you are doing javascript its a definite read.

As I was writing this and finishing up the blog I also saw elegant code has posted a very good review of this books on their blog.

linq

You are here beacause you probably got a Linq error, specifically Operator ‘==’ cannot be applied to operands of type ‘System.Collections.Generic.KeyValuePair‘ and ‘
So if you wish to do something like this to KeyValuePair using Linq

You will get this error
Operator ‘!=’ cannot be applied to operands of type ‘System.Collections.Generic.KeyValuePair‘ and ‘

Since by default KeyValuePairs are struct.
To solve the issue we need to try out this

Hope this helps 🙂

jquery

Was coding some new functionality and though I would post this, so that others can find it helpful.
Lets say you have a form that has a checkbox that will update the select dropdown box with different values, when the checkbox is clicked.
Something like the UI below.

Your html code might look like

On your server side you would provide a Action Method to filter out the data, something like

Now we got to add the javascript to listen to the checkbox event changed, something like this in jquery, since we are getting Json Result we will have to parse the json object to populate the dropdown box.

By doing so you will have something that shakes and updates the drop down box by some simple ajax calls 🙂

UA-4524639-2