TCP Port ReRouter: v2.0.0 Released (Open Source)

by Jon Davis 23. July 2010 19:20

About 6 years or so ago I wrote a heavily pirated little Windows app called TCP Port ReRouter that I tried to sell for a few bucks. It was written in Visual Basic 6.0 using the Winsock ActiveX control and enabled someone to basically redirect all TCP traffic (such as web traffic) coming in on one IP address and port to another IP address and port. It was a needed utility because most utilities I found on the Internet didn’t allow you to modify the port.

I’ve been meaning to rewrite that thing ever since I learned and adopted C#, I just never got around to it. I did have a couple false starts but never got past the sockets learning curve. Until now.

I spent the last two or three days writing the same functionality in C#. Thanks to improvements made to .NET, the implementation turned out to be extremely simple. It’s a basic byte-array-to-byte-array propagation of received socket data to and from a configured routed host and a connecting client.

Here it is, open-sourced, fresh from my laptop to your whatever. http://portrerouter.codeplex.com/ Please feel free to scroll down to the bottom and buy me a coffee. :)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Independent Azure - Now That's More Like It

by Jon Davis 17. July 2010 07:34
Over at http://www.developerfusion.com/news/84635/azure-coming-to-a-datacenter-near-you/ it seems that Microsoft is letting Azure run out of its own physical cloud. That's better. Now I can start paying attention to this thing called Azure. Not knowing if Azure was ever going to be hostable independently from Microsoft's hardware cloud, I had absolutely no reason to care about Azure, and frankly it made me angry that they kept touting Azure as an essential development skill if it was all about their stinkin cloud. But if we can host it ourselves, now I'm going to listen.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

How To Reference A Property By Name Without Using String Constants

by Jon Davis 13. June 2010 02:39

I’m working on some code here where I’m loading an entity using a dictionary. I’m not using .NET 4.0 so I’m not going to get to use the dynamic keyword and its IDictionary implementation of properties reflection.

My original code looked something sort of like this:

public void Populate(IDictionary<string, object> values) 
{
	foreach (var kvp in values) {
		var key = kvp.Key;
		var value = kvp.Value;
		switch (key) 
		{
			case "FirstName":
				this.FirstName = value;
				break;
			case "LastName":
				this.LastName = value;
				break;
			// and so on and so forth
		}
	}
}

This looks sloppy. First of all, I don’t want to have to declare each and every property again for its setter just to support IDictionary. Secondly, what if I rename my properties? Then I’d have to go back and update this switch…case statement again. I want to use a strongly-typed property reference, so that if I rename the property I can use the IDE’s built-in refactoring functionality with which all uses of that property are automatically updated. I don’t get that benefit if I’m using string constants.

I’d come across some interesting tutorials in the past that addressed this problem, and I thought I’d give it my own go based on their insight. My revised solution is not by any means original. (Nothing I ever do is, I suppose.)

To make a long story short, I flexed some recently learned LINQ expression muscle on a member evaluator expression to obtain the strongly typed property’s name. In my implementation (not entirely shown here) I actually do need to verify the property by name because I have a bunch of setter logic that I don’t expose here, such as replacing the value being an IEnumerable<string> with logic that clears an ObservableCollection<string> and populates it with the value. Anyway, here’s the basic flow:

public override void Populate(IDictionary<string, object> values)
{
	foreach (var kvp in values) {
		if (key == base.ResolvePropertyName(() => this.SpecialProperty))
		{
			// this.SpecialProperty = value;
            // do something special with setter here
		}
		else if (key == base.ResolvePropertyName(() => this.AnotherSpecialProperty))
		{
			// this.AnotherSpecialProperty = value;
            // do something special with setter here
		}
		else 
		{
			base.Populate(key, value);
		}
	}
}

/// and in the base class ....

/// <summary>
/// Returns the name of the property. Syntax:
/// <code>
/// var propertyName = ResolvePropertyName(() =&gt; this.MyProperty)
/// </code>
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
protected string ResolvePropertyName(Expression<Func<T>> e)
{
    if (e.NodeType == ExpressionType.Lambda)
    {
        return ResolvePropertyName(((LambdaExpression)e).Body);
    }
    if (e.NodeType == ExpressionType.MemberAccess)
    {
        return ((MemberExpression)e).Member.Name;
    }
    throw new InvalidOperationException("Given expression is not type MemberAccess.");
}

private static Dictionary<Type, List<PropertyInfo>> TypeProperties
    = new Dictionary<Type, List<PropertyInfo>>();
/// <summary>
/// Populates a property of the current entity with the the given key/value pair.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void Populate(string property, object value)
{
    var t = this.GetType();
    List<PropertyInfo> pis;
    if (!TypeProperties.ContainsKey(t))
    {
        TypeProperties[t] = t.GetProperties().ToList();
    }
    pis = TypeProperties[t];
    var pi = pis.Find(pif => pif.Name == property);
    if (pi == null) throw new KeyNotFoundException("Property does not exist: \"" + property + "\"");
    pi.SetValue(this, value, null);
}

The Populate(key,value) method is basic .NET 2.0 reflection with some sorta-kinda basic caching.  The method above it is using LINQ expressions to extract the member access invocation as expressed in the LINQ statement.

I wrote a unit test to invoke this and stepped through it with the debugger. Seems to work fine.

After further tweaking, I migrated the base class functions to a utility class so that these functions can be used with anything, not just with objects that inherit a common base class. I also improved the reflection caching of Populate() so that the properties are properly hashed on their property names.

public static class ObjectUtility
{

