27 April 2007

BrainStorm Rocks

Yesterday was a great day. My team called BrainStorm won the local round of Microsoft's Imagine Cup 2007 competition in the area of software design.
Our project with the code name Bookvar will compete in the finals this year in Seoul, Korea. This was possible, because of the remarkable team I'm part of. Right now I can't find appropriate words to describe them, but as one of the jury said: "I had the chance to watch these young motivated and smart people". It's a great pleasure to work with each one of them.
Although we managed to win the local round, there is still much work to be done. Lucky for us, the finals are after 3 months.
Check out this post from Ruslan Trifonov if you want to see some pictures from the event. Ruslan was member of the jury.

13 March 2007

Grand Prix Racing Online

Last week I was chatting with an old friend of mine - Vladimir Alexandrov. I was surprised to find out that he is driving the Grand Prix Racing Online site. Basically this is a F1 on-line simulator. If you are a F1 fan you will appreciate what Vladimir and his team is doing. The site is completely free and guess what - no advertisements are present.
There are some interesting issues about the implementation and hosting, but because of security I can not share them.
Good luck Vlado, and continue with the great job.

12 March 2007

NHibernate and triggers

Recently I was asked from a college of mine, about the following exception in application based on SQL Server 2005 and NHibernate :
[NHibernate.AdoNet.TooManyRowsAffectedException] {"Unexpected row count: 2; expected: 1"} NHibernate.AdoNet.TooManyRowsAffectedException

After a quick investigation the problem was obvious - the exception raises when a row from a table is updated and the row has fired a trigger which executes an update statement.
I have faced with this problem since early beta of NHibernate (0.8.4 as far as I can remember). Till now the only solution I have found is to use SET NOCOUNT to stop the message that shows the number of rows affected by a Transact-SQL statement from being returned as part of the results.
Just place SET NOCOUNT ON before the UPDATE statement inside the trigger and then after the UPDATE statement place SET NOCOUNT OFF. This will solve the problem.

ASP.NET Web Part editor in pop-up dialog

Last hour I was wondering how to host a ASP.NET Web Part editor control inside a pop-up dialog. I was lucky to find this implementation. It is based on some tricks, but resolves the problem.

20 December 2006

First Bulgarian .NET Book is here

Second volume from the first Bulgarian book about .NET is out. You can download free copy from here. Printed version will be available after a month.

17 November 2006

Windows Forms data binding and notification of changed properties

Yesterday a college of mine asked me a question: "How to refresh TextBox control in Windows forms application binded to a property of object, when the property change its value?". The solution is very simple, but according to me not very well documented (at least in books I read).


Solution: Provide event named PropertyNameChanged, where PropertyName is the name of property binded to the TextBox. During the binding process, the text box inspects binded object for event with the mentioned signature and binds to it. The sample code looks like this:


public class Customer
{
    private string mName;
 
    public string Name
    {
        get { return mName; }
        set
        {
            mName = value;
            if (NameChanged != null)
            {
                NameChanged(this, EventArgs.Empty);
            }
        }
    }
 
    public event EventHandler NameChanged;
}
 
Customer cust = new Customer();
textBox1.DataBindings.Add("Text", cust, "Name");
 
Another possible solution is to remove and add binding of the text box to the object. You can write general routine to loop through each control for a given form and remove-add binding. This is nasty solution of course.

11 September 2006

Cool conference in Bulgaria

DevReach - one of the most exiting conferences about Microsoft technologies in Bulgaria is coming. According to the program, some of the world leading industry speakers will present quite interesting lectures. Hope we will meet there.

Google toolbar 4

This morning I was amazed! A few minutes after turning on my PC a small balloon appeared. The message in the balloon was something about Google toolbar update. Who cares, was my first reaction. But when the update was finished I was surprised to learn that LONG WAITING FEATURES LIKE ACCESSING BOOKMARKS FROM ANY COMPUTER are already present. Believe me - this is like a elixir of life for me. I'm using one PC at work, one PC at home and one laptop. Managing bookmarks in situation like this is almost nightmare. In the past there were some other decision of this problem, but none of them was so elegant as Google toolbar. BTW, Microsoft are developing the same service, but as far as I know this feature is still in beta.
The other features I like are SpellCheck and Translate. If you are inspired, install the toolbar from here for WinXP and IE. Enjoy!

