F*cking Programming

clock August 7, 2008 10:58 by author Sergi
Just found this (to me) incredibly funny post. Sorta gives you an insight into a programmer's mind on a high-stress/despair moment. I think you'll laugh, even if you're not a nerd :)


On books and old computers

clock March 27, 2008 14:40 by author Sergi

How many (computer) books can a nerd actually accumulate during, say, 5 years? Apparently a lot. And if you're into .NET, what with Microsoft trying to release versions 3.5 through to 7.2 by august this year, even more.

The thing is, what do you do with, for example, .NET 1.1 books?
The very thought of throwing a book away makes me start to hyperventilate, but can you give books about outdated technology away, even if it's to those who can't afford new books? I must say that I felt bad about it, as if saying: here, that's useless to me or pretty much anyone in the western world, so you can have it. You may go now.
A friend of mine from Ghana helped me overcome this nagging feeling and said he could give the books to a school in Ghana, no problem, so I did, but it still felt more like I was doing a favour to myself than helping somebody else.

Anyway, what do you guys do in such situations? And for that matter, what do you do with computers?

I tried contacting Computer Aid International about a more than decent computer that I hardly ever use,  but to my utter disappointment I never got a reply. Does anyone have a suggestion? Please bear in mind that I'm located in the Netherlands. I'm more than happy to send the computer to pretty much anywhere in Western Europe, but if I have to get is shipped to the states I might as well bring it myself to [Insert country in need]! ;)

kick it on DotNetKicks.com


WPF Bootcamp

clock January 21, 2008 07:52 by author Sergi

wpfbootcamp_3

Those of you that want to learn Windows Presentation Foundation, but are put back by the sheer mass most WPF books pack (I have a great one [non-affiliate link] that exerts some serious gravitational pull), should take a look at Windows Presentaiton Foundation Bootcamp, almost 20 hours worth of trainings, labs, demos, etc for free!

If that doesn't get you started I don't know what will ;)



Eternal Debate 2.0: Will the Web replace the Desktop?

clock January 17, 2008 18:53 by author Sergi

It started somewhere in the (very) late ’90, faded away in shame for a while after the Big Burst and, what do you know, it came back with a good dose of supervitamin A(jax) and superminerals provided by the advent of Web 2.0: The Web will make the Desktop a thing of the past. Again.

Now, I know I’m not touching a super hot issue here, but since I only ever read about it from known and sometimes even respected Web 2.0 developers/companies who have, naturally enough, an extremely biased view on the matter, I decided to approach it form the other side: The view of an anonymous Windows and Web developer who disagrees with them.

(Side Note: no Web 2.0 developers, respected or otherwise will be (really) damaged during this rant.)

First off, I don’t even know why such a debate should ever even arise. Twice. I don’t know what it is with the Web and internet, but It seems as though every new thing, every step forward, triggers some as of yet undiscovered testosterone reservoir in tech journalists’ brains that makes them claim that this time hell will really break loose, the end of the world as we know it! And god forbid you miss that train because it is the last one!

“Now departing: Last train out of Stoneageville! Last train! LAST train! Will you just hop in?!?”

What I consistently fail to understand is this need to confront these two environments that for all I know coexist in perfect harmony when left be.
Of course, there are some people out there, by which I mean Google, who would just totally love it if the Desktop, by which I mean Microsoft, would disappear. Mind you, that might be what drove them to revolutionise the web landscape with Google Maps in the first place.

Google Engineer #1: I know! Let’s get rid of Microsoft! And Apple! And Linux! Let’s get Rid Of The Desktop!
Google Engineer #2: Cool! But… er… how are we actually going to develop anything after that?
GE #1: Oh, don’t worry; we’re halfway through Google OS. Beta.
GE #2: What about BeOS?
GE #1: Shut up.

So, for the big one: Will the Web, the thin client, make the thick client a thing of the past? I think that’s a right pile of bullshit. Period.

Don’t get me wrong here, I am, by no means, criticising Web applications. I think there’s a bunch of really cool web apps out there, and many kudos to 37Signals, Gmail, Digg, Litmus (formerly SiteVista), and many others I’m forgetting, I just think people need to get a grip because some things are plain far fetched.

