Archives for April, 2010
Úplně bych zapomněl na letošní Gopas TechEd. Vlastně už začal, takže jsem pozadu. Nicméně to nejlepší teprve přijde.
Mám celkem tři přednášky. První je již zítra a pokrývat bude “.NET4 – PLINQ, Task Parallel Library”. Já sám jsem blázen do všeho paralelního, a pokud jste jako já nemůžete vynechat.
Druhá, ve čtvrtek, je na téma “Novinky v Entity Framework 4.0″. Co říci více? Název je myslím jasný. A poslední, taktéž ve čtvrtek, je “ADO.NET vývoj s Firebirdem”. No pokud si nemyslíte, že existuje jen MS SQL Server a chcete se dozvědět něco (a možná zkusit) o jedné z alternativ, rozhodně přijďte – doufám, že zodpovím všechny vaše otázky a nalomím vás.
Program poskytne informace kde a kdy se vše koná.
Some time ago I wrote about creating PowerShell aliases for commit and update for TortoiseSVN. But I needed little bit more flexibility with path so I added a parameter with default to ..
function fn_update($path = ".") {tortoiseproc /command:update /path:$path}
function fn_commit($path = ".") {tortoiseproc /command:commit /path:$path}
I remember from old times that I was using some plug-in into Delphi IDE to set tab order by clicking on components in order you wanted the ordering to be. Today I was reviewing the tab ordering in ID3 renamer as somebody reported in forum it’s wrong. I was scared doing all the work through Properties window in Visual Studio.
But it turned out, that Visual Studio has this tool in the box. You can find the description here. You’ll simply select form, go to View > Tab Order and click on different controls to change the ordering. You see the numbers directly in designer and go through possible options by clicking.
Pretty nice, don’t you think?
I don’t know whether it’s somewhere specifically pointed, but the ObjectContext in Entity Framework v4 has two (three) new handy methods. And I like these.
It’s kind of escape hatch similar to DefiningQuery. First method is Translate<T>. It takes DbDataReader and materializes the data back into entities. It’s similar to Materialize method from EFExtensions. If you some code in pure ADO.NET and you don’t have time or resources to redo it in EF (or it’s way easier old way) you can rewire the result into existing objects. I like it. Whenever I’ll feel I need to get dirty (and probably due to performance reasons) I can do it pretty easily.
using (testovaciEntities ent = new testovaciEntities())
{
IDbConnection conn = (ent.Connection as EntityConnection).StoreConnection;
conn.Open();
using (IDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from master";
using (DbDataReader reader = (DbDataReader)cmd.ExecuteReader())
{
MASTER[] result = ent.Translate(reader).ToArray();
Console.WriteLine(result.Length);
}
}
}
The other method is similar and simplifies the process of getting dirty if you simply need to run you fine tuned query with neat and sexy constructs.
It’s ExecuteStoreQuery<T>. This method simply allows you to run any sql command directly in store language (thus you can use all features your database offers) and fetch and materialize back resulting entities. Similar to this is ExecuteStoreCommand which is similar to ExecuteNonQuery from pure ADO.NET. But you can do this without the method easily too, the method is just more convenient.
BTW also note that the Translate method isn’t adding the entities into context, it’s just about fetching and materializing.
I updated my pretty big model today. Added absolutely simple table, no foreign keys, basic datatypes, simple PK. When I was calling CreateObjectSet<T> what a surprise - InvalidOperationException saying “Mapping and metadata information could not be found for EntityType”. That’s not so much descriptive.
Quick search thru internet resulted in suggestion that I’m missing some property in my POCO classes. I was not. I double checked it and I know that Entity Framework throws MetadataException listing all the missing properties (yep, I was there too, but it’s easy to fix). Another reason I found was that the metadata are not placed into resources or things are screwed up because of mixing two models in one project (to be clear, it is possible, but you have to carefully design your namespaces and names). Not the case either. I have only one model, metadata are there properly, other entity sets were working fine.
After loosing my mind slightly I found, rejected and then believed back again that it’s true, that you have misspelled property. I first rejected the idea because it was mixing the misspelled property and missing property together. And I knew about the MetadataException case. But the misspelled property actually results in InvalidOperationException exception with useless message.
Hopefully you’ll believe this (or any other solution found) faster than I did.
Yesterday there was a question in one list I’m following. Simply to share some recommendation for tool being able to reverse engineer the database structure and show some E-R diagram.
After I get time to reply with some suggestions, idea came to my head. The EDM designer is actually kind of reverse engineering tool. Did you realized that?
As the EF support for major database platforms is available it’s easy to get a basic overview of the database structure without leaving Visual Studio. Sure it doesn’t have some cool features of specialized programs, but on the other hand it’s already in VS (Express edition is free) and you don’t have to buy and install another product (if you’re not doing this often, but then you’re probably using the specialized program).
I was looking at Go language presentation now.
The first stuff that took my attention was the C-like syntax. I really don’t like it. I want my language to talk to me. There’s, IMO, no reason in todays world to have identifiers and symbols from only couple of characters and using a lot of different characters from various places on keyboard.
Anyway what was interesting for me was the concept of channels, especially the mapping the to threads and attempt to effectively use system resources. The APM (Asynchronous Programming Model) and/or new Tasks both looks similar to channels – at least from the view what it’s offering, not how it’s exposed into language.
I don’t know about internals. But I’m sure, that the threading and parallelism attempts to get some reasonable programming model(s) are exponentially growing.
Entity Framework has a nice ability to wire up associations on both ends when the related entities come into context. This feature is handy and you can use it to simulate some scenarios, like this. But we’re on EFv4 time, everybody is using POCO.
In last project I used POCO too, just to make myself more familiar with the way how it works in EFv4 and to face some challenges. Last week I was designing some flow, and I needed to have some related objects in memory too. I immediately jumped into Include method (actually I used this improved version). But later I couldn’t help but wonder would the automagic association wiring with (pure) POCOs?
I created simple master-detail scenario and created these POCO classes (note I’m not going to use proxies).
class Master
{
public int ID { get; set; }
public string Foo { get; set; }
public ICollection<Detail> Details { get; set; }
public Master()
{
this.Details = new List<Detail>();
}
}
class Detail
{
public int ID { get; set; }
public string Bar { get; set; }
public Master Master { get; set; }
}
And created test code (testEntities is a simple ObjectContext).
using (testEntities ent = new testEntities())
{
ent.ContextOptions.LazyLoadingEnabled = false;
var details = ent.Details.ToArray();
var q = ent.Masters;
foreach (var item in q)
{
Console.WriteLine(item.Details.Count);
}
foreach(var item in details)
{
Console.WriteLine(item.Master.ID);
}
}
If you run the code, you’ll see it works as expected. Exactly like the generated classes, although these are (pure) POCO classes – no need to use proxies or any special collections. I like the smart work done behind by Entity Framework for me.
The soon to be released Firebird 2.5 has a new ability to cancel running command (or any operation currently being processed by server) via API (in 2.1 you can do it via monitoring tables). This is a nice feature, interesting not only for database administration tools.
I was working on supporting it for a while in .NET provider, but I’m happy to say that it’s done (though I may tune the boundaries based on feedback). From some initial proposal in list I picked one following the design of SqlClient, as it’s a de facto standard in ADO.NET world.
So right now you can do:
using (FbConnection conn = new FbConnection(@"database=localhost:rrr.fdb;user=sysdba;password=masterkey"))
{
conn.Open();
//conn.DisableCancel();
//conn.EnableCancel();
using (FbCommand cmd = conn.CreateCommand())
{
cmd.CommandText =
@"execute block
as
declare cnt int;
begin
cnt = 999999999;
while (cnt > 0) do
begin
--cnt = cnt-100;
end
end";
IAsyncResult ar = cmd.BeginExecuteNonQuery(null, null);
Thread.Sleep(4000);
Console.WriteLine("Canceling");
cmd.Cancel();
try
{
object result = cmd.EndExecuteNonQuery(ar);
}
catch(FbException ex)
{
Console.WriteLine(ex.Message);
}
and you’ll get the result (even though the loop in fact never ends):
Canceling
operation was cancelled
Of course, you can screw things up by i.e. starting command and then trying to do something else with connection/transaction/command (the ADO.NET providers are not thread safe by default).
Oh, BTW, if you wanna test it, download weekly build.