Gáspár Nagy on software

coach, trainer and bdd addict, creator of SpecFlow; owner of Spec Solutions

OracleClient problem in .NET 2.0 SP2 (comes with VS2008 SP1)

by Gáspár on January 13, 2009

As mentioned in a lot of sources, Visual Studio 2008 SP1, in spite of its name, also contains service packs for the .NET core components. Namely:

  • .NET 2.0 SP2
  • .NET 3.0 SP2
  • .NET 3.5 SP1

Unfortunately there is not too much information available what these components contain, but it seems that they may seriously affect applications that were running perfectly before.

It seems for example that .NET 2.0 SP2 contains a change in the System.Data.OracleClient assembly that may break applications using an Oracle database.

The most visible result of the issue is that LOB handling fails with the exception:

Modifying a LOB requires that the connection be transacted.

Although this issue causes breaks in applications, there are even more dangerous effects of the bug. The transactions may automatically switch “Auto-commit” mode that can produce inconsistent and corrupt data in the database even without any visible error. More details here.

In December, Microsoft released an update to the .NET Framework service packs: KB959209

Funnily, Microsoft has not stopped confusing developers with the updates. Although the title of the update is “Microsoft .NET Framework 3.5 Family Update”, it contains updates for .NET 2.0 SP2, .NET 3.0 SP2 and .NET 3.5 SP1. You can install these updates individually. (To resolve the Oracle issue, it is enough to install the fix for .NET 2.0 SP2.)

Update: It seems that the update can be installed only to English .NET frameworks. Other language versions (e.g. German) can be also found at Microsoft Download Center (although it is not so easy). Here are the most common download links for German .NET frameworks:

Visual Studio 2010 – Historical Debugger

by Gáspár on December 16, 2008

I’ve played a little bit with Visual Studio 2010, trying out the automated web UI tests, but I have found a feature that became quickly my favorite from the VS20010 feature list. This is the historical debugger.

The idea is easy: the IDE collects and stores information about the debugged application (important events, called methods, parameters, (handled) exceptions, etc.) that allows you later to jump back in time, and check what happened before you have actually run into a problem that you want to fix. You can walk through the execution steps and review when the things went wrong. (The experience is a little bit similar like the call stack window, but here you are not constrained to the callers and you can get much more information about the executed methods.)

Of course nothing is for free, the application will be slower with this historical debugger, but MS addressed this problem, and you can fine-tune the information to be collected (or even switch it off), and you can also limit the size of the collected data (it always keeps the last part of it, so when you break the execution the information that is relevant to the experienced problem will be most likely still available).

You can see a nice screencast about this new feature by Habib Heydarian and also some screenshots from Pavel Nikolov.

Entity Framework as an OR/M

by Gáspár on December 10, 2008

I know that Entity Framework (EF) has a lot of concepts behind the OR/M features, but I think most of the people who try it out just use it as an object-relational mapping tool. And it seems that also the tools around it (designer) are also driving you to this direction. And BTW this is the reason why MS could announce it a recommended replacement for LINQ to SQL.

I have also investigated EF from this point (I did a small application with it). Here are my feelings.

Note: I have used the currently released version of the Entity Framework, as I wanted to see, how it is useable now.

Generally I have been quite productive with EF and I have not encountered any problem that I could not solve. On the other hand, it did not really impress me. It does not really try to be smart, you have to do everything you want explicitly. This seems to be a defined concept that I can accept, but in some cases the default behaviors are not defensive enough, which caused some tricky errors in my application.

So I have collected some things where IMHO it makes sense to take extra care when writing an application with EF.

Designer

I think in a larger project the designer will be the first problematic point. As normally the model is modified by multiple developers, I already see the nasty merging issues that you can run into. But even for a single developer, it is very hard to have an overview of your domain model in a single screen, where only 6-8 entities can fit nicely.

I also encountered with funny issues with the designer that does not seem to support the code-first approach to extend your domain model. I wanted to create a new entity, so I selected the Add/Entity in the designer. Then I realized that I cannot map it to a table, until I add a table too – which you cannot do from the designer (at least I could not find out how). The new unmapped class, although it was not used in the application yet, broke all other use-cases too, so my application stopped working. So finally I created the table manually in the database and refreshed the model. It still did not work, and after 10 minutes I realized that the refresh also created a new entity with an “1” postfix and mapped the new table to it – but as the designer surface was full, the generated entity was out of the screen (and I did not scroll down). The only thing that I don’t understand is why it is allowed to create a new entity from the designer then? And of course the same refers to the new properties created in the designer.

Lazy framework

From the OR/M functionality my biggest problem was the way how EF handles the unloaded relations. There is no lazy loading, I knew that. But I was very much surprised that if you access a property that is not loaded yet, you don’t receive an exception! For references, it returns null, for collections an empty collection. This is a great source for hidden errors, so I decided that I’ll consider a line of code like these, simply as a mistake:

Console.WriteLine(product.Category.Name);

DoSomething(product.Category)

foreach(var item in order.Items)
    //…

These lines properly should look like this:

if (!product.Category.IsLoaded)
  product.CategoryReference.Load();
Console.WriteLine(product.Category.Name);

if (!product.Category.IsLoaded)
  product.CategoryReference.Load();
DoSomething(product.Category)

if (!order.Items.IsLoaded)
  order.Items.Load();
foreach(var item in order.Items)
  //…

You say now that this is lazy loading. Ok, you are right, but if you want to be correct and you don’t want to have lazy loading (because it is the evil that makes our application wrong), you still have to write:

if (!product.Category.IsLoaded)
  throw new ProgrammersFaultEx(
    “Don’t you think that this should be loaded, mate?”);
Console.WriteLine(product.Category.Name);

if (!product.Category.IsLoaded)
  throw new ProgrammersFaultEx(
    “Please load the category, please, please, please!”);
DoSomething(product.Category)

if (!order.Items.IsLoaded)
  throw new ProgrammersFaultEx(
    “I told you already, that this wont work!”);
foreach(var item in order.Items)
  //…

You say now that it is too verbose. And you are actually right.

Transactions and query consistency

There is a little bit similar problem with the query consistency. The use case is that you write a method that lists some products.

void ListProducts()
{
    foreach(var p in db.Product.Where(p => p.Color == “blue”))
        Console.WriteLine(“{0} {1}”, p.Name, p.Color);
}

This method will work fine until you call it from an update use case, where you already updated a color of a product from blue to red. In this case you might see something like:

product1 blue
product2 blue
product3 red

Strange, isn’t it? The problem is that the db.SaveChanges(); is only invoked at the end of the transaction, and therefore the database does not know about this change at the time when the method is called. The solution: you have to call the db.SaveChanges() earlier. Clear. But who should do this exactly? The method that updated the color? Just to make sure that methods like ListProducts() work well if they called later? Or should the ListProducts() call it? Just to make sure that it does not depend on pending changes? Or should someone else call it?

Well, of course this is not an easy question (which is good, if you see this as a reason for salary increase), but the funny thing here is that the OR/M knows that there is a pending product change and that you are about to list products! So why I should care?

Collection as a query

Sometimes you don’t want to populate a collection, but you want to use it as a query for further transformations. If you write

order.Items.Where(oi => oi.Product.Color == “blue”)

this will filter the loaded collection (which is an empty, when you forgot to call Load()). If you want to query the database, you have to write:

order.Items.CreateSourceQuery()
    .Where(oi => oi.Product.Color == “blue”)

But this will not contain the items, that are added/removed locally, so you have to call db.SaveChanges(), or check whether it is loaded already, so you end up with:

var collectionToQuery = order.Items.IsLoaded ?
    order.Items : order.Items.CreateSourceQuery();
collectionToQuery.Where(oi => oi.Product.Color == “blue”)

but it does not compile, as order.Items is not queryable… :) So the winner is…

var collectionToQuery = order.Items.IsLoaded ?
    order.Items.AsQueryable() : order.Items.CreateSourceQuery();
collectionToQuery.Where(oi => oi.Product.Color == “blue”)

And now you are finished.

Small annoying things

Of course I’ve found also some small annoying things. Without too much explanation here is a list:

  • Single() / SingleOrDefault() does not work in LINQ to Entities. You have to use First() / FirstOrDefault().
  • There is no operator in LINQ to Entities to express the SQL “LIKE”. They suggest to use Contains(), but LIKE can do more!
  • You cannot map custom methods / properties, so you cannot encapsulate parts of the queries, and also you have no chance to solve things like the missing LIKE.
  • I have not found a way how to map a database field to an enum property. (You need to write a wrapper property).
  • There are not too many ways to control what is cached in the object context, you can Detach() objects individually or skip loading of a query result.
  • If you use the Include() method to add relations to the query load span, you have to specify the expressions to be loaded as string. In the time of lambdas, this is quite ugly IMHO. Also the syntax looks quite strange, if you want to include a collection and a reference in each item in the collection, as you have to write this as: orders.Include(“Items.Product”), although Items is an EntityCollection that does not have a “Product” property.
  • You cannot compare entities in queries, you have to compare the id fields (the id fields should be hidden by the OR/M as much as possible):
    …Where(p => p.Customer == selectedCustomer)
    causes

    “Unable to create a constant value of type ‘Closure type’. Only primitive types (‘such as Int32, String, and Guid’) are supported in this context.”

    and has to be written as:
    …Where(p => p.Customer.Id == selectedCustomer.Id)

