Recent site activity

Davy in France‎ > ‎

Blog

Random stuff I've found out or written.

Code injection.

posted 17 Jan 2012 06:10 by Davy Jones

So I was told, "You can't use spring or any third party control".

  public static class AutoInjector
    {
        private static readonly object lockDictionary = new object();
        public static Dictionary<Type, Type> dictionary = new Dictionary<Type, Type>();

        public static void Register(Type interfaceType , Type concreteClass)
        {
            lock (lockDictionary)
            {
                if (dictionary.ContainsKey(interfaceType))
                {
                    throw new ArgumentOutOfRangeException(interfaceType.Name + " already exists in the dictionary");
                }
                else
                {
                    dictionary.Add(interfaceType, concreteClass);
                }
            }
        }


        public static T Get<T>()
        {
            return (T)Get(typeof(T));
        }

        private static object Get(Type interfaceType)
        {
            lock (lockDictionary)
            {
                if (!dictionary.ContainsKey(interfaceType))
                {
                    throw new ArgumentOutOfRangeException(interfaceType.Name + " does not exist in the dictionary");
                }

                Type t = dictionary[interfaceType];
                ConstructorInfo ci = t.GetConstructor(new Type[] { });
                object x = null;
                if (ci != null)
                {
                    x = ci.Invoke(new object[] { });
                }

                PropertyInfo[] pis = t.GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    if (dictionary.ContainsKey(pi.PropertyType))
                    {
                        pi.SetValue(x, Get(pi.PropertyType), null);
                    }
                }

                return x;

                throw new Exception("Auto Injector did not complete");
            }
        }
    }

Windows Script Component

posted 7 Nov 2011 06:59 by Davy Jones

An old technology, but a very useful one.

<?xml version="1.0"?>
<component>
   <registration  
    description="description"
    progid="someName.WSC"
    version="1.00"
    classid="{48474D70-4BC7-4c04-8346-28659D67FF82}"
    >
    </registration>
    <public>
        <method name="IsInstalled"/>
    </public>

    <script language="VBSCRIPT">
        <![CDATA[
            public function IsInstalled(name)
                IsInstalled = "File Not Found"
            end function
        ]]>
    </script>
</component>

More threading Fun.

posted 20 Jun 2011 08:04 by Davy Jones

Recently I had to use the Background worker to sync a UI from a threaded computational part.