    /// <summary>
    /// Returns the name of the property. Syntax:
    /// <code>
    /// var propertyName = obj.ResolvePropertyName(() =&gt; obj.MyProperty)
    /// </code>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="obj"></param>
    /// <param name="expr"></param>
    /// <returns></returns>
    public static string ResolvePropertyName<T>(this object obj, Expression<Func<T>> expr)
    {
        return ResolvePropertyName(expr);
    }

    /// <summary>
    /// Returns the name of the property. Syntax:
    /// <code>
    /// var propertyName = ObjectUtility.ResolvePropertyName(() =&gt; obj.MyProperty)
    /// </code>
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    public static string ResolvePropertyName(Expression e)
    {
        if (e.NodeType == ExpressionType.Lambda)
        {
            return ResolvePropertyName(((LambdaExpression)e).Body);
        }
        if (e.NodeType == ExpressionType.MemberAccess)
        {
            return ((MemberExpression)e).Member.Name;
        }
        throw new InvalidOperationException("Given expression is not type MemberAccess.");
    }

    private static Dictionary<Type, Dictionary<string, PropertyInfo>> TypeProperties
        = new Dictionary<Type, Dictionary<string, PropertyInfo>>();
    /// <summary>
    /// Populates a property of an object with the the given key/value pair.
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="property"></param>
    /// <param name="value"></param>
    public static void Populate(this object obj, string property, object value)
    {
        var t = obj.GetType();
        if (!TypeProperties.ContainsKey(t))
        {
            TypeProperties[t] = new Dictionary<string, PropertyInfo>();
            var lst = t.GetProperties().ToList();
            foreach (var item in lst)
            {
                if (!TypeProperties[t].ContainsKey(item.Name))
                {
                    TypeProperties[t][item.Name] = item;
                }
            }
        }
        var pisdic = TypeProperties[t];
        var pi = pisdic.ContainsKey(property) ? pisdic[property] : null;
        if (pi == null) throw new KeyNotFoundException("Property does not exist: \"" + property + "\"");
        pi.SetValue(obj, value, null);
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

C#

Gemli.Data: Basic LINQ Support

by Jon Davis 6. June 2010 04:13

In the Development branch of Gemli.Data, I finally got around to adding some very, very basic LINQ support. The following test scenarios currently seem to function correctly:

var myCustomEntityQuery = new DataModelQuery<DataModel<MockObject>>();

// Scenarios 1-3: Where() lambda, boolean operator, method exec
var linqq = myCustomEntityQuery.Where(mo => mo.Entity.MockStringValue == "dah");
    linqq = myCustomEntityQuery.Where(mo => mo.Entity.MockStringValue != "dah");
    // In the following scenario, GetPropertyValueByColumnName() is an explicitly supported method
    linqq = myCustomEntityQuery.Where(mo=>((int)mo.GetPropertyValueByColumnName("customentity_id")) > -1);

// Scenario 4: LINQ formatted query
var q = (from mo in myCustomEntityQuery
         where mo.Entity.MockStringValue != "st00pid"
         select mo) as DataModelQuery<DataModel<MockObject>>;

// Scenario 5: LINQ formatted query execution with sorted ordering
var orderedlist = (from mo in myCustomEntityQuery
                   where mo.Entity.MockStringValue != "def"
                   orderby mo.Entity.MockStringValue
                   select mo).ToList();

// Scenario 6: LINQ formatted query with multiple conditions and multiple sort members
var orderedlist = (from mo in myCustomEntityQuery
                    where mo.Entity.MockStringValue != "def" && mo.Entity.ID < 3
                    orderby mo.Entity.ID, mo.Entity.MockStringValue
                    select mo).ToList();

This is a great milestone, one I’m very pleased with myself for finally accomplishing. There’s still a ton more to do but these were the top 50% or so of LINQ support scenarios needed in Gemli.Data.

Unfortunately, adding LINQ support brought about a rather painful rediscovery of critical missing functionality: the absence of support for OR (||) and condition groups in Gemli.Data queries. *facepalm*  I left it out earlier as a to-do item but completely forgot to come back to it. That’s next on my plate. *burp*

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C# | Open Source | Pet Projects | Software Development | LINQ

HTC EVO 4G Is The Shiznat

by Jon Davis 5. June 2010 23:49

Friday I got my HTC EVO 4G, my first Android phone and my replacement for my first-generation iPhone 2G. I had considered the iPhone 3Gs and also bought the latest generation of the iPod Touch to see if I could get away with just VOIP (bad idea, quickly resold it on eBay), and I’ve seen what’s coming from iPhone OS 4.0. Frankly, I don’t care about the iPhone anymore, not after Steve Jobs urinated on us cross-platform devs with his laughably rude EULA provisions (“only code that was originally written in Obj-C/C/C++ using the SDK-provided Xcode IDE, no cross-compilers and no third party libraries”).

Fortunately, this didn’t have to be a boycott. I know that Apple is about to announce the next-gen iPhone, but I honestly believe that the next iPhone will be only on par with, and not superior to, the HTC EVO 4G.

I was quite pleasantly surprised by how feature-packed, how beautiful, and how responsive and usable the HTC EVO 4G is. After a day or two of getting used to it, I find the well-powered Android phone to be much better than I anticipated, even far better than the iPhone as the iPhone stands today. My biggest fears about Android was that with it being VM-based it would be extremely slow, since every Android I’ve seen is slow, even on the home screen as users slide the application list up and down. But I figured the 1GHz processor on the HTC EVO 4G might make up for it enough to be able to perform at least on par with the iPhone 3G, though perhaps not on par with the iPhone 3Gs. It turned out I was right. The monster device strongly outperforms my now-obsoleted iPhone 2G and doesn’t seem to flinch like all the other Android devices I’ve seen. And when the new Android v2.2 is installed on this thing, it’ll be at least twice as fast (reports say even faster, up to 5x).

Big plus’s:

  • The display size and resolution on the HTC EVO 4G is huge. No, it’s monstrous. And the display quite brilliant if you ask me, maybe not quite as true-color as the iPhone but certainly in its ball park.
  • The various connectivity features on the HTC EVO 4G are ridiculously numerous: 3G (turns out to be pretty fast in Scottsdale), 4G (where available, not here), Wi-Fi, Bluetooth, USB, and GPS. You can toggle each one of these on or off. The USB option will let you mount its installed Mini-SD card as a hard drive on the PC, or fire up HTC Sync to synchronize your Outlook or Windows contacts.
  • Yay, I finally have a clock on my smartphone that I can set to “just clock” mode for sleeping or for my desk, it’s a single app that manages the alarm clocks as well, and I didn’t need to download a marketplace app for any of that. Woohoo!
  • 8-megapixel camera. Just 8? Pfft ..  (My Nikon D50 DSLR camera only has 6 megapixels.)
  • An extra front-facing camera! I don’t see myself using it today, but I might next year! Maybe I can get my parents to use their webcam with software that’ll work with my Android, too.
  • Social app integration extremities abound. I think at least five of the apps I installed have Twitter integration support, three of the apps have Facebook integration support, and the OS itself (perhaps by way of HTC’s shell extensions?) is among them for Facebook and Twitter.
  • The device fully, 100% supports Google Goggles and Layar. “zomg” and stuff! This is like it’s straight out of the sci-fi movies.
  • Supports Internet tethering and being a WiFi hotspot. W00t!!
  • I love the vibrator feedback as you tap on things. Tapping keys, for example, makes the whole unit shake a little bit for a tiny fraction of a second, as if to suggest a gentle nudge. Really makes it feel like it’s alive and interacting with you.

My opinion-neutral observations:

  • The ringtones are many, with lots of curious noises, but most of them are synthesized music tidbits rather than noisy sound effects like car horns. The “traditional” ringtones like those of the old-fashioned telephone are rather crappy in quality, with abrupt cut-offs, etc. But overall I think I prefer the Android ringtones over the iPhone ringtones.
  • I desperately need a dock to keep it charged while keeping it upright in portrait position. It has a “kickstand” (which is cool) but you can’t use it in portrait position while charging it.
  • People aren’t kidding when they say that the battery life on this thing is pretty limited. I feel like I spend more time charging it than I do emptying it (though this isn’t actually true, and it will stay charged for some time if you’re not constantly downloading stuff or constantly poking at it). However, I’m not too upset about this; I am rarely away from  a power source (such as a PC I can connect it to via USB), and I plan on getting two docks, one for home and one for work. I am also hoping that a better battery will come out for it in a year or two.
  • 4G isn’t available in the Phoenix metro. My boss says it’ll be here in January of next year. I’m skeptical; we’ll see.

My complaints (so far):

  • The keyboard and textbox inputs are actually kind of nice, but often annoying. During setup, for example, while entering my mailing address I needed to add a line break, but the textbox was one-line, so it still allowed for a line break but its visibility was cropped off. The auto-suggestion functionality takes a lot of getting used to as it seems to be a lot more intrusive than on the iPhone, swapping my text out even when I just hit the Shift key rather than waiting for me to hit the spacebar or period or other symbol.
  • HTC Sync truly stinks. They don’t have 64-bit support (I use 64-bit Office 2010 everywhere) and there’s no synchronization of, say, shared documents, photos, or music. Just contacts. Bleh. Fortunately, one can use SyncToy easily with the mount-as-hard-drive USB support.
  • The Android marketplace is horrifically disorganized. The categories are way too sparse. And there’s no sort ordering with highest rated apps at top. It’s just a mess.
  • Games and other multimedia apps from the marketplace are natively low-resolution on this thing, and look blurry when stretched to the display resolution of the huge EVO 4G. Even the rich 3D games I tried look blurry.

But overall, I’m very happy. Neener.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

General Technology | Android | Mobile Devices

Microsoft: We’re Not Stupid

by Jon Davis 2. May 2010 23:40

We get it, Microsoft. You want us to use Azure. You want us to build highly scalable software that will run in the cloud—your cloud. And we’ll get the wonderful support from Microsoft if we choose Azure. Yes, Microsoft. We get it.

We’re not stupid. You play up Azure like it’s a development skill, or some critical piece of the Visual Studio development puzzle, but we recognize that Azure is a proprietary cloud service that you’re advertising, not an essential tool chain component. Now go away. Please. Stop diluting the MSDN Magazine articles and the msdev Facebook app status posts with your marketing drivel about Azure. You are not going to get any checks written out from me for hosted solutions. We know that you want to profit from this. Heck, we even believe it might actually be a half-decent hosting service. But, Microsoft, you didn’t invent the cloud, there are other clouds out there, so tooling for your operating system using Visual Studio does not mean that I need to know diddly squat about your tirelessly hyped service.

There are a lot of other things you can talk about and still make a buck off of your platform. You can talk about how cool WPF is as a great way to build innovative Windows-only products. You can focus on how fast SQL Server 2008 R2 is and how Oracle wasted their money on a joke (mySQL). You can play up the wonderful extensibility of IIS 7 and all the neat kinds of innovative networked software you can build with it. Honestly, I don’t even know what you should talk about because you’re the ones who know the info, not me.

But Microsoft it’s getting really boring to hear the constant hyping of Azure. I’ve already chosen how my stuff will be hosted, and that’s not going to change right now. So honestly, I really don’t care.

Maybe I need to explain why I don’t care.

Microsoft, there are only two groups of people who are going to choose your ridiculously wonderful and bloated cloud: established mid-market businesses with money to spend, and start-ups with a lot of throw-away capital who drank your kool aid. You shouldn’t worry about those people. The people you should worry about are those who will choose against it, and will have made their decision firmly.

First of all, I believe most enterprises will not want to put their data on a cloud, certainly not with a standardized set of cloud interfaces. It’s too great a security risk. Amazon’s true OS cloud is enticing because companies can roll their own APIs with proprietary APIs and have them talk to each other while rolling out VM instances on a whim. They have sufficient tooling outside of cloud-speak to write what they need and to do what needs doing. But for the most part, companies want to keep internal data internal.

Second, we geeks don’t fiddle a whole lot with accounting and taking corporate risks. We focus on writing code. That code has to be portable. It has to run locally, on a dedicated IIS server, or in a cloud. Writing code that deploys to your cloud—whether a true cloud or locally for testing, it doesn’t matter—if it doesn’t run equally well in other environments it’s at best a redundant effort and at worst a potentially wasted one. We have to write code for your cloud and then we have to write code for running without your cloud. We most certainly would not be comfortable writing code that only runs on your cloud, but the mangled way your cloud APIs are marketed we might as well bet the whole farm on it. And that just ain’t right.

See, I don’t like going into anything not knowing I can pull out and look at alternatives at any time without having completely wasted my efforts. If I’m going to write code for Azure, I want to be assured that the code will have the same functionality outside of Azure. But since Azure APIs only run in the Azure cloud, and Azure cannot be self-hosted (outside of localhost debugging), I don’t have that assurance. Hence, I as a geek and as an entrepreneur have no interest in Azure.

When I choose a tool chain, I choose it for its toolset, not for its target environment. I already know that Windows Server with IIS is adequate for the scale of runtimes I work with. When I choose a hosting service, I choose it expecting to be very low-demand but with potential for high demand. I don’t want to pay out the nose for that potential. I often experiment with different solutions and discover a site’s market potential. But I don’t go in expecting to make a big buck—I only go in hoping to.

What would gain my interest in Azure? Pretty much the only thing that would have me give Azure even a second glance would be a low-demand (low traffic, low CPU, low storage, and low memory) absolutely free account, whereby I am simply billed if I go over my limit. If free’s no good, then a flat ridiculously low rate, like $10/mo for reasonable usage and a reasonable rate when I go over. A trial is unacceptable. I’m not going to develop for something that is going to only be a trial. And I also prefer a reasonable flat rate for low-demand usage over a generated-per-use one. I prefer to have an up-front idea of how much things will cost. I don’t have time to keep adjusting my budget. I don’t want to have to get billed first in order to see what my monthly cost will be.

I’m actually paying quite a bit of money for my Windows 2008 VPS, but the nice thing about it is there are no surprises, the server will handle the load, and if I ever exceed its capacity I can just get another account. Whereas, cloud == surprises. You have to do a lot of manual number crunching in order to determine what your bill is going to look like. “Got a lot of traffic this month? We got you covered, we automatically scaled for you. Now here’s your massive bill!”

Let’s put it this way, Microsoft. If you keep pushing Azure at me, I can abandon your tool chain completely and stick with my other $12/mo Linux VM that would meet my needs for a low-demand server on which I still have the support of some magnificent open source communities, and if my needs grow I can always instance another $12/mo account. Honestly, the more diluted the developer discussions are with Azure hype, the more inclined I am to go down that path. (Although, I’ll admit it’ll take a lot more to get me to go all the way with that.)

Just stop, please. I have no problem with Azure, you can put your banner ads and printed ads into everything I touch, I’m totally fine with that. What is really upsetting to me is when magazine and related content, both online and printed, is taken up to hype your proprietary cloud services, and I really feel like I’m getting robbed as an MSDN subscriber.

Just keep in mind, we’re not stupid. We do know service marketing versus helpful development tips when we see it. You’re only hurting yourselves when you push the platform on us like we’re lemmings. Speaking for myself, I’m starting to dread what should have been a wonderful year of 2010 with the evolution of the Microsoft tool chain.

 

[UPDATE: According to this, Azure will someday be independently hostable. That's better. Now I might start paying attention.] 

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Peeves | Software Development | Web Development

Windows Phone 7 SDK First Impressions

by Jon Davis 25. April 2010 13:20

A couple co-workers and I had a couple days’ opportunity at my day job to tinker with creating a Windows Phone 7 app as a proof of concept. We successfully created an app that lets you find and purchase a company product (complete with checkout, although the checkout part was web-based), access account information via a web interface, and contact customer support via either an e-mail form or a “Call Us” button that, yes, would dial customer support via the Phone function of the device. (Strangely, the Phone part was the only part that did not work nor give any “Not implemented” nor “Not supported” feedback.)

We accomplished all that in two days, plus some reading up on XAML knowledge (i.e. I’d spent the entire prior Saturday wading through the Petzold PDF that had been linked from the developer web site .. where is that Petzold PDF link now?), plus some insider knowledge of existing APIs and some preexisting web interfaces used with other mobile devices. The only huge hold-up for us, as far as I was concerned, was the learning curve of the tool chain. I mean, it’s C# which I’m fluent in, but it’s also XAML which I’ve used but never mastered, and it’s brand-new Silverlight 4.0 (a binary-incompatible derivative thereof, I think). And I’m still pretty green to MVVM and still don’t understand a lot of the basics of MVVM such as how and when to use ICommand, etc. But we got a prototype built, with only two days to do it.

That said and done, however, I walked away from that little mini-project with a few opinions. A couple years ago I tinkered with iPhone development with the Xcode / Obj-C / Interface Builder tool chain, so I have the two phone SDK experiences to compare.

One theme – “minimalistic” – to rule them all?

My immediate observation is that the UI aesthetic guidance in the Apple toolchain is nowhere to be seen in Microsoft’s toolchain, and I expect this will hurt Microsoft. Sure, Microsoft pre-packages a couple white-on-black templates as an aesthetic suggestion to get you started with a basic theme. But that’s where guidance ends. You’re given a few XAML controls to get you started, and everything starts at a bland white-on-black. A button, for example, it’s just a white rectangle, no gradient, no border bezel, same story as the white-square-on-a-white-line slider control, all to reinforce the idea that white-on-black is good because OLED displays consume 50% as much energy this way versus 3x more energy if everything displays in full color.

image 
Wow, Microsoft, you outdid yourselves here, that is a
beautiful slider handle! (Not.)

And on that latter note, why didn’t Microsoft innovate here, such as this: as soon as the user interacts with the device, for a full 15-30 seconds everything’s in rich, full color, then after 30 seconds of inactivity the view goes into “interactive screen saver” mode whereby the colors fade to inverse and you see the color scheme that’s on the device color scheme now? Toggle this behavior for your app or navigation “page” in the SDK with a boolean switch and a timespan setting. Just a thought.

Whereas, with Apple, in Interface Builder (Apple’s GUI designer) you’re given a nice big library of richly colored controls and widgets including a nice tab control on the bottom, a header control on top where you can have your richly labeled back and forward buttons, and a number of nice interactive controls such as a “checkbox” that actually renders as a beautifully detailed ON/OFF slider, complete with simulated physics and even a drop shadow on the moving slider.

It’s not the absence of aesthetic tooling that bothers me here. Actually, Microsoft opened the door wide open to aesthetic freedom, as XAML richly supports deliciously detailed interactive graphics with Expression Blend and the like. My issue is the fact that other than “minimalistic white-on-black” there is no guidance, which is an issue that plagued Windows Mobile 6 and previous Windows Mobile flavors. You will have some beautiful Windows Phone 7 apps like the Foursquare app, and then you will have some apps from people who learned Silverlight and go nuts with gradients and bright, flashy, ugly designs, or who simply ported their Silverlight apps straight over to the Windows Phone 7 SDK. Aesthetic inconsistency is going to be a nuisance. This happened in Apple’s developer community, too, but you’re almost required to do this with the Windows Phone 7 SDK because the prefab tooling in the Windows Phone 7 SDK is, at least so far, incomplete. Then again, this is still just a CTP (a beta), so we don’t know how much more complete the SDK will be on its release.

The SDK comes also with a “list application” template, which demonstrates how to have that pretty list that has the 3D rotating items when you tap on them or navigate around. That was really neat, but then I found myself scratching my head wondering how to make that behavior stick across the rest of the application? Can I declare this animation/storyboard definition once and reference it with a one-line behavioral reference of some kind across all of my “navigation pages”? If so, that was not demonstrated at all, and it looked like I’d have to copy and paste everything in the templated demonstration to each and every page I’d create. We ultimately just ignored the animation code, saddened that only the “root menu” showed this animation, because it’s really not worth it to copy/paste so much code (about 50 lines of XAML) everywhere, making a mess of the markup.

No HTML DOM interaction

My other complaint is with the Silverlight 4 web browser component. It is truly wonderful that Silverlight has an integrated web browser component, and I adore Microsoft for adding it especially for the Windows Phone 7 SDK, as it’s absolutely necessary to integrate the web browser with many Internet-enabled applications. That said, however, I found no easy way of interacting with the DOM from the Silverlight CLR. In Windows’ Internet Explorer, you had to make a separate COM reference to mshtml.dll before you could interact with the DOM. I used to do this heavily. Perhaps I’m spoiled. But not having access to the document interface and, as far as I can tell, only being able to navigate and toggle scripting support really worries me that my hands will be tied here. It was, after all, Microsoft who invented dynamic HTML with fully dynamic content back in Internet Explorer 4, when Microsoft’s innovation inspired me so much that I decided to go all-out into being a web developer. That was way over a decade ago. I still want that power; XAML doesn’t always cut it.

Silverlight (and XNA) awesomesauce in a bag

I didn’t have a chance to play with XNA on WP7 much yet, but just knowing that support for XNA is there is very exciting. XNA is a fantastic platform to build games on, as it is approachable even for a beginning developer. My only complaint about XNA is that there is no virtual keyboard support. I did verify by fiddling with it that the physical keyboard seems to be exposed in the XNA API, though.

Despite my complaints about the WP7 SDK, WP7 is going to be a game changer, and a huge hit for the developer community, I’m sure of it, simply because it allows you to leverage your existing C# or VB.NET skills and prototype functional software on the device with beauty and finesse. Heck, you can even leverage Ruby skills for it. For some unknown reason, IronPython won’t run on it yet; I’m sure support for that will come before RTM, particularly given the volume of feedback for it.

But you could already?

Actually, C# developers have been able to write Windows Mobile software quickly and easily for years, albeit not with Silverlight nor with XNA. Windows Mobile has lost the market share, consumer sales is now akin to Linux’s market share on the desktop—about 2%. Microsoft is going to have to work hard—harder than they have worked so far, from what I can see—at winning over people to the Windows Phone 7 away from the iPhone, which now takes up a huge chunk of the mobile market share. They have to compete as well with the Android which already sought to compete with the iPhone, Blackberry, and Windows Mobile, and is doing one incredibly good job at doing so.

Frankly, looking at the iPhone 3GS vs. Nexus One (Google Phone) demos, I can’t help but look at the Windows Phone 7 and laugh at it. Windows Mobile 6 may have had the awful Start menu, the most ridiculous interface ever to have been put onto a mobile device, but with that stupid Start menu the platform also retained the “many apps and their icons” notion of an application playground which is strong in the iPhone and Android as well but not so easy to find in WP7. I have difficulty seeing how a marketplace would even work for WP7—will installed apps be categorized and the user would find their installed apps by category? Surely WP7 will not just throw all apps into one giant vertical list, oh good grief, please tell me no!!

Microsoft has also been alienating people who identify things based on icons rather than text. The Live applications team have demonstrated this, for example, by dropping icons completely. In so doing, Microsoft is also alienating the international communities. Sure, text can be translated, but it doesn’t make a particularly great sell when the first impressions of an interface are of gobs and gobs of Engrish text and you don’t speak Engrish. Meanwhile, many of us who do speak English still prefer attractive icons over avant-garde lower-case text headings. When you have five or so menu items in 72 point font size, it’s fine, but when you’re trying to find something among 200 others, nothing beats the simple icon. Throwing away the simple icon on a grid won’t make this reality go away; this is not like a floppy drive, it is actually essential.

WP7’s minimalistic interface could actually end up being its downfall. They’re taking a huge chance and risk. I’m hopeful, but sadly slightly doubtful, that it will be enough to throw Microsoft back into the playing field.

Starting over

Maybe I’m just not getting it. Actually, Microsoft’s new direction has had me scratching my head so much that I’m convinced that I’m still struggling to get it, and that actually all of my concerns are quite intentional for Microsoft’s part.

This video demonstrates this quite well. Microsoft wants to “start over” not just with their SDK but with deemphasizing standalone installed apps and reinforcing the notion of a phone whereby the many apps are not just active but are integrated with each other.

The only problem is, I don’t see such cross-app integration demonstrated in the SDK, at least not up front, not yet. Also, why should I as a developer be excited about this new direction if the apps—my development efforts, and yours—are deemphasized in favor of prefab packaging?

Stoked and confused

Overall, I’m both stoked and confused. This is an exciting time to delve into this new technology from Microsoft. Microsoft really needs to fill in some holes, though, and these holes are less technological than they are design.  Their design and marketplace support strategies seem very vague.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

C# | Windows Phone | Silverlight

Gemli: Do We Need An XPath-to-Object Mapper?

by Jon Davis 14. April 2010 18:11

It dawned on me today that two fundamental problems exist in my day-to-day (and year-to-year) programming activities that haven’t been fairly addressed with all of our innovations in language and tooling improvements, and they are a) the absence of a flexible, late XML-to-object-graph binding mechanism, and b) the absence of an XPath-like query mechanism for CLR object graphs.

Right now I want to focus on a) the absence of a flexible, late XML-to-object graph binding mechanism.

