Category Archives: .net

Proof of concept: C#-SQLite running in Silverlight

Noah Hart has ported SQLite to C#. I was intrigued to see whether the code could be adapted to run in Silverlight, which has a cut-down .NET Framework and prohibits platform invoke to native code.

I hacked away at his code until it ran in Silverlight:

Note that the sole purpose of the exercise was to see if SQLite could be made to run, not to prepare a port that is production-ready or even code that is fit to check-in. In essence, I defined SQLITE_SILVERLIGHT, removed all the locking, adapted the file I/O to use isolated storage, and generally took as many shortcuts as I could think of to get it to compile and run. Unfortunately transactions do not work, though that can probably be fixed.

As far as I can tell, Silverlight’s Isolated Storage does not support locking at all. If the same application were opened twice, for example in different browsers, that could cause problems, but in normal use it would likely be OK.

One other point of interest: the XAP file in release mode is 257KB.

Update: The sample app is here. The hacked code is here [zip].

C#-SQLite now published

Noah Hart has published his port of SQLite to C#. The project is called C#-SQLite. The main SQLite author, D Richard Hipp, originally said that the project should not include SQLite anywhere in its name, but has now relented:

I think I would be comfortable with a name like C#-SQLite. The prefix makes it clear that it is a reimplementation, not the original. People notice prefixes much more readily than suffixes. There is also precedent for changing a prefix for a reimplementation. Lucene, for example, was ported to C-Lucene. I’m sure there are others.

The whole point of this exercise it to avoid confusing people. Folks who use C#-SQLite need to be fully aware that what they are using is distinct and separate from the original SQLite. As long as that one condition is met, I am happy.

I downloaded the code, compiled in Visual Studio 2008 (requires project conversion), ran the shell and tried a couple of commands. It works!

Of course there are already a million ways to use SQLite from .NET, but they all require platform invoke to native code, which is not allowed in some scenarios for security and/or cross platform reasons – Silverlight being a prime example.

Next question: how hard will it be to get this working in a Silverlight project?

See also SQLite C# port raises hopes for a Silverlight local database manager which has some early performance results.

Technorati Tags: ,,

Silverlight 3 is out

Microsoft has released Silverlight 3, though some pieces of the platform are still not done – it seems there is always something to wait for.

There are links to the tools developers and designers need to install here:

http://silverlight.net/GetStarted/

Note that Expression Blend and Sketchflow are still at Release Candidate stage.

The .NET RIA services, a server-side piece that simplifies authentication and database operations, is available in a new July 2009 preview:

http://www.microsoft.com/DOWNLOADS/details.aspx?FamilyID=76bb3a07-3846-4564-b0c3-27972bcaabce&displaylang=en#filelist

See this excellent post by Nikhil Kothari for more on RIA Services – it’s from March but does a good job of explaining what they are about.

Using Silverlight 3, or plan to? I’d love to hear from you, along with your views on what is best and what is worst about Microsoft’s RIA efforts.

Is Mono safe to use?

Microsoft has promised not to sue those who develop implementations of its C# language and Common Language Infrastructure – the heart of .NET.

You might assume that to be good news for Mono, the open source implementation of .NET sponsored by Novell; and I suppose it is, though not in any major way.

The key point here is that Microsoft’s .NET platform goes well beyond what is covered by the promise. ASP.NET, Windows Forms, ADO.NET, Windows Presentation Foundation, Silverlight: all this falls outside. The promise covers only what is standardised by ECMA.

Mono leader Miguel de Icaza is clear about the differences in his post, and says Mono will be split into safe and – presumably – unsafe parts:

In the next few months we will be working towards splitting the jumbo Mono source code that includes ECMA + A lot more into two separate source code distributions. One will be ECMA, the other will contain our implementation of ASP.NET, ADO.NET, Winforms and others.

A good thing? Well, it could help promote the core of Mono in the open source community, which is wary. Open source champion Richard Stallman recently stirred up debate on the subject by proclaiming C# dangerous, in what has become a somewhat perplexing post:

It is dangerous to depend on C#, so we need to discourage its use. … The problem is not unique to Mono; any free implementation of C# would raise the same issue. The danger is that Microsoft is probably planning to force all free C# implementations underground some day using software patents.

he says (and is unlikely to be comforted by Microsoft’s recent moves), though he adds:

This is not to say that implementing C# is a bad thing. Free C# implementations permit users to run their C# programs on free platforms, which is good. (The GNU Project has an implementation of C# also, called Portable.NET.) Ideally we want to provide free implementations for all languages that programmers have used.

Confusing. Still, it’s a valid and important question. Is Mono safe to use?

I have been asking myself this for many years; and have asked Microsoft about it on a number of occasions with no clear answer. However, to me the breakthrough came when Moonlight was announced, an implementation of Silverlight for Linux for which Microsoft has partnered with Mono. The dual significance is first, that Microsoft is working with Mono; and second, that it shows how Microsoft has realised that overall Mono is more a benefit than a threat to its platform.

Action against Mono now seems less likely than ever. That opinion is not based on legal knowledge, but on the business & PR case.

Nevertheless, there could be limits. Stallman’s recent statement was provoked by discussions over whether Mono should be part of the default Debian install. It is not; but it is rising higher on the list of packages that are likely to be installed sooner rather than later by users, because Mono applications are growing in number.

On the server, Mono’s implementation of ASP.NET is now rather good. I’m using it on ITWriting.com for a Silverlight demo. I’ve been impressed by its compatibility with Visual Studio, though I’ve also found it increasing resource usage more than I would like. I’m using what is now an old version though, so I expect this to improve.

The cost savings in using Linux+Mono rather than Windows+ASP.NET are significant, which implies that the potential cost to Microsoft is significant too. It’s only a potential cost, because frankly the official platform is still less risky for a commercial deployment, from a technical rather than legal perspective. Mono is currently more likely to attract free or hobbyist users; but that could change. If Microsoft saw server license sales bleeding away because of Mono, my guess is that there would be rumbles.

What then of de Icaza’s move to separate Mono into safe and unsafe parts? I see the sense of it, but wonder if it could be counter-productive. While Mono is, errm, monolithic, Microsoft cannot take action against the unsafe parts without also blocking the safe parts. After the split, it will be easier for Microsoft to agitate about the pieces of Mono which might (or might not; I’m no lawyer) infringe its rights.

It still strikes me as unlikely that Microsoft would risk full-on legal action against its partner and against what is now a significant part of its platform story. However, I doubt we will get any further comfort from the company, beyond what it has already given.

*updated to acknowledge that Silverlight and WPF are covered by the open spec promise – see links in the comments.

Does Visual Basic have a future?

I was interested in this podcast with a member of the Visual Basic team at Microsoft, Lisa Feigenbaum, as I ask myself the same question.

Unfortunately the questioning from Joe Stagner (who also works for Microsoft) is tame. Nevertheless, there are a few points of interest.

“The things that come out of Microsoft, it is C# biased” admits Feigenbaum, which she says is because so many at Microsoft have a C or Java background.

That is part of the reason (though more C than Java) but I doubt it is that simple. If you go back to the beginnings of .NET, Microsoft designed C# and what was then the new framework and runtime together. VB on the other hand was hauled into the new world and still bears the scars.

Let’s answer the question first. Microsoft cannot afford to abandon VB, which remains popular, especially (though certainly not exclusively) at the less professional end of the market. VB isn’t going away.

Further, there’s really very little difference in the capabilities of the two languages, so there is little incentive for anyone to switch. Microsoft has attempted to differentiate them, but these attempts generally fail. “Any time we do something cool in one language, the other folks want it” says Stagner in the podcast.

As I see it, that’s part of the problem. It begs the question: what is the point of VB, other than to keep existing VB developers happy? In what circumstances would you advise a new programmer to learn VB rather than C#?