Lots of background workers can be very very painful, so I subclassed it.

    public class Task<T, R> : BackgroundWorker
    {
        public delegate R RunDelegate(T args);

        public event EventHandler<TaskCompletedEventArgs> TaskCompleted;

        public void Run(T args, RunDelegate codeToRun)
        {
            this.RunWorkerCompleted += (s, evt) =>
            {
                OnTaskCompleted(evt);
            };

            this.DoWork += (s, evt) =>
            {
                evt.Result = (R)codeToRun((T)evt.Argument);
            };

            this.RunWorkerAsync(args);
        }

        protected void OnTaskCompleted(RunWorkerCompletedEventArgs e)
        {
            EventHandler<TaskCompletedEventArgs> handler = TaskCompleted;
            if (handler != null)
            {
                handler(this, new Task<T, R>.TaskCompletedEventArgs(e));
            }
        }

        public class TaskCompletedEventArgs : EventArgs
        {
            public TaskCompletedEventArgs(RunWorkerCompletedEventArgs e)
            {
                Result = (R)e.Result;
                Error = e.Error;
                Cancelled = e.Cancelled;
            }

            public R Result { get; set; }

            public Exception Error { get; set; }

            public bool Cancelled { get; set; }
        }

iPad2 - Two week review.

posted 6 Jun 2011 05:36 by Davy Jones   [ updated 6 Jun 2011 05:57 ]

Two weeks ago I bought an iPad2. 16gb 3g wifi.

I'm pretty impressed by the applications, the navigation and the battery life. Good playback for films, games are fun to tap a screen and for the application I bought it for Amplitube I'm in love with it, really fun to play around with all the effects on my guitar without spending 150-300 euros for an effect that I might not want. 8 - 10 effects > price of ipad.

But, and this is a big point,  it is NOT a replacement for a laptop or a desktop computer, it's little more than a big iPhone without the telephone calls / sms.

and now the bad points.

iTunes,  what a pile of crap that is, trying to copy music to the iPad is a complete nightmare, you can't display folders or artists if their id3 tag isn't set, I spent years organising my play lists into folders for the album in a folder for the artist, with the correct name for the mp3.  And iTunes can't read that! Add to that the fact that half of my music doesn't show up at all. I thought apple was all about Ergonomics and ease of use.

iOS 4.3  after applying the update, many apps crash for no apparent reason, I have to go in and manually kill all the applications and then reopen the app from the start again. This is a pain in the ass and needs fixing.

Documentation,  what documentation!  there is nothing in the box, only a little thing that says  install iTunes, give over your credit card to jobs, then sync and charge. Where does it tell you about gestures / button, etc?  I had to go ask an iPhone user how to use my new toy.

Overall,

I'm impressed, I bought the iPad2 for two reasons,
 1. replace my broken eeepc
 2. amplitube 

If I can find a way to use the iPad without having to have the abortion that is iTunes on my computer I would like it a hell of a lot more.

Davy.



Using Twitter Atom Search Api.

posted 24 May 2011 06:40 by Davy Jones

I subscribe to certain words around Paris using the below link.

http://search.twitter.com/search.atom?geocode=48.866666%2C2.333333%2C10km&q=

where the text after the q= is the search you want to perform.

ie to keep up todate with RATP,   do

http://search.twitter.com/search.atom?geocode=48.866666%2C2.333333%2C10km&q=ratp

And this will only return tweets around paris for the Ratp,

change the geo code to around your city.

Dj,


Visual Studio Macros

posted 30 Mar 2011 05:17 by Davy Jones

I've been messing around in VS macros for a while now but one thing I always wanted to do but couldn't find help with was.

How do you run a macro after a Build?

And the answer is very very simple.

Open macro explorer,

right click you macro project and select edit.

on the left hand side is the project explorer..

open up EnvironmentEvents

in the top left drop down select the event group you want to trap,  ie BuildEvents

then in the top right drop down select the event you want to do.


Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone

Contrary to all documentation and blogs that I've seen on the net, this method ONLY works in the EnvironmentEvents module.

So here's my build event.

Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone

        DTE.ExecuteCommand("View.ErrorList")
        Dim er As ErrorList = DTE.ActiveWindow.Object

        If Action = vsBuildAction.vsBuildActionBuild Then
            If er IsNot Nothing AndAlso er.ErrorItems.Count = 0 Then
                DTE.ExecuteCommand("Tools.RunStyleCop")
            End If
        End If
    End Sub




Messing around with Localized strings

posted 2 Feb 2011 08:53 by Davy Jones

I've had some free time this week so I decided to have a look at how to check that all strings have been localized.

public void ValidateTranslations(string[] supportedCultures)
        {
            CultureInfo ci = Thread.CurrentThread.CurrentUICulture;
            Type t = typeof(Properties.Resources);
            PropertyInfo[] pis = t.GetProperties(BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
            foreach (PropertyInfo pi in pis)
            {
                if (pi.PropertyType == typeof(string))
                {
                    ResourceManager rm = new ResourceManager(typeof(Properties.Resources));
                    string reference = rm.GetString(pi.Name, ci);
                    foreach (string culture in supportedCultures)
                    {
                        string compare = rm.GetString(pi.Name, CultureInfo.GetCultureInfo(culture));
                        if (string.IsNullOrEmpty(compare) || reference == compare)
                        {
                            throw new TranslatedStringException(CultureInfo.GetCultureInfo(culture), pi.Name, typeof(Properties.Resources));
                        }
                    }
                }
            }
}

I'm assuming that all the ressources are written in english and the development environment is in english, this can be changed by setting
CultureInfo ci = CultureInfo.GetCultureInfo("en");    
or to what ever language you write strings in.

It then uses your string resources in the Properties.Resources class to get the string in the default language and then for each of the supported cultures passed.

You could yield a not translated result if you needed, but this version I use in a unit test, so that I know if I add a string I need to add the translations into the other strings.

The resulting XML files with Google / Bing translated text in them can be given to a professional along with the orignial english version for correction..




Removed

posted 16 Dec 2010 03:25 by Davy Jones   [ updated 25 Apr 2012 02:33 ]




Clearcase Rant again.

posted 21 Oct 2010 02:53 by Davy Jones

I want to go back to a version of the code that I have used in the past, can I figure out how to do it.. can I f'ck.   So freaking hard to do stuff in clearcase I waste half a day trying to do something that I can do in seconds with any other source control.

#Temp table problem truncating data.

posted 15 Oct 2010 03:00 by Davy Jones

I recently came across this problem in a database I'm working on.

The original developer just didn't grasp what he was doing.

select 1 as Id, null as value, 1.3 as value2 into #myTemp
 insert into #myTemp (id,value,value2) values (2 , null , 1.3 )
 
 update #myTemp set value = 1.5 where id = 2
 
 select * from #mytemp

The results from select * from #myTemp are

ID    Value    Value2
1      Null        1.3
2        1            1.3

because the table was created with a select into statement,  the NULL in the creation is defined in the table as an INT.
Thus automatic truncation of decimals inserted into it.






1-10 of 39