Microsoft.NET inherently supports XML serialization and deserialization of object graphs, but what I’ve found so far is that

  1. the mappings are defined in explicit C# code (with attributes) at compile-time,
  2. class members must be sibling elements or attributes of their parent and cannot be “relational” cousins,
  3. XML trees must have a fixed hierarchy pattern or else manual serialization/deserialization code directly inside the class is required,
  4. in deserialization, serialized content cannot be XmlElement objects, and self-sufficiently XML-serialized objects often cannot be appended as children to other XML trees because of XML/XSD namespace declarations,
  5. extra or missing CLR members, or extra or missing XML members, do not always get handled gracefully when values are mapped at runtime.

Let’s take a look at each of these problems.

The first problem is simple. You add [Serializable] to your objects, and you express how the members align with the XML using [XmlElement], etc. There’s nothing much wrong with this, I don’t mind this, but because of 2) above, it’s pretty inflexible. For one thing, you cannot define BookName’s XML mapping with, say, [XmlElement(“../../Books/Book[@ID={BookId}]/Name”)], where {BookId} maps to the C# sibling member BookId, as a way of saying “map this member to cousin ‘Name’ in that other branch under Books”. The other issue with the first problem is that you cannot late-define a mapping of XML to an object without manual reflection and manual population of data. What if you have a POCO object that was never intended to be serialized?

