3

In a lot of apps I write, I find myself assigning a default value for a subclass of a DBML object. Here is an example of the current way I'm having to do it:

var viewModel = new RandomViewModel
{
    ContactName = (Order.Customer != null ? Order.Customer.ContactName : "Unknown"),
    Date = Order.OrderDate,
    Shipper = (Order.Shipper != null ? Order.Shipper.CompanyName : "Fedex"),
    ShipCity = Order.ShipCity,
    ShipRegion = Order.ShipRegion,
    FirstCategory = (Order.OrderDetails.First().Product.Category != null
        ? Order.OrderDetails.First().Product.Category.CategoryName
        : "DefaultCategory",
    ItemCount = Order.OrderDetails.Count
};

Ideally it would be most readable as this:

var viewModel = new RandomViewModel
{
    ContactName = Order.Customer.ContactName ?? "Unknown",
    Date = Order.OrderDate,
    Shipper = Order.Shipper.CompanyName ?? "Fedex",
    ShipCity = Order.ShipCity,
    ShipRegion = Order.ShipRegion,
    FirstCategory =
        Order.OrderDetails.First().Product.Category.CategoryName
        ?? "DefaultCategory",
    ItemCount = Order.OrderDetails.Count
};

but there is a NullReferenceException for any foreign keyed object that is null. I don't think null coalescing is usable here, but is there some other consise way to accomplish this very frequent task? Perhaps with a class/method extension, or even a VS macro?

I used the Northwind database for this example:

northwind db

Open to any and all suggestions.

arserbin3
  • 6,010
  • 8
  • 36
  • 52

2 Answers2

1

Here's one possible approach to take. Declare partial class Order, with properties like this:

string CustomerNameOrDefault { get { return Customer != null ? Customer.ContactName : "Unknown"; } }

Then your code becomes:

var viewModel = new RandomViewModel
{
    ContactName = Order.CustomerNameOrDefault,
...
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • 1
    This is a good idea - although i think the ideal answer is if it's possible to make it generic. And yet, only do it for properties that are generated by the DBML from a foreign keyed ID field, and that the ID is also Nullable. I'll be attempting that – arserbin3 Jul 03 '12 at 18:15
0

Open to any and all suggestions.

Personally I use AutoMapper to map between my domain models and view models. Your code could now become:

RandomViewModel viewModel = Mapper.Map<Order, RandomViewModel>(Order);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I have considered AutoMapper in the past, but never tried. One database many of our apps integrate with is a legacy one with terrible naming conventions (all column names eight letters with inconsistent abbreviations). So I believe most all AutoMapping would need custom mapping - and seems to make the code even more complex and less readable. It is a good library for many others though – arserbin3 Jul 03 '12 at 18:08