SQL from Linq to Entity Framework
November 27, 2008 on 8:49 am | In Oracle, DotNet | No CommentsProblem: how do I tell what SQL does Linq to Entity Framework generates?
If you are using SQL Server its easy. You start the SQL Profiler and you are done. If you are using Oracle the task is not so easy. You can:
1. start the trace on the Oracle server and get the huge barely readable text file physically on the server and use TKPROF
2. look up the lat SQL query from session using the Enterprise Manager-Konsole
3. buy a 3rd party tool like FlexTracer to trace the OCI communication.
Well yeah! Not fun.
But there is another option inside Entity Framework itself! It is not quite obvious, so it took me a little to figure it out:
((System.Data.Objects.ObjectQuery)Query).ToTraceString()
where Query is something like System.Linq.IQueryable<T>.
Everyone is entitled to broad opinion
October 26, 2008 on 8:18 pm | In DotNet | No CommentsI had an interesting dispute with my colleague weather to use only qualified type names in code or not. He insisted that we should not use the using directive to introduce a namespace but to write the fully qualified name every time it is necessary. I had quite opposite opinion but I agreed to give it a try. So I started to write
public class MyClass { public static void Main() { System.Console.WriteLine(“Foo”); } }
instead of
using System; public class MyClass { public static void Main() { Console.WriteLine(“Foo”); } }
Unfortunately over time it proofed no value to me. I was not happy with this rule, but I thought I’ll consult the brother audience for opinion before I start a holly war against something that I might not fully understand.
I decided to give stackoverflow.com a try. It is quite new Q&A Service dedicated specially for software developers. I write my question I was given a flood of answers. Some better, some worse. Some hitting the bottom line, some pouring “divine knowledge” of individuals that think they know things better.
Nevertheless the discussion on stackoverflow.com helped my back my opinion. Give it a try!
MTS 2008, Warsaw, Poland
October 12, 2008 on 10:28 pm | In DotNet | No CommentsMicrosoft Technology Summit 2008 (8th and 9th November), the biggest Microsoft technology event in Poland went quite well. It was my first MTS and I was trying not to expect anything special. And indeed I haven’t experienced anything earth shaking but altogether it was quite interesting and a good investment of time. I surely learned a few new things and meet interesting people there. I was astonished by the sheer number of attendees that gathered in there. I presume the old communist “castle” in the center of Warsaw the “Palace of Culture and Science” was the only place to accommodate this whole crowd, but it is not a cushy place. It’s big and cold and far from perfect to host a technology event like that. And well… despite the overwhelming size of this place, you almost have to eat you lunch on the floor, because there was no eno
ugh tables ;-)
But I passed the 70-543 Exam there and gained the Microsoft Certified Professional Developer in ASP.NET title. So altogether it was a full success!
What null smaller than 0? Adventures with nullable types in .NET
September 9, 2008 on 9:14 pm | In DotNet | No CommentsLets say we have two nullable integers like this:
int? i = null; int? j = 0;
Is null smaller than 0
Console.WriteLine(i < j);
False – no it is not.
So probably null is greater than 0
Console.WriteLine(i > j);
False – no it is not greater as well.
All right! So null is equal 0. It has to be, JIT has no other choice, right?
Console.WriteLine(i == j);
Well False too! This two little fellows are not equal too. What? Is it raining frogs and we about to experience Armageddon?
No! We have to use Nullable.Compare() and we will by back in normal world:
switch (Nullable.Compare(i, j)) { case -1: Console.WriteLine(”i < j“); break; case 1: Console.WriteLine(”i > j“); break; case 0: Console.WriteLine(”i == j“); break; }
0 is a little more than null. null is less than 0. null equals null and 0 equals 0.
Uff!
Enum Factory
August 10, 2008 on 9:16 pm | In DotNet | 2 CommentsI have a little tip for you. Let’s say you have an enumeration. You need a enumeration constant out of string variable. simply use System.Enum.Parse(). Here a small Snippet Compiler source code.
using System; using System.Collections.Generic; public class MyClass { public enum FooBar { Foo, Bar, FooBar }
public static void Main() { WL(FooBarFactory(”Foo“)); RL(); } public static FooBar FooBarFactory(string init) { try { return (FooBar)System.Enum.Parse(typeof(FooBar), init); } catch { // Do something throw; } } #region Helper methods private static void WL(object text, params object[] args) { Console.WriteLine(text.ToString(), args); } private static void RL() { Console.ReadLine(); } private static void Break() { System.Diagnostics.Debugger.Break(); } #endregion }
Silverlight for everybody
June 22, 2008 on 5:32 pm | In Silverlight, DotNet | No Comments
My fist text for a Polish computer magazine NEXT hast just been published. It is called “Silverlight for everybody”. New publisher new challenge. I hope for a long lasting collaboration. In a mean time you can check my GeoCodedCalcualtor. A simple piece of Silverlight app that calculates the degrees, minutes, seconds form of latitude, longitude notation to a numeric notation and shows the translated coordinates using Google Static Maps Api. Enjoy!
Remember to brake your egg at the small end*
June 9, 2008 on 9:53 pm | In Clarion, DotNet | No CommentsSometimes the interoperability between .NET and Win32 could by a pain in the ass. Especially in places you don’t expect it to be. I’ve worked recently on a cryptography algorithm. I had the source code in Clarion and all I had to do was to implement it in C#. No problem I guessed. But… The Clarion algorithm used pointers extensively. I have worked with byte arrays. I read the text as a char array and I copied the bytes into long variables using a binary shift. Lets say we have a
string s = “abcd“;
Its something like this:
If we try to interpret this as a uint variable:
uint ui = (uint)((s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]);
we will get something like this number 1633837924.
Lets look at this from the other side. Our string goes direct into memory:
Using Win32 compiler we want to read it to a ulong variable x from a px address. In Clarion the code to do this will look like this:
x &ulong
long px
x &= (px)
What do we get in x?
1684234849
Hmm, variable ui in C# and x in Wind32 are not the same. Why? Because Intel is using a small endian way to store the numbers. So it means that the highest byte is saved at the right side and the lowest at the left. Like this (small endian):
and not (big endian)
* In Jonathan Swift’s Gulliver’s Travels the Little Endians broke their eggs at the small end, where the Big Endians broke theirs at the large end. And the ones were not very fond of the others ;-)
Multiple projects - multiple repositories
April 26, 2008 on 9:00 pm | In SVN, DotNet | No CommentsDo you have one project you want to share between multiple solutions? Something like a big set of helpers or a framework you need here and there? There are multiple ways ones have achieved this goal. Let me describe how I’ve done this.
We work with Visual Studio as our main development environment and Subversion as our source control system. What we needed was a systematic approach where we share our framework across all of our projects. We are actively developing our framework so simple dll reference is not an option. Our rules according to framework development make it fairly save to share the same code across multiple projects and to develop at the same time.
We decided that the framework should by placed in separate SVN repository and a separate Visual Studio project. But we wanted the framework and the project being pullable from source control as a whole. Firstly because we are (I’m!) lazy and I don’t want to remember that I have to check out the framework and than the project separately. Secondly we have a living Continuous Integration system running on CruiseControl.NET and it should by able to detect the changes and build to project in one shot.
Our way is to add the framework as an existing project from separate solutions…
and to connect the main SVN project repository with framework repository as a svn:external.
Step by step solution:
1. Create “Framework” Project in Visual Studio
2. Create a SVN repository for this project (check everything you need in)
3. Create new solution in Visual Studio that will hold your “Project”
4. Create a SVN repository for this project
5. Create new folder on hard drive inside the “Project” folder
6. Check out the framework from repository into newly created folder
7. In Visual Studio add the framework to you solution as a existing project.
8. Link the Framework repository with the Project repository (using svn propset or with TortoiseSVN like this)
You are done!
Now if you check out the “Project” you will get the “Framework” automatically. Unfortunately if you will make changes into “Framework” you will not by able to checkc everything in. You will have to separately check the “Framework” changes and “Project” changes. But if you are using Tortoise SVN you will by warned.
Is is cool or not!?
Aiding your work with Visual Studio Code Snippets
April 16, 2008 on 7:35 pm | In DotNet | 2 CommentsWriting MSBuild Custom Task
March 13, 2008 on 8:16 pm | In MSBuild, Continuous Integration, DotNet | 3 CommentsScenario: we have Subversion server to manage our source code and a build server (CruiseControl.NET) to manage our deployment. We have decided to automatically set the SVN revision number to our assembly version.
[assembly: System.Reflection.AssemblyVersion(“1.2.3.0″)] [assembly: System.Reflection.AssemblyFileVersion(“1.2.3.0″)]
So we will replace the last 0 with the current Subversion revision number. How to do this? One way to achieve this is to modify the AssemblyInfo.cs and read the modified number from that exists. The file modification is easy with MSBuild Community Task FileUpdate
<FileUpdate Files=“Cic.P001001PropertiesAssemblyInfo.cs” Regex=“(d+).(d+).(d+).(d+)” ReplacementText=“$1.$2.$3.$(RevisionNumber)” />
But how to read it back? Well there is no easy way. I have written a custom MSBuidl task to achieve this like this:
<AssemblyInfoReader Path=“PropertiesAssemblyInfo.cs” Property=“AssemblyVersion“> <Output TaskParameter=“Value” ItemName=“ApplicationVersion” /> </AssemblyInfoReader>
Writing a custom MSBuild task is fairly easy. You have to Reference Microsoft.Build.Framework and Microsoft.Build.Utilities and implement Microsoft.Build.Framework.ITask. Just like this:
namespace Cic.MsBuildTasks { public class AssemblyInfoReader : Microsoft.Build.Framework.ITask { #region Private Varaibels private string path; private string property; private string value; #endregion #region Fields [Microsoft.Build.Framework.Required] public string Path { get { return path; } set { path = value; } } [Microsoft.Build.Framework.Required] public string Property { get { return property; } set { property = value; } } [Microsoft.Build.Framework.Output] public string Value { get { return this.value; } set { this.value = value; } } #endregion #region ITask Members private Microsoft.Build.Framework.IBuildEngine engine; Microsoft.Build.Framework.IBuildEngine Microsoft.Build.Framework.ITask.BuildEngine { get { return engine; } set { engine = value; } } bool Microsoft.Build.Framework.ITask.Execute() { string message; value = string.Empty; try { value = MyReadAssemblyInfoProperty(); message = string.Format( “AssemblyInfo property {0} read. Property value {1}”, property, value); } catch (System.Exception e) { message = string.Format( “Error reading AssemblyInfo property {0}. Error: {1}”, property, e.Message); } Microsoft.Build.Framework.BuildMessageEventArgs args = new Microsoft.Build.Framework.BuildMessageEventArgs( message, string.Empty, “AssemblyInfoReaderTask”, Microsoft.Build.Framework.MessageImportance.Normal); engine.LogMessageEvent(args); return true; } private Microsoft.Build.Framework.ITaskHost host; Microsoft.Build.Framework.ITaskHost Microsoft.Build.Framework.ITask.HostObject { get { return host; } set { host = value; } } #endregion #region Internals private string MyReadAssemblyInfoProperty() { string propertyValue; // Eraly return if (!System.IO.File.Exists(path)) return “”; foreach (string line in System.IO.File.ReadAllLines(path)) { if (line.Contains(property)) { try { propertyValue = line.Remove(0, line.IndexOf(‘”‘) + 1); propertyValue = propertyValue.Remove( propertyValue.LastIndexOf(‘”‘), propertyValue.Length - propertyValue.LastIndexOf(‘”‘)); // return matching property value return propertyValue; } catch { // Ignore errors } } } return string.Empty