(I already went over the second problem, as it aligns with the first problem.)

The third problem is that if you have “nephew” nodes that map as siblings, you can accomplish this, but not without manually implementing IXmlSerializable. I went down this path for Gemli.Data 0.3 in order to load up the O/RM mappings from XML, and I must say I am floored by how little confidence I have in my implementation. You’re literally poking at stream data with IXmlSerializable, not working with logical constructs at all, and I find that to be a thoroughly unacceptable way to deal with XML-to-object mapping considering the fact that XML and CLR object graphs are both expressive of their structures and hierarchies. Perhaps I am spoiled by the W3C XML DOM (System.Xml.XmlDocument), which is not used in XML deserialization, and rightly so, I suppose, since object-mappable XML streams can be huge (gigabytes) when expressed from an XML stream. We are left with few options, though, in the IXmlSerializable interface declaration.

The fourth problem goes something like this. Say that you XML-serialized an object, with rich XSL namespace semantics. Then, you created an XmlDocument that contains a bunch of custom XML markup. You cannot simply drop your XML-serialized object into the XmlDocument as a child element because your custom XML declares namespaces in its header. Runtime errors result because either the namespaces of your object are not understood (are not declared) or your object’s XML is declaring the namespaces and being put inside the body of the XML which is invalid. This is a fundamental problem with currently used XML/XSL to begin with, actually, that you cannot have nested namespace declarations inside the XML body, but this may or may not have already been addressed by the W3C with XML/XSL revisions, I don’t know.

