Wednesday, May 19, 2010

CAL and AutoMapper

I’m using Composite Application Library (CAL) in a project. I decided to use AutoMapper as well. I put AutoMapper’s Mapper.CreateMap<source, dest>() calls into one of the Bootstrapper.cs methods. I got an InvalidOperationException in the ModuleCatalog’s InnerLoad() function.

Reason

AutoMapper creates an in-memory assembly and CAL tries to read all the assemblies’ Location property. This property raises an InvalidOperationException in case of in-memory assemblies.

Solution

Insert AutoMapper mappings right after the Bootstrapper.Run() call. In my case I call this in App.xaml.cs, so you can write the following:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // Run bootstrapper
    Bootstrapper bootstrapper = new Bootstrapper();
    bootstrapper.Run();

    // AutoMapper mappings
    Parallel.Invoke(CreateAutoMapperMappings);
}

/// <summary>
/// Creates the AutoMapper mappings.
/// </summary>
private static void CreateAutoMapperMappings()
{
    Mapper.CreateMap<Source, Dest>();
}

Friday, May 14, 2010

F#: Piping and Composition

Piping and compositions are very expressive tools in F#. It significantly improves code readability.

Let’s have the following functions:

> let square x = x*x;;
val square : int -> int

> let negate x = -x;;
val negate : int –> int

Function square returns the square of x, while negate returns the negated value of x.

We can calculate square(negate(10)) as follows:

> let x = negate 10;;
val x : int = –10

> let x = square x;;
val x : int = 100

Or a simpler way:

> let x = square (negate 10);;
val x : int = 100

Piping

We can define piping as symbol-named function definition that simply applies two functions in reverse order:

let (|>) f x =
  x f

The example with piping:

> 10 |> negate |> square;;
val it : int = 100

That means ((10 negate) square) –> (square (negate 10)). I used an integer (10) as f.

Composition

Composition is a reverse-order function composition:

let (>>) f g x =
  g(f x)

The example with composition:

> (negate >> square) 10;;
val it : int = 100

That means ((negate square) 10) –> (square (negate 10)).

Usage

The examples above are very simple ones. The real benefits of piping and composition reveal in case of function definitions and usage.

Pipes can reverse parameter definition orders (like in 10 |> negate) and can help type inferring. When F# infers types, it evaluates from upper left corner to lover right corner of the text file, so sometimes it doesn't know the type of a function parameter. Pipe brings parameters forward.

Composition can help omitting function parameter declaration. If the first function expects a parameter, the composed function expects it’s parameter. 

Tuesday, May 4, 2010

Database Comparison

I' am very interested in open source database comparisons. I found two interesting blog posts:

Link: H2, HSQLDB, DERBY, PostgreSQL, MySQL
Link: HSQLDB, H2, SQLite

Sunday, May 2, 2010

Get WPF DataGrid row and cell

There are no simple built-in methods for accessing individual rows or columns in WPF DataGrid. The following code samples will provide simple methods for accessing these items.

First of all we need a helper function for selecting a visual child:

public static T GetVisualChild<T>(Visual parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
       if (child != null)
       {
           break;
       }
   }
       return child;
}

There is a simple method for getting the current (selected) row of the DataGrid:

public static DataGridRow GetSelectedRow(this DataGrid grid)
{
    return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}

We can also get a row by its indices:

public static DataGridRow GetRow(this DataGrid grid, int index)
{
    DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    if (row == null)
    {
        // May be virtualized, bring into view and try again.
        grid.UpdateLayout();
        grid.ScrollIntoView(grid.Items[index]);
        row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    }
    return row;
}

Now we can get a cell of a DataGrid by an existing row:

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
    if (row != null)
    {
        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

        if (presenter == null)
        {
            grid.ScrollIntoView(row, grid.Columns[column]);
            presenter = GetVisualChild<DataGridCellsPresenter>(row);
        }

        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        return cell;
    }
    return null;
}

Or we can simply select a row by its indices:

public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
    DataGridRow rowContainer = grid.GetRow(row);
    return grid.GetCell(rowContainer, column);
}

The functions above are extension methods. Their use is simple:

var selectedRow = grid.GetSelectedRow();
var columnCell = grid.GetCell(selectedRow, 0);

View source on Google Code (unformatted selection possible)