posted on Sunday, March 09, 2008 3:14 AM |

For my Twitter / Facebook sync app, after some good input from Scott Hanselman, has been shifted to a plug-in architecture. 

Seems easy right?  Not so much.  My ghetto, non-extendable way was so much easier and hack-ish.

Here is the LINQ for getting the assemblies.

statusUpdates =
    (from file in Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins"), "*.dll")
     let a = Assembly.LoadFrom(file)
     from t in a.GetTypes()
     where !t.IsAbstract && t.BaseType == typeof(StatusUpdate) 
     select (StatusUpdate)Activator.CreateInstance(t, credentials))
    .ToList();

Same code, but in traditional context.

string[] filesToTest = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins"), "*.dll");
foreach (string file in filesToTest)
{
    Assembly a = Assembly.LoadFrom(file);
    foreach (Type t in a.GetTypes())
    {
        if (!t.IsAbstract && t.BaseType == typeof(StatusUpdate))
        {
            statusUpdates.Add((StatusUpdate)Activator.CreateInstance(t, credentials));
        }
    }
}

So a few things I learned about doing this.

  1. You can't have the abstract in the same assembly you're loading.  I had the credentials as an internal var in the abstract and was lazy.  This little bit of laziness caused me a solid hour of trial and error getting this to work properly.
  2. If you create a plug-in system, your plug-in's damn well better work like everyone else's.  Right now there is a fatal flaw with the application's credentials.  I have no easy way for lets say, my mom, to update their user names and passwords.  This means credentials are compiled into the app which is no good.  No longer dynamic.
  3. I have to tweak the Facebook Developer Toolkit slightly, the login form shows when it really shouldn't.
  4. I need to get the beta of ReSharper since the non-c# 3.0 features underlines are pissing me off.
  5. I have to get the events to fire off asynchronously.

 

Also I did a prebuild and postbuild event for this project which made me feel like a sys-admin

Prebuild

if not exist "$(TargetDir)Plugins" mkdir "$(TargetDir)Plugins"

Postbuild

move "$(SolutionDir)StatusSync.Plugins\bin\$(ConfigurationName)\StatusSync.Plugins.*" "$(TargetDir)plugins"
move "$(SolutionDir)StatusSync.Plugins\bin\$(ConfigurationName)\Facebook.*" "$(TargetDir)plugins"

Tags [ Ramblings pictures ]

Your Comments.

  • # re: blog updates and what not

    From Jess Coburn @ Applied Innovation

    Hey Clint, glad to see you're making the move. All that PHP crap is going to run fine. In fact, your VPS will have Plesk 8.3 installed on it so not only will PHP run fine but you'll have the option to run PHP in FastCGI! You can actually run mysql on that VPS too but running all of that may be a bit too much of a memory hog.

    Left by Clint at 1/31/2008 11:03 AM
  • # re: blog updates and what not

    im actually excited that you are migrating from this crap environment to something useful.

    Left by konks at 2/2/2008 10:55 AM
Post Comment
Title *
Name *
Email
Url
Comment *  
Please add 3 and 8 and type the answer here: