Thursday, May 27, 2010

Why I hate Entity Framework (Version 1) – Importing stored procedures with scalar results

Lets start by writing the following simple store procedure example which returns count. Millions of those are used all over the web for paging:
 @ProductTypeID AS INT
SELECT COUNT(*) FROM Products WHERE ProductTypeID = @ProductTypeID
Now you should add this stored procedure to your data layer. Importing stored procedures into you project data layer is simple with Entity Framework, isn't it? If you import stored procedure which returns entity type as results then yes.
  1. update model to include your procedure
  2. make function import
  3. define result type
But what about stored procedures which returns scalars as results? (Now you are F***ED) Then you have the same steps as with previous procedure plus you should write additional code. You will have to extend your Entity Data Model to be able to use you procedure: (I use separate file: EntityDataModel.cs)
public partial class EntityDataModel: global::System.Data.Objects.ObjectContext
        private T ExecuteScalarFunction<T>(string functionName, DbParameter[] parameters)
            DbCommand cmd = ((EntityConnection)this.Connection).CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = this.DefaultContainerName + "." + functionName;
                if (cmd.Connection.State != ConnectionState.Open)
                var obj = cmd.ExecuteScalar();
                return (T)obj;
            catch (Exception ex)

        // And now your stored procedure
        public int sp_CountProducts(global::System.Nullable<int> regionId)
            EntityParameter ProductTypeIDParameter;
            ProductTypeIDParameter = new EntityParameter("ProductTypeID", DbType.Int32);
            if (regionId.HasValue)
                ProductTypeIDParameter.Value = regionId;

            return ExecuteScalarFunction<int>(
                new EntityParameter[]{ ProductTypeIDParameter });
Now you can use your procedure in the application code like this:
using (EntityDataModel context = new EntityDataModel(ConnectionString))
    RowCount = context.sp_CountProducts(productTypeId);
What is wrong with EF team? What are you drinking at work guys? Thanks GOD you fixed this stupid flaw in EF 4. (One tiny note: to be able to use EF 4 I will have to buy Visual Studio 2010. Isn't it wonderful?)

When you hear of an opportunity you want, you have to go for it completely

Excerpt from story:
  • being in the middle of things increases your chances for opportunities
  • everything great that happens in your career starts with someone you know
  • when you hear of an opportunity you want, you have to go for it completely, over-the-top, not casual, and work your ass off to get it
  • the tiniest detail can derail everything
Very good story about opportunity with moral read full story here

Thursday, May 20, 2010

Finally I contributed to Open Source

Finally I contributed to Open Source. One little bug report but it is a start.

P.S. "Do it F***ing Now" program really works!

Wednesday, May 19, 2010

Vidunaktį dažnai aš pabundu... (Vytautas Mačernis)

Vidunaktį dažnai
Aš pabundu,
Kada keistai, keistai
Visuos namuos tylu,

Ir aš nebežinau,
Kas daros su manim,
Bet man kaskart sunkiau
Tokiom naktim

Išspręst gyvybės ir mirties lygtis
Su begale nežinomųjų.
Veltui aš laukiu: niekas man nepasakys,

Atėjęs iš erdvių giliųjų,
Kodėl kas nors yra? Kodėl aš pats esu
Didžiausia paslaptis visatos slėpinių?

[Šarnelė, 1943.X.17]

Tuesday, May 11, 2010

Using GetVersionEx from C#

using System;
using System.Runtime.InteropServices;

namespace TestGetVersionEx
 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
 public struct OSVERSIONINFOEX
  public static int SizeOf
    return Marshal.SizeOf(typeof(OSVERSIONINFOEX));
  public uint dwOSVersionInfoSize;
  public uint dwMajorVersion;
  public uint dwMinorVersion;
  public uint dwBuildNumber;
  public uint dwPlatformId;     
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  public string szCSDVersion;
  public ushort wServicePackMajor;
  public ushort wServicePackMinor;
  public ushort wSuiteMask;
  public byte wProductType;
  public byte wReserved;

 class MainClass
  [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  public static extern bool GetVersionEx (ref OSVERSIONINFOEX lpVersionInfo);
  public static void Main (string[] args)  
   string OsInfo = string.Empty;
   info.dwOSVersionInfoSize = (uint)OSVERSIONINFOEX.SizeOf;
   if (OS.GetVersionEx (ref info)) 
    OsInfo = "\n1. Is Windows: " + PlatformDetection.IsWindows + 
    "\n2. dwOSVersionInfoSize: " + info.dwOSVersionInfoSize + 
    "\n3. dwMajorVersion: " + info.dwMajorVersion + 
    "\n4. dwMinorVersion: " + info.dwMinorVersion + 
    "\n5. dwBuildNumber: " + info.dwBuildNumber + 
    "\n6. dwPlatformId: " + info.dwPlatformId + 
    "\n7. wServicePackMajor: " + info.wServicePackMajor + 
    "\n8. wServicePackMinor: " + info.wServicePackMinor + 
    "\n9. wSuiteMask: " + info.wSuiteMask + 
    "\n10. wProductType: " + info.wProductType + 
    "\n11. wReserved: " + info.wReserved + 
    "\nszCSDVersion: " + info.szCSDVersion;
    Console.WriteLine (OsInfo);

Path to true happiness 'revealed' (link)

The 10 steps to happiness
  • Plant something and nurture it
  • Count your blessings - at least five - at the end of each day
  • Take time to talk - have an hour-long conversation with a loved one each week
  • Phone a friend whom you have not spoken to for a while and arrange to meet up
  • Give yourself a treat every day and take the time to really enjoy it
  • Have a good laugh at least once a day
  • Get physical - exercise for half an hour three times a week
  • Smile at and/or say hello to a stranger at least once each day
  • Cut your TV viewing by half
  • Spread some kindness - do a good turn for someone every day
(Read here more.)

Monday, May 10, 2010

Do it F***ing Now.

Do it F***ing Now. Don't wait. Don't procrastinate. The winners in this world are not the ones who find the greatest excuses to put off doing what they know will make them more money. The winners are the ones that prioritize and seize the day. Create a list of action items to make sure your important tasks get accomplished. Every project you're working on should be in action. If you're not moving, you're standing still. Your next step towards making money must not be "something I'll take care of maybe sometime next week." If it's going to help make you money: Do it F***ing Now. Some of you may think that you don't need the "f***ing" in "do it f***ing now". You do. You need that impact, that force, that call to action, that kick in the ass to get you moving. Otherwise, you'll end up another loser that had a great idea a long time ago but never did anything about it. Dreamers don't make money. Doers make money. And doers "Do it F***ing Now." Quote from this blog Read here