Someone, somewhere, even theorised (or proposed?) that the future operating systems would shrink immensely and become just a platform for running our highly evolved, super capable web browsers. Puh-lease! I think someone should try and leave his web browser alone from time to time.
And why is it always the “web” side of the story that has murderous thoughts about its fatter relative and not the other way around? For all I know the Desktop has always embraced the web; recognised the potential of cooperation. Per la Familgia.

I understand the underlying thought, that the new web applications help build an ever connected world where you data is accessible from anywhere, at any moment, by any Google employee.
But to say we’ll be going back to thin clients, back to terminal era...

- But why would you need anything else if your browser can provide everything you need and your data is stored in servers that are permanently available to you from anywhere?
- And what about confidential data? Stuff you’d rather keep an eye on at all times?
- Well, you could always save it on your Hard Drive, I suppose…
- On what file system?
- Er…

And that might be quite a simplistic rebate, I know, but so is their theory.
What seems obvious to me is that both environments, both worlds complement each other and are meant to share some space in our digital lives. Deal with it.



How To build a lightweight, flexible logging component in C#

clock September 11, 2007 20:03 by author Sergi

Ever found yourself writing quick and dirty logging classes -just to get out of the way- over and over again?
Ever wished you had a neat and decent logging mechanism you could reuse in your projects?  Search no more.

Before we start, you might want to download the code or download the binaries.

A short while ago, I was designing/building a relatively small system for a client and one of the key issues was reusability. Some parts would/might end up in a much bigger system so would I please take that into account when putting a design together.  Nothing new under the sun.

One of the modules was to be a logging module. The system was quite small, so Log4Net was out of the question. Still, I thought that the Log4Net concept was pretty neat (how's that for an understatement?) and tried to use some of the basic ideas for the client's logging module.
Of course, as it so often happens (right?) a big chunk of the small system got discarded and the rest was never used becuse other pressing matters took over and we all got new projects assigned. More business. Good.

Anyway, I still liked the idea of finishing the logging module so I took it home, rethought it and improved it and here I present it to you in a vitamin rich, highly digestible chunk:

How to build a lightweight, reusable and extensible logging component in C#.

Off we go, then

First off, what do I want the logging module -we'll call it LogManager- to be? I want it to be:

   - Simple
   - Unobtrusive
   - Reusable
   - Extensible: Must support all kinds of loggers (File loggers, console loggers, xml loggers, database loggers, ...)

Pretty common requirements, really. The only uhm... challenge I faced was, how can I make LogManager extensible and scalable without having to recompile the containing assembly every time I -or anyone else for that matter- wants to add a new type of logger?

Here's what I thought out:

image  
LogManager UML Diagram

The idea is that any instances of a concrete ILogger implementation are instantiated by means of LogManager.GetLogger(), which can instantiate any implementation of ILogger whether it resides in the LogManager assembly or not.

To the point, already!

Time to take look at some code, and we'll start with the ILogger interface. Nice and easy, just to make sure we warm up and stretch before we get to the weight lifting part.

public interface ILogger : IDisposable
{
    /// <summary>
    /// Determines the maximum log level.
    /// Example: If LogLevel.Warning, all warning, Info and debug messages will be logged.
    /// </summary>

    LogLevel LogLevel { get; set; }

    /// <summary>
    /// Logs an entry.
    /// </summary>
    /// <param name="text"></param>

    void Log(string text, LogLevel level);

    /// <summary>
    /// Logs an Exception.
    /// </summary>
    /// <param name="e"></param>

    void Log(Exception e);
}
ILogger Interface

The most remarkable thing about this bit of code is probably how nicely coloured it is, but to make sure we're leveled, just say that every class that implements ILogger will have to implement an overloaded Log method, one version for exceptions and another for any other message, and hold an instance of LogLevel implemented as a property.

Side Note: Please forgive me if I'm pointing out the obvious but I'm still geeting the hang of this kind of post. I'm just trying to make it accessible for every one (that would be public, wouldn't it?), beginners and advanced .NETters alike.

And so, without further dilation, we'll quite deliberately skip the LogLevel enumeration and move on to more interesting stuff: the LogManager.