MVC – What is the Model? How to pass data to the view?

by Gáspár on December 8, 2008

In my introduction post, I have mentioned that I will collect the problems or questions that I have encountered during my MVC research. So this is the first one.

As I have discovered these problems during the rewrite of our code sample for Genome, which is a simple web shop, I’ll use the terms and use cases of that sample. The sample can be downloaded together with the evaluation version of Genome, but even without downloading it you will probably understand the concept. It is a simple web shop with products, categories, shopping cart, order and a simple user class.

What is the model?

If you create a new MVC web project with ASP.NET MVC wizard, it creates three different folders for models, views and controllers. However, if you watch the “getting started” videos, the “Model” folder is always left empty or even removed from the project (StoreFront). What does this mean? Where and what is the model really?

Based on the original MVC pattern, the model should represent the data and the business logic that manipulates the data. It is also an important point that the model does not know about the view, in order to ensure the desired separation.

So if we transform this to today’s .NET multi-tier web development, than we get the result that the model is the business logic and the domain model of our application. Which is normally separated into a different layer anyway – a different assembly (sometimes even behind a (WCF) service interface). So probably that is the reason why the model folder is always empty – the model is in a different assembly.

The business logic, especially if it follows some kind of SOA pattern, usually separated by the different function groups of the application. So there are different business logic methods for product management, ordering, user management, etc.

The user interface is, however, usually not separated so well. The user login and the shopping cart status are displayed in every page. You can put the items to the shopping cart directly from the product display page and you might want to display the categories even in the shopping cart page. The different functionalities of the application are composed together in the displayed pages – the views.

As the model does not know about the view, the controller should collect the data from the different business logic functions and pass it to the view. As in the MVC framework, the view can only have a single model (not considering the ViewData state bag, as it is not type safe and should not be used heavily IMHO), this means that each view will have a class that groups together the different domain model pieces (let’s call it view data class). But where should this class come? Does it rather belong to the view or rather to the controller? In the “getting started” samples, this class is usually together with the controller (as a nested class of the controller class). I don’t like public nested classes, so I placed these view data classes next to the controllers in separate files. But I’m not fully satisfied with the result. I think to mix these classes to the views (especially since they are aspx/ascx files, and not even pure C#) is even worse. But we can also see these classes like the projections of the domain model to a concrete use-case. From this point of view, maybe they belong to the model a little bit. Should they maybe come into the “Model” folder in the web project?

The SelectList problem

It is probably an interesting fact that (at least in the public samples) sometimes also very user control specific data classes are used as view data, for instance SelectList. Based on what the MVC pattern says, this should be OK, as this information is passed from the controller to the view, and the controlled may know about the view details. Maybe the view data should be much more view specific and more detached from the domain model? (You might think that this is just a minor question, but if you want to setup an architecture for a project where multiple developers work on different parts of the application, you have to agree, what kind of data you pass to the view, and where you do the different transformations.) Is it the responsibility of the view to transform the model data to user control specific representation or is it the responsibility of the view? Based on my MVC pattern knowledge, this should be the view, but I’m not sure. But SelectList under the ViewData.Model property is suspicious for me somehow…

Refactoring view data

I have another question regarding the model data,too. Let’s suppose that I have a view that has to display a simple domain model entity like a shopping cart. Currently I don’t have anything else in the view, so the ShoppingCart class can be used directly as a domain class for this view. But should I use it directly?

If the view will be extended later, probably other things must be also displayed in the view (e.g. the category list), so I will need to introduce a view data class that groups the shopping cart and the category list together. Introducing such class requires a painful refactoring in the view. With the current refactoring tools I know, you cannot automate this step, you have to go through all the cases where you have used ViewData.Model and should replace with ViewData.Model.ShoppingCart. (Well, maybe you can use search and replace, but still not nice.)

I see two possibilities to avoid this situation:

  1. You always introduce a view data class when creating a new view, even if it’s embedding a single class only.
  2. You don’t use the ViewData.Model directly, but you always create properties in the code behind that access the elements in the model class (in this case a ShoppingCart property that returns the ViewData.Model). If you have to extend the view data, you can simply change the property implementation and you don’t have to change the aspx code itself.

Unfortunately I don’t like either of these. Solution a) causes probably an unnecessary overhead for most of the views (the ones that will never be extended), and also causes too long data access expressions (ViewData.Model.ShoppingCart.Items.Count). Solution b) decreases the gain that we have achieved by having the generic view base class and the strongly typed model. It also increases the usage of the code behind file, which is the first step to have logic sneaked into the views that we wanted to avoid with MVC. Also it is hard to enforce that everyone keeps this rule and does not access the ViewData.Model directly, causing a big mess in the view. But I think altogether I like solution b) more, probably because the long data access expressions anyway mess up the view aspx.