Tag Archive for 'DevExpress'

Matrix algorithm for rendering XtraLayout designs into Web UI

In the past post on using XtraLayout component by DevExpress as a simple but powerful run-time designer I’ve mentioned rendering designs into Web UI. Here’s simple recipe for the matrix algorithm that is actually used to render XtraLayout-generated form designs in web UI of xLim 2 implementation.

Matrix algorithm for rendering XtraLayout designs into Web UI

This implementation does not require creating groups for every logical section like it has been done in XAF Web UI (and defining some custom orientation direction there) - all group contents sit within one table. Additionally this makes the designs reusable for WPF, Windows.Forms and Silverlight UI rendering (xLim 2 compatible renderer prototypes already exist for the first two).

Basically the UI rendering process happens in two steps. First, we check out every single control in the group and register its coordinates. We are only interested in unique X and Y values at this step.

Then, we count number of unique non-zero X values (that’s the number of columns in our table) and number of unique non-zero Y values (that’s the number of rows in our table). After that we calculate the sizes of these columns and rows (derive them from actual X, Y values). Widths will get sizes in percentages, since normally we want our forms to resize with the browser window.

In second pass we simply render all the controls into cells of the HTML table, while assigning appropriate RowSpan and ColSpan to these cells.

Online Analytical Processing with .NET and DevExpress PivotGrid

Now and then I hit situation where some kind of Online Analytical Processing (OLAP) is needed.

In short OLAP is just a way to provide convenient slicing and dicing of some data. You can call it fluent reporting, where the the data-source for the report is fact table and report itself is some kind of aggregation.

There are two simple implementation scenarios of OLAP .NET world that I have encountered so far. In both cases facts table could be simply represented as enumeration of strongly-typed objects (residing DB, memory, flat file etc). And the aggregation is performed via

  • Linq queries against facts data that generate some output. Sorting, grouping, filtering, projecting – all this is done in the code. This output is in fact the aggregation/projection/report (whatever you call it).
  • Pivot Grid controls that are fed with the facts table and are used to let end-users define filtering, sorting etc rules via the UI. The process is less flexible than in the previous scenario, but it does not require any development skills.

I’ve assembled quick-n-dirty tool for the second type of approach. You can download it here and use to slice-n-dice your raw data (see below for the details).

Simple OLAP tool

Continue reading ‘Online Analytical Processing with .NET and DevExpress PivotGrid’

XPO hosted on a web site (remoting with binary encoding + http channel)

Below you will find an extremely simple prototype that uses XPO hosted within web application (http channel) with the binary encoding. You will need to have DevExpress components installed and Visual Studio 2008.

Now, before you continue, here is an important caveat - don’t use XPO in any complex project. It will just become bottle-neck there.

In my case complex project means:

  • 41 Visual Studio projects (74k LOC and 35k statements)
  • 3 integration projects on CC.NET
  • 417 revisions over 6 months
  • 2Gb+ FileStore in production system
  • All the features of the xLim 2 approach (and some more)

Here’s the Kiviat metrics graph:

xLim 2-2 Kiviat Graph

And these are some of the problems with the XPO:

  • Linq2XPO implementation is horrible (I’m not even talking about decent documentation)
  • Flexibility is extremely low (most of all I miss custom mapping routines and ability to specify custom queries)
  • Generated SQL is quite inefficient in complex requests
  • Remote communication is too chatty by default

As you can see, these problems of mine mostly come from the Lego scenario.

If I were able to start the project from scratch, then I’d probably (would have to do some prototyping first) use NHibernate. Half a year ago there was no chance of using it, since DX controls would not be able to work with NHibernate in server mode.

Ok, you have been warned. Now you finally can download sample of XPO hosted on a web site (remoting with binary encoding + http channel).

XPO-related changes that are needed to make the ORM+IoC samples work.

Below you will find the description of the minimal changes to XPO sources to make the ORM+IoC series samples work. Obviously, I’m quite limited with that, since these sources are commercial.

That’s the new method that we have to add to ReflectionDictionary to make it recognize out interfaces:

public void AddClassInfo(Type service, XPClassInfo implementation)
{
  classesByType.Add(service, implementation);
}

Or one could just add method that looks up existing type registration and re-registers it as service type, while hiding the ClassInfo specifics.

The changes, that differ FireSession (and logically linked classes) from normal Session, revolve around the delivery of the _resolver delegate from the FireSession Constructor to this method inside ObjectLoader:

void CreateTypedObject(object id, XPClassInfo classInfo) {
  theObject = null == _resolver ?
    classInfo.CreateObject(loader.Session) :
    _resolver(classInfo.ClassType);
  cache.Add(classObjects, theObject, id);
}

The _resolver delegate is a simple lambda passed down from the IoC:

private Func<Type, object> _resolver;
...
// register FireSession with the default data layer
builder.Register<Session>(
  c => new FireSession(type => c.Resolve(type))).ContainerScoped();

Note: these 30min hacks are neither stable nor intended for production. It would require at least XPO unit-test suite and IoC+ORM functionality/integration tests to stabilize them with all the required usage scenarios. And that’s out of the scope of the current research.

In my next article I’ll get to the topic of the efficient unit-testing of decoupled components with the MockContainer. That’s where we left the xLim 2 series.

How to implement unit-testable abstraction and reusable domain models with IoC + ORM?

In the previous post in ORM+IoC series we’ve managed to decouple away from the ORM-based collection and the literal-based syntax (which was bound to the logics of eXpress Persistent Objects ORM).

In this post we’ll try to get rid of all the remaining coupling (while making it unit-testable) and even add domain-based extensibility that is easy to implement.

Let’s modify the code:

public sealed class DisableAllAccounts : ICommand
{
  private readonly IOrderedQueryable<IAccount> _accounts;
  private readonly ILog _log;

  public DisableAllAccounts(IOrderedQueryable<IAccount> accounts, ILog log)
  {
    _accounts = accounts;
    _log = log;
  }

  public void Execute()
  {
    _log.Write("Disabling accounts:");

    var accounts = from c in _accounts
                   where (c.Disabled == false)
                   select c;

    foreach (var account in accounts)
    {
      IComment comment = account.CreateComment();
      comment.Text = string.Format("Account {0} was disabled on {1}",
        account.FirstName, DateTime.Now);
      comment.Save();
      account.Disabled = true;
      account.Save();
    }
  }
}

Continue reading ‘How to implement unit-testable abstraction and reusable domain models with IoC + ORM?’

How to use ORM (XPO), IoC, C# 3.5 syntax, Linq and .NET 2.0 together?

One of the most important features of any proper development architecture, environment or process, is that the synergy (network) effects accumulate, if you keep doing the right things. IoC/DI, unit testing and TDD, continuous integration - all these just form up the external side of how everything works. They simply help (or force) to shape the logics of the development to make everything fold smoothly and let your solutions accumulate the benefits.

Now, let us get back to the IoC+ORM series and take one more look at the ICommand implementation from the last post. It could be improved quite a bit:

public sealed class DisableAllAccounts : ICommand
{
  private readonly IRepository<Account> _accounts;
  private readonly ILog _log;

  public DisableAllAccounts(IRepository<Account> accounts, ILog log)
  {
    _accounts = accounts;
    _log = log;
  }

  public void Execute()
  {
    _log.Write("Disabling accounts:");
    _accounts.CriteriaString = "Disabled=0";
    foreach (var account in _accounts)
    {
      _log.Write(account.Name);
      account.Disabled = true;
      account.Save();
    }
  }
}

Things become better as we throw out things that do not fit well. So let us remove our custom IRepository<> from this picture (along with the FireCollection behind it) and get rid of the string literal in favor of something that is compiler-checked

Continue reading ‘How to use ORM (XPO), IoC, C# 3.5 syntax, Linq and .NET 2.0 together?’

How to inject ORM with some IoC?

This post continues ORM+IoC series, namely - the discussion initiated by “How to decouple your code from ORM (XPO) while granting it the power to IoC?” and comments that followed.

Let us try to inject ORM itself with some IoC and see what happens. Take a look at the following exaggerated command example:

public sealed class CreateRandomAccount : ICommand
{
  private readonly Account _account;
  private readonly ILog _log;

  public CreateRandomAccount(Account account, ILog log)
  {
    _account = account;
    _log = log;
  }

  public void Execute()
  {
    _log.Write("Creating new random account");
    _account.Name = Guid.NewGuid().ToString();
    _account.Disabled = true;
    _account.Save();
  }
}

I like to use some ICommand implementations as examples since they help to crystallize the effects of IoC in one single piece.

The code above, being resolved in some container, just does what it says: it asks for the class (registered as Factory instance) and saves it to some repository. IoC uses Session from the current scope to create the instance and so it will use UoW, session in transaction or whatever session descendant is available in the container. Additionally it writes a note to the logger.

Continue reading ‘How to inject ORM with some IoC?’