Meet the Manager

LogManager is defined as a static class with only 2 properties and 1 overloaded method designed to instantiate and keep track of ILoggers.

The LogLevel property, unsurprisingly enough, sets the general level of messages to log -that is, when this property is set, all previously instantiated logs will start logging at the newly set level.

Then there's _activeLogs... _activeLogs begun, in my mind, as a single ILogger instance that would be set to the last-instantiated ILogger type. "Why would anybody want to be logging to 25 different kind of logs in one single app?" I thought. "Why wouldn't they?" I answered after a while. And it was in this dangerous fashion that I decided that _activeLogs had to be a List<ILogger>. And so it is.

Every time an ILogger instance is summoned into existence LogManager checks whether that concrete type of ILogger has been instantiated before. If so, it returns the existing instance. Otherwise it tries to create a new instance and returns it.

Time for code.

public static ILogger GetLogger(string logType, string initialization)
{
   Assembly logAssemby = Assembly.GetAssembly(typeof(LogManager));

   return GetLogger(logAssemby.GetName().Name, logType, initialization);
}
LogManager's GetLogger() Method - first overload
public static ILogger GetLogger(string assemblyName, string logType, string initialization)
{
   Type log = Type.GetType(assemblyName + "." + logType);

   // Avoid creating a new instance of an already active ILogger
   ILogger requestedLog = _activeLogs.Find(delegate(ILogger match)
   {
      return match.GetType() == log;
   });

   // Got it? return it.
   if (requestedLog != null)
      return requestedLog;

   // Determine wether the given type implements ILogger
   Type iLogger = log.GetInterface("ILogger");

   if (iLogger != null)
   {
      requestedLog = Activator.CreateInstance(log,
         BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic,
         object[] { initialization }, null) as ILogger;

      requestedLog.LogLevel = _level;
      _activeLogs.Add(requestedLog);
   }

   return requestedLog;
}
LogManager's GetLogger() Method - second overload

Assembly, assembly on the wall...

As the more alert of you -the ones actually reading the code instead of only watching the colours- might have noticed, LogManager relies heavily on reflection in order to return a valid ILogger instance.

The first overload is a no-brainer; it simply calls the second overload passing the name of the LogManager assembly as a parameter, and thus it can only be used to initialise ILoggers found in the LogManager assembly.

What the second overload does is: It looks in the ILogger list it maintains to see if the type of ILogger we're trying to instantiate has already been instantiated and if so, it simply returns the found instance.
If no instance is found it checks whether the given type implements the ILogger interface. If it does, the method attempts to create an instance of the given type passing it initialization as a constructor parameter. It finally adds the newly-instantiated ILogger to the _activeLogs list and returns the ILogger.

Over and out

And that's it really. I've actually used the code in a pre-production environment recently (yeah, yeah, I was feeding you untested code) and it performs quite nicely, so let me know of any success (or drama) stories if you decide to use it yourselves!

By the way, I had the decency to include an ILogger implementation with the code (FileLogger), so that it can be used right away.

Enjoy!

kick it on DotNetKicks.com


Welcome! Thanks for stopping by

clock September 1, 2007 17:38 by author Sergi

A blog. A company blog. Wel, well.

Does the world need another blog? Probably not. Probably, the world could just do with a handful of blogs, but there you go. Up to a billion and counting. So why bother starting a blog that might never have more than 2 readers -that would be me, and the cat, who writes the posts?

Only because every micro ISV needs one in order to survive? Well, partly. But to my utter surprise, I found out that I actually have some things to share. Useful things even. And so I managed to convice that cat and here we are, a newborn blog in the blogsphere.

Why should I ever come back?

Don't worry, I won't be writing about cats, family holidays or the likes.
This is a blog about development. About C#, .NET, ASP.NET... were you to end up here, you'll find tips, tricks, tutorials, (interesting) thoughts and occasionally a good 'ol rant about the (development/IT) world around us.

And so, with the hope (which springs eternal) that someday in the nearby future I'll see you regularly showing up for coffee and cookies in your particular journey through the blogsphere, I Welcome you again.

See ya soon.
Sergi Papaseit, founder of SharpEdge Software.