On the fifth problem, where missing or extra members are not handled gracefully, this is actually an implementation detail and, truthfully, it’s not normally a problem because XSD and .NET attributes can accomodate (per generated xsd.exe output). However, combined with resolutions to the previously mentioned problems, this is something to keep in mind. Sometimes the problem goes the other way around, actually. Sometimes you need exceptions thrown if, for example, an XML element is missing. XSD and the mapping attributes in .NET define that, too. However, as far as I know there is no way to establish late bindings where such mapping behaviors can be defined at runtime.

Is There A Solution?

Actually, I’m not sure that there is a solution to these problems, or even a set of solutions that work together to address these problems. However, I am pondering on whether or not it would be worth my time to start working on some tooling in Gemli to make Object/XML Mapping easier.

The scenario that had me pondering such tooling goes something like this. Suppose you have an XML node with hierarchy described in XML that maps to an object graph, and you aren’t sure exactly just yet how the XML hierarchy or naming convention is going to flow, i.e. whether it will be a child of this element or a child of that element or if it will be at the root of the document. Let’s say that you want to express, in a hand-crafted, initially XSL-less XML structure, some kind of definition of an object graph and the values of the members therein. Let’s say that you already have some POCO objects that can accomodate the hierarchical structure of such a described XML tree. At this juncture, what tools are available for you to use to perform said mapping? Without going into your POCO code and manually declaring the XML deserialization behaviors, there is no straightforward way of doing this.