I dip in and out of both VB and C#, and of the two I prefer C#. I find VB’s slightly increased verbosity annoying, and I dislike the statement continuation character which is unnecessary in C#, because statements end with semicolons. I prefer case-sensitive languages, which give more flexibility when naming variables. If you want to do XNA games programming, currently only C# is supported. 

VB’s dynamic features are useful in some scenarios, particularly Office automation, though this advantage is removed in C# 4.0 which has dynamic variables.

The original attraction of BASIC, its English-like syntax, is nearly lost in today’s VB.

Well, choice is good; and the existence of VB alongside C# proved the cross-language credentials of the “common language” runtime from the get-go.

Nevertheless, I’m expecting VB usage to decline gradually. An external factor is the rise of the JavaScript family, which is more like C# than VB.

Incidentally, Feigenbaum threw in a comment about Visual Studio that I found interesting. After talking about the managed code editor in Visual Studio 2010, she remarks “In the release after 1010 we’re rewriting the compilers in managed code.”

Technorati Tags: ,,,,

Book Review: IronPython in Action

I guess that to many people IronPython, an implementation of the Python programming language that runs on Microsoft’s .NET platform, is little more than a curiosity. For them, a glance at the preface and foreword to this new Manning title may be enough to convince them that it is at least interesting. In the foreword, IronPython creator Jim Hugunin describes how he first worked on Python for .NET as an exercise to prove “why the CLR is a terrible platform for dynamic languages”:

The plan was turned upside down when the prototypes turned out to run very well – generally quite a bit faster than the standard C-based Python implementations.

Next is a preface from the authors, Michael Foord and Christian Muirhead, about how IronPython came to be used in an innovative programmable, testable spreadsheet called Resolver One:

Resolver One is in use in financial institutions in London, New York and Paris, and consists of 40,000 lines of IronPython code with a further 150,000 in the test framework. Resolver One has been tuned for performance several times, and this has always meant fine tuning our algorithms in Python. It hasn’t (yet) required even parts of Resolver One to be rewritten in C#.

Why is Python used in Resolver One?

As a dynamic language, Python was orders of magnitude easier to test then the languages they had used previously.

If that piques your interest, you may find the book itself worth reading. It is aimed at Python programmers who do not know .NET, and .NET programmers who do not know Python, rather than at existing IronPython developers. The authors run through the basics of Python and its .NET integration, so that by the end of Part 1 you could write a simple Windows Forms application. Part 2 is on core development techniques and covers duck typing, model-view-controller basics, handling XML, unit and functional testing, and metaprogramming – this is where you generate and execute code at runtime.

The third part of the book covers .NET integration, and how to use IronPython with WPF (Windows Presentation Foundation), PowerShell, ASP.NET, ADO.NET (database access) , web services, and in Silverlight. Finally, Part 4 shows how to extend IronPython with libraries written in C# or VB.NET, how to use Python objects in C# or VB, and how to embed the IronPython engine into your own application.

It’s a well-written book and fulfils its purpose nicely. I like the way the book is honest about areas where IronPython is more verbose or awkward than C# or VB.NETAs someone who knows .NET well, but Python little, I could have done without all the introductory .NET explanations, but I understand why they are there. I particularly liked part two, on core programming techniques, which is a thought-provoking read for any developer with an interest in dynamic languages.

According to the IronPython site, you can get 35% off by ordering from the Manning site with the code codeplex35.

It is also available on Amazon.com:

and Amazon.co.uk:

 

New Visual Studio 2010 beta has WPF editor, Silverlight designer

I’ve just downloaded and installed the Visual Studio 2010 beta 1 release. I’ve not explored much yet – and it is rather slow in a virtual machine – but it does now seem to have the new editor and other pieces built with Windows Presentation Foundation.

A landmark for both WPF and Visual Studio.

I also noticed that the Silverlight visual designer now works as you would expect, though I had to download the developer runtime and SDK separately:

I’d welcome comments from anyone using the beta.