04 September 2006

ADO.NET Entity Framework ContextObject and ASP.NET application

Last month Microsoft published first CTP of ADO.NET Entity Framework. For more info - look here. Although Microsoft is still planning integration between Entity Framework and ASP.NET, I want to show you how to handle ADO.NET Entity Framewrok context object in ASP.NET application. This post is inspired from NHibernate session management.
The idea is simple: Context object will be stored in ASP.NET session and after the request HTTPModule will free it. I'm using ASP.NET session instead of ASP.NET context to further extend this example to support session per application transaction strategy. If you are wondering what is "session per application transaction" take a look at NHibernate session management. Here is the code of the implementation:

public class ContextManager<T>where T: ObjectContext, new()
{
    public static T ObjectContext
    {
        get
        {
            T resultContext;
 
            if (HttpContext.Current.Items.Contains(
                ContextConst.CONTEXT_OBJECT_SESSION_KEY))
            {
                resultContext =
                    (T)HttpContext.Current.Items[
                        ContextConst.CONTEXT_OBJECT_SESSION_KEY];
            }
            else
            {
                resultContext = new T();
                HttpContext.Current.Items[ContextConst.CONTEXT_OBJECT_SESSION_KEY] = 
                    resultContext;
            }
 
            return resultContext;
        }
    }
 
    public static bool isActiveObjectContext
    {
        get
        { 
            return HttpContext.Current.Items.Contains(
                ContextConst.CONTEXT_OBJECT_SESSION_KEY);
        }
    }
 }

public class ContextHttpModule : IHttpModule 
{
    #region IHttpModule Members
 
    public void Init(HttpApplication context)
    {
        context.EndRequest += new EventHandler(OnEndRequest);
    }
 
    public void Dispose()
    {
        //Intentionaly not implemented
    }
 
    public void OnEndRequest(Object sender, EventArgs e)
    {
        if (HttpContext.Current.Items.Contains(ContextConst.CONTEXT_OBJECT_SESSION_KEY))
        {
            ObjectContext contextObject = 
                (ObjectContext)HttpContext.Current.Items[
                    ContextConst.CONTEXT_OBJECT_SESSION_KEY];
 
            contextObject.Dispose();
        }
    }
 
    #endregion
}


internal class ContextConst
{
    public const string CONTEXT_OBJECT_SESSION_KEY =
        "ContextConst.CONTEXT_OBJECT_SESSION_KEY";
}

Now, lets look how the code above can be used. First lets make some corrections to web.config file:

<connectionStrings>
  <add name="Duwamish7Model.Duwamish7" 
       connectionString="metadata=C:\Temp\ContextObject\Model;
          mapping=C:\Temp\ContextObject\Model;
          provider=System.Data.SqlClient;
          provider connection string=&quot;Data Source=ns-server;
          Initial Catalog=Duwamish7;Integrated Security=True&quot;"
    providerName="System.Data.Mapping" />
</connectionStrings>


<httpModules>
  <add name="ContextObjectManager"
       type="ObjectContextManager.ContextHttpModule, ObjectContextManager"/>
</httpModules>
Having all of the above done, we can write somethig like this:

Duwamish7 db = ContextManager<Duwamish7>.ObjectContext;
 
Query<Authors> query = db.GetQuery<Authors>(
    "SELECT VALUE a FROM Authors AS a WHERE a.PKId > 144");
 
Repeater1.DataSource = query;
Repeater1.DataBind();


P.S. The code above is not tested for concurrency problems.

03 September 2006

WCF and WS Eventing

WCF (aka Indigo) is the latest communication technology from Microsoft (still in beta). Although there are a lot of goodies in this technology, something really useful for me is missing - implementation of WS Eventing. If you are wondering why - this article gives the answer. Fortunately there is already non-Microsoft implementation. Read this article if you are interested.

24 August 2006

Finally it is true!

Yesterday, PFC Levski Sofia did it. The team had succeeded to qualify for the UEFA champions league group stage for the first time in Bulgarian football history. Although I'm fan of the CSKA Sofia the success of Levski makes me happy. More info - here.