M-V-VM Building an ERP-system – part 9 of N – Orders and handling null

Skrevet - Sunday, June 6th, 2010 kl. 21:45 | Kategori - * Coding, English posts

We are now turning towards a somewhat more involved example after a little break in part 8 – the orders. We’ll make a bit more complex filter for the list of orders while keeping it paged – the IOrdersViewModel is so akin to ICustomersViewModel that I’ll jump to the differences in the IOrderFilter:

public interface IOrderFilter : IBaseFilter
{

DateTime? OrderDateFrom { get; set; }
DateTime? OrderDateTo { get; set; }
ICustomer Customer { get; set; }
IAvailableCustomerSelection AvailableCustomers { get; }

}
public class OrderFilter : BaseFilter, IOrderFilter
{

private readonly IDefaultEntities _defaultEntities;
private ICustomer _customer;
private DateTime? _orderDateFrom;
private DateTime? _orderDateTo;
public OrderFilter(IAvailableCustomerSelection availableCustomers, IDefaultEntities defaultEntities)
{
AvailableCustomers = availableCustomers;
_defaultEntities = defaultEntities;

}

public ICustomer Customer
{

get { return _customer ?? _defaultEntities.DefaultCustomer; }
set
{
_customer = value;
OnPropertyChanged(“Customer”);
OnFilterUpdated();

}

}

}
public class DefaultEntities : IDefaultEntities
{

private static readonly ICustomer _defaultCustomer = new NullCustomer();
public ICustomer DefaultCustomer
{
get { return _defaultCustomer; }

}
private class NullCustomer : ICustomer
{

public long Id
{
get { return -1; }

}

public string CompanyName
{

get { return “No customer”; }
set { }

}

}

}

So, a few notes:

What is the deal with the default customer?
The DefaultCustomer is a dummy-implementation of ICustomer that is guarantied to exist only once in memory as it is statically new’ed up and the class is private to the DefaultEntities-class. Basically, it represents null everywhere a Customer is allowed to be null at some point. This means that in the IOrderFilter implementation, the Customer is allowed to be null – representing ‘not searching by customer’. So, we get the ability to show null in a combobox’ed list.

Why -1 in the Id property
To make absolutely certain that Default customer is never saved, I’ve arranged for it to behave ‘badly’ in the persistence layer. Id -1 is an obviously wrong value – meaning that it does not belong in a persistent state.

Why isn’t available customers a part of a ViewModel – and what are they doing in a filter?
Since I’ve chosen to implement the filter with a combobox containing all customers, I need to gain access to these – and I’ll need it again when we turn to Order Detail, so I decided to put it in it’s own class. I didn’t make it a part of the ICustomersViewModel since it isn’t really a list, I want to interact with – it’s readonly by nature – so it received the name IActiveCustomers. I don’t want to read the database for customers everytime, I new up an order – so, it is basically a form of caching.

Other than that, Orders behave much like Customers when we look at the list – so, next and last chapter will focus on the detail editing of Orders.

About the series:
The solutions introduced here are not entry-level material. You should understand generics and basic patterns to fully utilize the material presented. The solution represents about 3 years of experience working with one ERP-solution. Some of the choices taken are heavily influenced from the large-scale solution and may seem overkill for the simple examples here, but the idea is to show the end-result of my experience and hopefully you as the reader will at least see a new way of doing things. The solution is built in .Net 4.0 with heavy use of WPF’s capabilities. Here you can find the source-code for the series. You are free to use this code for any purpose, but it is presented as-is with no guarantees. Enjoy!

Feed | Trackback |

1 Comment
  1. Goblincave » M-V-VM Building an ERP-system – part 10 of N – OrderLines said,

    June 6, 2010 at 22:38

    [...] saved the goodies for the last post. We are now ready to edit an order after post 9 – and we want to do it in style. We want to present the user with a really nice interface for [...]

Post a Comment