New York Times switches from WPF/Silverlight to Flash and AIR for Reader 2

The New York Times has released Version 2 of its Times Reader, for seamless online/offline viewing of its content. It’s interesting from a media perspective, but hardly a breakthrough, since it is not new. What’s more interesting to me is that the Times switched from a hybrid approach using WPF (Windows Presentation Framework) on Windows and Silverlight on the Mac, to Adobe AIR. Switches like this are bad PR for Microsoft, since it gives the impression that the developers were sufficiently unhappy with WPF/Silverlight, or so strongly attracted to AIR, that they were willing to throw away much of their previous development effort.

I’ve been tracking Times Reader for some years. It was presented at Microsoft Mix07 and I wrote up a panel discussion on the subject:

I asked about the cross-platform issue. According to Bodkin a Silverlight implementation is on the way, which includes most of the features in the full version, in “a matter of months.”

That was optimistic; but a Silverlight version was delivered and I used it successfully on the Mac; though it lacked some features of the WPF edition. It also attracted hostility from Mac users who are Microsoft-averse, as I reported here, and apparently ran into further problems because of incompatibility with Safari 4.

I tried the new AIR edition and it seems pretty good, though my impression is that it is not quite as smooth as the old WPF version. I might be wrong, since I could not install both on the same machine. The new version does add video support. Here’s the old one:

and this is the new effort:

I think this is a fascinating case study which demonstrates a number of things.

First, that cross-platform support is not an optional feature any more (if it ever was) for this kind of public application. Let’s assume here that the WPF version was just fine for Windows users, but was not viable long-term for lack of cross-platform support. It was inevitable that the Times would eventually either use Silverlight on both Windows and Mac, or abandon both WPF and Silverlight for a cross-platform alternative.

Second, that Silverlight is not yet mature enough for this kind of application. Although the Times developers were able to deliver a Silverlight version, it required a bit of hackery for offline support (embedded Safari on the Mac) and apparently ran into version problems when Apple upgraded Safari. Silverlight is also known to be poor for text rendering – a Google search for “blurry text Silverlight” brings back plenty of hits. Adobe also made a big improvement to text handling in Flash player 10, with the new flash.text.engine.

Third, that offline support really is a big deal. Would Silverlight 3.0 have been good enough? Possibly, though I haven’t seen any suggestion that Silverlight 3.0 offline apps will be able to run in the background while showing just an icon in the notification area, to support continuous synchronization.

It is possibel that these problems may be fixed in Silverlight 4.0. That’s a long time to wait though, when you need your application out now (and your industry is in crisis).

It would be silly to extrapolate this case study into a broader statement about the superiority of Flash over Silverlight. For the specific needs of the New York Times though, it is easy to see why Adobe AIR appeals.

Silverlight: developer win, designer fail?

I posed this question in a post over on itjoblog. There are several reasons why Silverlight struggles to get designer attention, including:

1. Designers are pragmatic and target the runtime that is already deployed most broadly, ie. Flash.

2. Flash is already good enough so why bother?

3. The tools: Adobe’s designer tools are a de facto standard, target Flash, and run on the Mac.

Developer is another matter. The cross-platform .NET runtime is Silverlight’s big advantage; and this time the tools tip the balance towards Microsoft (Visual Studio) – not for everyone, but for the substantial Microsoft platform community. That’s going to be further reinforced by Visual Studio 2010 which gets full visual designer support, plus of course Silverlight 3.0.

Microsoft does have a problem with Silverlight out of the browser. Developers need a way to have these run with more local permissions, subject to user consent, otherwise they will turn to Adobe AIR. Actually the whole Silverlight on the desktop story is confused, since you can also do Silverlight Mesh-Enabled Web Applications, or stick Silverlight content in a desktop gadget or other embedded browser. No, not the one in AIR (nice idea though): Adobe only includes Flash support and the PDF plug-in.

The tension behind this is that ultimately developers and designers need to work on the same applications, so this remains a fascinating contest.