"The noblest pleasure is the joy of understanding"   (Leonardo da Vinci)
Website banner
Wikipedia Affiliate Button

Best of Japanese 80′es band Wands

June 4th, 2010

Trying out ScribeFire

February 13th, 2010

ScribeFire is a plugin for FireFox that facilitates adding entries to your blog from within FireFox. I am yet to find  out which other nice features it has, for instance if it can grab and rescale images from websites, or quote pages.

http://www.scribefire.com/help/firstrun/

Ny router

February 13th, 2010

Så fik jeg endelig taget mig sammen til at købe en ny wireless router. Valget faldt på en prisbillige N Home Router fra D-Link.

Den ser måske ikke ud af meget, men den har et væld af opsætningsmuligheder, og var forbløffende nem at få til at virke. De utallige routere jeg har haft i tidens løb har været enten meget svære at sætte op, eller har vist sig at være helt umulige at arbejde med. Senest en Huawei D100 der ikke har givet mig endet end grå hår!

Good article on the LINQ To SQL vs Entities

January 17th, 2010

Good article highlighting the advantages of both LINQ To SQL and LINQ To Entities.

http://msdn.microsoft.com/en-us/library/cc161164.aspx

Static DataContext? No-no

January 6th, 2010

It is necessary to reuse the DataContext object in an ASP.NET application, but the way to do it is a bit tricky. Especially if you have a multi-layered data model and you make several queries against the database.

The obvious - but wrong way - is to create a static instance of the DataContext somewhere in your business model. The reason this is wrong is that the static variable will remain there for successive calls to your web application. If something goes wrong in one request, for instance if an entity is updated but for some reason the SubmitChanges fails, then this failed object will still be in the DataContext when subsequent (or concurrent) requests to your site occur.

This blog has a nice approach to how the DataContext can be tied to the HttpContext.

http://www.west-wind.com/weblog/posts/246222.aspx

In particular the comment by Richard Deeming using an extension method looks promising.

Koch curve variant in C# using delegates and userPaintBox

December 27th, 2009

Why? Because I can. And because delegates are cute. :)

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Setup
            turtleBox.Reset();

            KochDelegate kochFunction = null;
            kochFunction = delegate(int angle, int step, int depth)
            {
                if (depth-- < 0) return;

                turtleBox.Turn(angle);
                kochFunction(-angle, step, depth);
                turtleBox.Move(step);
                turtleBox.Turn(-angle);
                kochFunction(angle, step, depth);
                turtleBox.Move(step);
                kochFunction(angle, step, depth);
                turtleBox.Turn(-angle);
                turtleBox.Move(step);
                kochFunction(-angle, step, depth);
                turtleBox.Turn(angle);
            };

            kochFunction(90, 4, 8);
        }
    }

    public class TurtleBox : PictureBox
    {
        private Pen pen = new Pen(Color.Black);
        private Graphics graphics;

        public void Reset()
        {
            Image = new Bitmap(Size.Width, Size.Height);
            graphics = Graphics.FromImage(Image);
            graphics.Clear(Color.White);

            graphics.ResetTransform();
            graphics.ScaleTransform(1, -1);
            graphics.TranslateTransform(0, -Size.Height + 1);
            Turn(-90);
        }

        public void Turn(float angle)
        {
            graphics.RotateTransform(angle);
        }

        public new void Move(int step)
        {
            graphics.DrawLine(pen, 0, 0, 0, step);
            graphics.TranslateTransform(0, step);
        }
    }

    delegate void KochDelegate(int angle, int step, int depth);

Tuddelum

Reposting… How to upgrade your 1.5 developer Android phone to 1.6

December 23rd, 2009

For those of you who, like me, have been visiting this site

http://developer.htc.com/adp.html

just to find out that the links to the official 1.6 firmware have “gone fishing”, here is a mirror that actually works.

Instructions for how to install the image can be found on the HTC website above.

Want to root your dev phone, but don’t know which custom ROM to download? Let me recommend CyanoGen. Read more here:

http://www.cyanogenmod.com/about

Happy modding!!

