Monthly Archives: August 2009

Tab closing button in Visual Studio 2010

The new Visual Studio 2010 looks. I’m more interested in .NET FW 4.0 and C# 4 new features, but some of the new stuff makes me happy to. But one new function makes me crazy. The editor tabs has now the closing button on each tab. Similar to what browsers have. But I’m used to look at it in upper right corner. And every time I’m about to close a file using a mouse, I’m starting to move wrong way. I hope to get used to this new feature or have some option to turn on the old behavior.

Using custom properties as parameters in queries (in EF)

Most of the time you’re using properties in objects that are also in database. But sometimes you may need to create property in object that’s not in database – it’s used only in this application or it’s there custom logic. Then, if you wanna use it in queries, you’re out. You can use only those properties declared in table, obviously.

But there’s a solution. First, the property has to be created from some other properties, so you’re able to do the same in database. And of course, functions used there needs to be translatable to store language too. This may limit you, but there’s nothing you can do about it, except evaluating query on client side.

Let’s have a table like this:

create table Foo (
  ID int primary key,
  IsAccepted bit,
  IsPaid bit not null,
  IsPacked bit not null,
);

And you wanna have property IsReadyToShip, in application. So you create:

public bool IsReadyToShip
{
  get { return this.IsAccepted.HasValue && this.IsAccepted && this.IsPaid && this.IsPacked; }
}

But with this property you’re not able to query for all Foos ready to ship. Luckily the solution is pretty easy. First you’ll create expression for this:

public static Expression<Func<Foo, bool>> IsReadyToShipExpression = f => f.IsAccepted.HasValue && f.IsAccepted && f.IsPaid && f.IsPacked;

Then you’l prepare static compiled version of this expression, just for performance reasons, you can compile it in getter everytime too:

protected static Func<Foo, bool> IsReadyToShipFunc = IsReadyToShipExpression.Compile();

And finally the property:

public bool IsReadyToShip
{
  get { return IsReadyToShipFunc(this); }
}

Right now we have the same result as before – working property. But because we also have the expressions of the property (and incidentally it’s translatable to store language ;) ), so we can use it for querying. You can use it in an easy way (this is in this case ObjectContext used in Entity Framework):

public IQueryable<Foo> FoosReadyToShip
{
  get { return this.Foos.Where(Foo.IsReadyToShipExpression); }
}

Not the shortest way to do it. But if you don’t wanna to write the condition again and again (and lower the maintainability) this is a way to do it.

Singleton shortcut

Probably every developer sometimes heard about the singleton pattern. I’ll be not far from truth that you’re probably writing it like this:

class Foo1
{
	private static Foo1 _instance;

	public static Foo1 Instance
	{
		get
		{
			if (_instance == null)
				_instance = new Foo1();

			return _instance;
		}
	}
}

I do it same way. But today I seen little bit different way. It’s using the C# coalesing operator and some C-like magic syntax.

class Foo2
{
	private static Foo2 _instance;

	public static Foo2 Instance
	{
		get
		{
			return _instance ?? (_instance = new Foo2());
		}
	}
}

Looks cool, isn’t it? On the other hand I’ll probably use the first one, as it’s more readable, at least for me.

Noviny.

Nevím, co mě to někdy popadne a vezmu si v autobuse/letadle noviny. Asi mám pocit, že bych měl číst také „klasické“ médium. Jenže po pěti minutách jsem hned naštvaný. Mám špinavé prsty. A nedá se pořádně otáčet stránky. Noviny jsou moc velké a papír je měkký.

Vím, že následujících pár dní/měsíců si zase noviny brát nebudu (a pak zase jednou jo :) ). Ale přemýšlím, jestli už finálně není čas na novou technologii pro toto médium.

A možná jsem jen profesně deformován a nejradši bych jen vše četl na nějakém elektronickém zařízení. ;)