Tooling I might work into Gemli, then, might work like this.

First, consider the following XML and POCO sample:

<myBigFatXmlDocument>
	<blah>..</blah>
	<somethingElse>..</somthingElse>
	<aContainerOfSorts>
		<events>
			<event name="Big Spectacular Event">
				<dateTime>01/01/2011 12:01 AM</dateTime>
				<attendees>
					<attendee>
						<name>Bob</name>
						<phone>299-299-2999</phone>
						<rsvp>yes</rsvp>
					</attendee>
					<attendee>
						<name>Phil</name>
						<phone>288-288-2888</phone>
						<rsvp>no</rsvp>
					</attendee>
				</attendees>
			</event>
		</events>
	</aContainerOfSorts>
</myBigFatXmlDocument>
// POCO!

public class PocoEvent
{
	public string Name { get; set; }
	public int AttendeeCount { get; set; }
	public List<string> Attendees { get; set; }
}

By the way, I realize that it seems like a stupid design to have AttendeeCount be something other than a lookup of Attendees.Count, but bear with me here, I’m demonstrating something. Besides, what if you know that you have more attendees than you know the names of from the XML?

Second, I might cobble together a rough mapping document like this (this is NOT at all a proper definition proposal, it’s more of a “pseudo-code XML document” to get the point across):