[C# Talk] : Query re-use in Linq To SQL

November 29th, 2009

Har du også overvejet om mulighederne for query re-use i Linq To SQL? Det vil sige det at man kan genbruge dele af sine Linq To SQL queries, ved at laegge dem ud som properties i sine
datamodel-klasser.

Det er noget man normalt ikke kan i Linq fordi hvis du laver en custom
property, og vil bruge den i en select statement, saa kan Linq ikke
finde ud af at lave den til en expression som kan omsaettes til SQL.

Men jeg har fundet en maade at goere det paa alligevel! Der er vist et
par andre gutter paa nettet der ogsaa har gjort det, men jeg synes
alligevel det var meget sjovt at lave min egen implementering.

Her er et simpelt eksempel. Lad os sige du har en entity class (fx en
klasse som er autogenereret af SQLMetal), vi kan jo kalde den Project.
Den har bla. to properties der hedder Income og Expenses. Lad os sige du
tit laver queries op mod den klasse hvor du gerne vil have Overskuddet
(revenue). Revenue er defineret som Income minus Expenses. Det var nu
oplagt at tilfoeje en ekstra property til Project klassen, saaledes:

public partial class Project
{
  public double Revenue
  {
    get { return Income - Expenses; }
  }
}

Denne property kan godt bruges til fx at populere felterne i en gridview
med, EFTER du har eksekveret din query, men du kan IKKE bruge den
direkte i queryen. Fx kan du ikke skrive:

var ds = from p in myDataContext.projects where p.Revenue > 0 select p;

Det vil komme ud med en fejlmeddelelse om at propertyen Revenue ikke har
nogen SQL-implementation.

Til redning kommer min nye extension method kaldet DynamicWhere. Hvis du
skifter Where ud med DynamicWhere, og skriver din Revenue property en
lille smule op, kan det lade sig goere.

Tricket er, at du skal lade Revenue levere en Expression<Func<>> tilbage
i stedet for et resultat. Men for at queryen skal kunne acceptere
at du skriver fx p.Revenue > xxx saa skal Revenue jo vaere en property
der leverer en double. I stedet laver du saa en statisk skygge-funktion
med et andet navn som leverer en Expression tilbage. Den funktion
associerer du saa med din Revenue funktion via en attribute. Saadan her:

[DynamicProperty(ExpressionPropertyName = "RevenueExpr")]
public double Revenue
{
  get { return RevenueExpr.Compile().Invoke(this); }
}

public static Expression<Func<Project, double>> RevenueExpr
{
  get { return (p => p.Income - p.Expenses); }
}

Jeg har ladet Revenue genbruge den associerede funktion saaledes at man
ikke bliver noed til at skrive p.Income - p.Expenses to gange.

Klik her for at downloade DynamicWhere som DLL projekt.

[C# Talk] : A smarter OrderBy<> and OrderByDescendnig<>

November 29th, 2009

Hvordan undgår man at blive nød til at kalde skiftevis OrderBy<> og OrderByDescendnig<> når man laver en custom sorting-algoritme i sit business lag i en Linq-baseret applikation? Man laver da naturligvis en extension method!

public static class ExtensionMethods
{
  public static IOrderedQueryable<TSource>
  OrderByDirection<TSource,TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, SortDirection sortDirection)
  {
    return sortDirection == SortDirection.Ascending ?
      source.OrderBy(keySelector) :
      source.OrderByDescending(keySelector);
  }
}

[C# Talk] : LALR(1) parser in C#

November 17th, 2009

Always being interested in parsers, I decided to try my luck on writing an LALR(1) parser in C#. Traditionally these parsers (Yacc, Bison) have been written so that you must run the scanner/lexer first, then include the output into your project as separate files.

I decided it was smarter if everything was assembled into one Parser class, which you could then instantiate in your program. I wanted to be able to configure it via a grammar also based on objects, build it, and then run the parser every time I wanted to parse a text string.

This is similar to the way the Reges class in C# works. You compile your regex first by instantiating a Regex class and give it the regex pattern, thereafter you can use it to match text strings multiple times.

Read more here…

[[[ Copyright (C) Thor Asmund 1998-2008 ]]]