Jun082011

Stick With ViewContext.Writer When Creating HtmlHelper

Published by anton at 5:55 AM under Programming

I just realized that Razor has different processing order.

It is common that we use htmlHelper.ViewContext.HttpContext.Response for rendering output via ASP.NET MVC’s HtmlHelper. We can also use htmlHelper.ViewContext.Writer to achieve the same result.

This is not the case with ASP.NET MVC 3’s new Razor view engine. If we use Response object directly, the output will be rendered in the top of the page. Using ViewContext.Writer will solve this issue.

Tags:

0 Responses

May232011

InnerHTML With Form Tag In IE

Published by anton at 9:12 PM under Programming

Today I was stucked with strange bug, the content of div showed nothing in IE when I tried to populate it with Ajax result. After I spent 20 minutes (or perhaps more ) of JavaScript debugging, I noticed that nothing wrong with that part of script, even I added exception handling though it wasn’t neccesary.

Somehow I just realized that the Ajax result contains form tag. This bring back old memories, I experienced similar problem long time ago. I ensured that this was the problem via googling.

The solution is creating other element (I use div), and populate the Ajax result inside that new element, then attach the newly created element to existing container.

var div = document.createElement('div'); 
div.innerHTML = ajaxResult;
divParent.appendChild(div); 

While the solution is really simple, this is enough to annoy me.

Tags:

0 Responses

Mar112011

I Haven’t Been A Phytonista … Yet

Published by admin at 8:54 PM under Programming

Apparently our office has been infected by Phyton, most of us are learning Phyton right now. I’m not sure how long I can ignore Phyton, I’m still a C# believer. But since Phyton is free and it has good performance, I won’t rule out studying it later.

Tags:

0 Responses

Feb192011

Level Up: Senior Programmer

Published by anton at 2:43 PM under Programming

Yesterday Scott Hanselman posted a list of senior programmer’s interview questions. Have been working as a software developer for 3.5 years, I’ve known most of them. I guess now I can call myself “senior .NET programmer” Smile.

Tags:

0 Responses

Feb152011

Ambiguous Match Exception In My Unit Tests

Published by anton at 3:17 PM under Programming

I was frustrated with My VS 2010 and its XUnit plugin. The XUnit test runner will keep running when I test using Test Driven.NET, so I always need to close my VS 2010.

Fortunately, today I tried the XUnit test runner (xunit.gui.exe) directly, and I found a detailed information about the error.

Tags:

0 Responses

Dec062010

NHibernate 3.0 General Availability

Published by anton at 3:03 PM under Programming

Yesterday (4 Dec 2010) , NHibernate 3.0 was released.

This new version targets .NET 3.5, so the new API supports lambda expression now.

For configuration, there are extension methods, that allowed us to ensure type safety.

Below is the example:

 

var nhConfig = new Configuration()
.Proxy(proxy =>
proxy.ProxyFactoryFactory<ProxyFactoryFactory>())
.DataBaseIntegration(db =>
{
db.Dialect<MsSql2008Dialect>();
db.ConnectionStringName = "MyConn";
db.BatchSize = 60;
})
.AddAssembly("MyProject.Model");
var sessionFactory = nhConfig.BuildSessionFactory();

Now NHibernate supports QueryOver method, which is new fluent syntax for criteria queries.

Previously we often use criteria query like this

var user=session.CreateCriteria<User>()
.Add(Restrictions.Eq("Email",emailToMatch))
.UniqueResult<User>();

The property is written in string, so if there is typo error, we won’t detect it during compile time. This problem is solved with QueryOver.

var user=session.QueryOver<User>()
.Where(u=>u.Email==emailToMatch)
.SingleOrDefault();

You may notice that the QueryOver syntax is similar to LINQ provider. However NHibernate has its own LINQ provider in the namespace NHibernate.Linq (which is integrated in the main assembly, NHibernate.dll). 

var user= session.Query<User>()
    .Where(s => s.Email==emailToMatch)
    .FirstOrDefault();

var user=(from u in session.Query<User>()
    where u.Email==emailToMatch select u)
    .FirstOrDefault();

Note that I’m new to NHibernate, and I ‘m learning it from Jason Dentler’s NHibernate 3.0 cookbook, which is a good reference book. I bought the book along with ebook version from Packtpub website directly last month (somehow it is cheaper than Amazon) and the coupon is NHIBCBK20 . Until now I haven’t received the book (which is usual because the post here is slow and I don’t use express service) but I can download the ebook version directly from their website.

You can download NHibernate 3.0 here

Tags:

0 Responses

Oct262010

PublicKey Error Of .NET Framework 3.5 SP 1 Bootstrapper

Published by anton at 11:42 PM under Programming

I got this suspicious error when compiling with Microsoft .NET 3.5 SP 1 set as a prerequisite in Visual Studio 2008 SP 1.

The value of the 'PublicKey' attribute in '.NET Framework 3.5 SP1' does not match that of file 'C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFX35SP1\dotNetFX30\XPSEPSC-x86-en-US.exe'

The bootstrapper itself was extracted from DotNetFX35SP1.exe because there weren’t any bootstrapper for .net 3.5 sp1 in my office pc previously.

Apparently, I need to change the public key and add package name as described here. Thanks to my colleague for pointing this.

Tags:

0 Responses

Oct252010

Installer Development = Frustrating

Published by anton at 5:38 PM under Programming

Now I’m frustrated with this installer development.

One thing that pushes my patience to its limit is the fact that to test an installer, I need to wait it for quite a long time. Sometimes I need to format OS, restore it to previous state. And the installer also includes heavyweight prerequisites, such as .NET Framework 3.5, Win Installer 4.5 and SQL Server 2008. To install them all it will need 2 hours+ in guest OS. Yes, I also need to try different Windows OS including Server (since 2003) and the 64 bit version.

If it fails, then normally I will test the failed part first, and then I retry the whole installation process.

Tags:

0 Responses

Oct232010

Reboot And Continue The Installation Process

Published by anton at 2:37 PM under Programming

I was in a situation in which the installer needs to reboot and then continue the installation after restart.

Basically what we need to do are:

  • Ensure you save the state before reboot. In this case I save it to registry
  • Save the installer name (as key) and path (as value) to HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
  • After restart, read the state and continue the installation.

I attached the example in NSIS here

Tags: ,

0 Responses

Sep012010

Ensure You Use The Release Version Of Library

Published by Anton at 2:56 PM under Programming

Yesterday I had an interesting experience, the web that we develop  suddenly became not responding. We predicted that this is because either database locking or too may request.

It turned out that an assertion from 3rd party library (PdfSharf) failed! So we need to click OK in web server pc first, then web continued as usual.

Honestly, I’ve never used Debug.Assert to check the state of my code, so this behavior surprises me.

It is a good practice to use the release version of 3rd party library.

Tags:

0 Responses