<?xml version="1.0"?>
<xmlObjectGraphMap>
	<clrTypeMappings>
		<clrType name="MyProject.PocoEvent" xmlElement="event">
			<clrMember name="Name" xpath="@name" />
            <clrMember name="AttendeeCount" xpath="count(.//attendee)" />
			<clrMember name="Attendees" xpath="./attendees/attendee/name" 
				clrType="string" />
		</clrType>
	</clrTypeMappings>
</xmlObjectGraphMap>

Third, the C# code that performs the loading might appear this way:

// get node of first event in the list
var eventXmlNode = myXmlDocument.SelectSingleNode("//events/event[0]"); 

// deserialization setup
var graphDeserializer = new Gemli.Xml.ObjectGraphSerializer<PocoEvent>();
XmlNode graphMap = new XmlDocument();
graphMap.LoadXml(Server.MapPath("~/XmlObjectGraphMap"));
// drill-down; just use the XmlNode I need for the job
graphMap = graphMap.SelectSingleNode("//clrType[@name=\"MyProject.PocoEvent\"]");

// work is done here
PocoEvent objectGraph = graphDeserializer.Deserialize(eventXmlNode, graphMap);

What I have done, assuming that such tooling was implemented, was

  1. declare an XML node that defines the data I want to be loaded into an object graph,
  2. declare an XPath-to-object mapping strategy in ridiculously easy to read semantics, and
  3. load my POCO object graph without any manual declaration of XML serialization behavior outside of that simple XML mapping file.
  4. I also demonstrated, albeit poorly, how I used an XPath function to define an initial value of AttendeeCount, which can be manipulated in isolation from Attendees.Count (just assume this was a requirement).

It’s still not succinct enough. I could have just as well set up the API to take an XPath selection in .Deserialize() so that I wouldn’t have to call .SelectSingleNode(). For that matter, perhaps I could have written this block of code as succinct as this, using a couple do-it-all methods:

PocoEvent objectGraph = new Gemli.Xml.GraphSerializer<PocoEvent>(".\\myObjectGraphMap.xml")
    .Deserialize(".\\myXmlFile.xml", "//events/event[0]");

It could very well be that something in .NET, perhaps even LINQ-to-XML, supports this level of simplicity in the tooling of XML-to-object mapping. But I highly doubt it.

If someone out there reading this knows of some tooling that does this already, let me know. Otherwise, I am likely to get started on this in Gemli.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

PDC '09 Video For Parallel and Asynchronous Programming

by Jon Davis 7. April 2010 23:17

Just wanted to link to this for future reference. Been watching this while trying to test a deadlock from hell (I *HATE* WinDbg!!), thought it was interesting.

http://microsoftpdc.com/Sessions/FT20

It never dawned on me until watching this video that F# with its extensive "automagical" parallel-oriented semantics would a) be a killer toolset for Silverlight and WPF for producing very highly responsive UI, and b) be a brilliantly terse yet readable language (a cross between Python and PowerShell, except only in the good ways) to produce asynchronous code that is much easier to maintain than the C# equivalent.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

F#

Update For The Masses

by Jon Davis 6. April 2010 03:34

Greetings, my minions. I decided to grace upon all of you (all of you being myself?) a short moment of an update of what I have been up to lately, just to be sure that none of you think that I have let recent personal circumstances of the last 12 months wind me up in a wheelchair or bed as a brain-dead vegetable. Actually, nothing much has changed on the personal front.

Here’s what’s exciting these days.

  1. I’m still tinkering with pet projects but have been semi-focused on something I cannot talk about for a while. However, the project does use
  2. I just got an iPad, which I cannot afford and yet here it is. I am now very satisfied that I still have a jailbroken 2G iPhone, as I can do Internet tethering for the iPad without any hassle. My expectations for serious future app offerings are huge. My cats are happy. Werd.
  3. I’m getting extremely excited about Windows Phone 7. This is Microsoft’s great big opportunity to really compete with Apple’s feminine hygiene products and I think they just might even enter the playing field (although I must confess I really don’t like the busy-ness of the demo’d Windows Phone 7 main menu, especially compared to the consistency of iPhone app icons; I voted for the Zune interface because it was clean and uncluttered). I am particularly intrigued by Microsoft’s boldness in giving only Silverlight or XNA as API platforms for the Mobile 7 platforms. Hmmmm C# and Silverlight/XNA versus Obj-C and Cocoa Touch, hmmm, I wonder which one is going to be more ideal for developers? Duhrrrr…
  4. This is a great time to be a PC gamer!
    • Most recently, I finally got around to loading up Company of Heroes, which I bought three years or so ago in the inspiration of a [then] boss who had bought Command & Conquer “to play with friends”, and nobody but me wanted to play that. I borrowed the game but lost interest, so I bought CoH. Sadly, I got stuck on CoH in the second or third mission, it didn’t “click” how the game was supposed to be won, and I kept losing, so I gave up and the game sat there for a couple years. But a month or two ago I finally managed to get in there, and rinse, repeat, rinse, repeat, like a madman until I beat that level. From there, the game just “clicked” and I understood why this game was so awesome. Loved it!
    • A few months ago I also bought and beat Dragon Age: Origins. Excellent game, I must say! Very immersive offering from Bioware.
    • Another Bioware hit that I bought and beat was Mass Effect 2. It disappointed me a little, but it was still one of those “ZOMG I HAVE TO FINISH THIS” games.
    • Batman: Arkham Asylum turned out to be an outstanding, beautifully designed game. I was never much of a Batman fan, but this is a just plain drool-worthy game. It is also the first game that I’ve seen make obnoxiously verbose mention of support for 3D gaming. I had recently purchased some red/blue film glasses for situations like this, now that the nVidia GeForce chipsets and drivers natively support 3D, so I turned it on, and wow! Stomping through the Arkham Asylum halls really felt like being there. I now have a great deal of appreciation, though, of the “real deal”, the true 120+Hz monitors with which you can buy 60Hz synchronized shutter glasses where the content is different in each eye without color distortion. The red and blue thing is really uncomfortable.
    • I never did play Call of Duty: Modern Warfare 2, which I did buy, because I still haven’t finished Call of Duty 4: Modern Warfare, which is truly great game (one of the best action games of all time) but it’s just too hard for me. However, I did buy and beat Battlefield: Bad Company 2... Oh. My. Word. This game is absolutely incredible. It is by far the best multiplayer shooter I have ever played. And if the single player game wasn’t so short, it would be truly monumentally awesome, up there on par with the Half-Life series of the action genre. Sadly, it is indeed short, and when you see the credits rolling, you’re more irked than satisfied.
    • I got the lifetime subscription of Star Trek Online. It was a mistake. She’s dead, Jim. The ratings agree.
    • I’m feeling nostalgic about Homeworld, the best epic story in game form ever (tear-inducing!), and never did finish Homeworld: Cataclysm (too hard!) nor have I played Homeworld 2. They’re old games, but I’ve decided that I want to have completed them. So that’s what I’ve most recently fired up.

Now, my kindred children, return. Go back to your lives that are inspired by my doings, and dream big dreams, yes, big dreams, of being like me. LOL ... cat fur up your noses and all!!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:


 

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Jon Davis Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.
 
Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up."

Jon is presently employed as a senior .NET developer at a very well-known Internet services company whom you're more likely than not to have directly done business with. However, this blog and all of jondavis.net have no affiliation with, and are not representative of, his employer in any way.

Contact Me 


Amazon Collection

Most Recent of Many Library Investments

Tag cloud

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar