Check This Out (Simple Reflection.Emit Stuff)

public class HelloModifier
{
    public static Target CreateTarget()
    {
        AssemblyName assemblyName = new AssemblyName("MyDynamic");
        TypeBuilder typeBuilder = AppDomain.CurrentDomain.
            DefineDynamicAssembly(assemblyName,
                AssemblyBuilderAccess.Run).
            DefineDynamicModule(assemblyName.Name).
            DefineType("TargetOverride", TypeAttributes.Class,
                typeof(Target));
        MethodBuilder hello = typeBuilder.DefineMethod("Hello",
            MethodAttributes.Public | MethodAttributes.HideBySig |
            MethodAttributes.NewSlot | MethodAttributes.Virtual,
            CallingConventions.Standard, typeof (string),
            Type.EmptyTypes);
        ILGenerator il = hello.GetILGenerator();
        il.Emit(OpCodes.Ldstr, "Hello, World!");
        il.Emit(OpCodes.Ret);
        typeBuilder.DefineMethodOverride(hello,
            typeof(Target).GetMethod("Hello"));
        return (Target)Activator.CreateInstance(
            typeBuilder.CreateType());
    }
}

public class Target
{
    public virtual string Hello()
    {
        return "sorry";
    }
}

[TestFixture]
public class NeatTests
{
    [Test]
    public void SayHello()
    {
        Target target = HelloModifier.CreateTarget();
        Assert.AreEqual("Hello, World!", target.Hello());
    }
}

OK, so it’s not that revolutionary, but I’ve never had to dig into the Reflection.Emit namespaces before. I tried once before, but never got that far. It does seem hard to find good, useful documentation and examples on it.

This isn’t really all that useful here, but I could come up with some imaginative uses for it if it was more dynamic.

Oh, and with DynamicProxy HelloModifier looks like this:

public class HelloModifier
{
    private static readonly ProxyGenerator _generator =
        new ProxyGenerator();

    public static Target CreateTarget()
    {
        return (Target)_generator.CreateClassProxy(
            typeof(Target), new HelloInterceptor());
    }

    private class HelloInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            if (invocation.Method.Name == "Hello")
                invocation.ReturnValue = "Hello, World!";
        }
    }
}
Posted in Software Development | Tagged , | Leave a comment

On Microsoft and Developer Learning

For a while now I had been expecting better developer documentation from Microsoft. You’d think if you were looking for tips and ideas on how to develop software that Microsoft would know a thing or two. I mean they write a lot of software (some better than others) and they have several development tools and platforms.

And there is a ton of documentation on specific tools or examples how to use some specific classes in excruciating detail. However, as far as general software design, architecture, and processes they haven’t had much. And that’s the kind of stuff I was looking for.

Then it dawned on me that a tool vendor is going to document how to use all the extended features and document as much as they can about their tool. But many of the principals of good design are platform independent. In fact good practice is often to go for the general case and only use the specialized features when necessary, and even then you build abstraction layers around them.

Then there is the quote “the sky is a different color at Microsoft.” i.e. Microsoft is so big that most everything they use comes from Microsoft. If an idea wasn’t invented there or they have not made their own version of a tool, they often appear ignorant that such an idea or tool exists.

I guess all I’m saying is that I no longer expect this kind of info from Microsoft. Instead I’ve been doing much better learning from blogs, open source projects, and books (lots of books.) The do have a patterns & practices group and they have been improving and the talk about the ASP.NET MVC framework seems promising (lots of examples of interacting with existing open source software.)

Posted in Software Development | Tagged , , | 1 Comment

Got my copy of Applying DDD (w/C#) today

DHL said it arrived yesterday, but I never saw it. Turns out DHL used USPS and it was in the mailbox.

Anyway, the book is Applying Domain-Driven Design and Patterns: With Examples in C# and .NET. I just finished the first chapter and although a lot of it was familiar to me, I liked the author’s writing style and found it more engaging than some of the more abstract books on the patterns, architecture, and designI have Domain-Driven Design, Patterns of Enterprise Application Architecture, and Design Patterns. I really like the books and I have learned a ton from them, it’s just that they are a little dense and abstract. I don’t know that I feel comfortable giving them to someone new to the concepts.. He mentioned several tools that I use daily like NUnit and CruiseControl.net.

I’m hoping this book will be both a good introductory book that I can give to people and that it will give me with some practical examples of how to actually work with DDD in .net. I really want to read through it as quick as I can so that can try to get some other people at work interested.

We have been using some things like unit tests and continuous integration and I think it has greatly improved our process and the quality of our code. Hopefully concepts like domain models and the object-relational impedance mismatch can spur further improvements.

Posted in Software Development | Tagged , , | Leave a comment

Workaround for weird Firefox bug where the textarea cursor will dissappear

Every once in a while I run across a bug in Firefox that basically hides the blinking cursor that is supposed to be in a textbox. It’s still there and I can still type, but I just can’t tell where the next character will appear. Every time this happens, I have to Google around a bit and find the Bugzilla bug describing it.

For my own reference: here is the bug report. It will be fixed in Firefox 3.0, the workaround is to add a style="overflow: auto" to a parent element of the textbox. Apparently it is caused by placing a textbox in a position: fixed element with a height.

I also found a fix over on Bram.us that is a bit of css that should fix every input and textarea on a page.

Posted in Software Development | Tagged , | 1 Comment

So 2008 is here…

Another year came and went already. I had a lot of fun in 2007 and I’m excited about the next year.

There’s a lot of interesting things coming up in the .net world: .net 3.5 and Visual Studio 2008 are finally out, the ASP.NET MVC framework is looking good, the ALT.NET stuff is giving some attention to non-Microsoft tools. I’ve been working on some interesting stuff for a while now that should get some use soon.

I’m not really much for the resolutions and stuff. I will try to get more content up here this year. So far I’ve kept this blog running. I haven’t abandoned it yet. I have a ton more ideas that I just haven’t written down yet.

Posted in Software Development | Tagged | Leave a comment

Quick Tip: Fullscreen Window in WPF

(aka Windows Presentation Foundation aka Avalon aka XAML)

WindowState = WindowState.Normal;
WindowStyle = WindowStyle.None;
Topmost = true;
WindowState = WindowState.Maximized;

That’s it, nice and easy. Setting WindowState to normal before and then maximized after fixed an issue where it was fullscreen, but didn’t expand to cover the taskbar.

I did this in order to make a patch for Big Visible Cruise, a new app that displays CruiseControl.net statuses in a big noticeable window. (Issue 14)

I got some ideas from this forum post on the MSDN forums.

Big Visible Cruise is hosted on Google Code. I’d never used it before, but it was really easy and simple to log in, add a comment, and attach a patch.

Posted in Software Development | Tagged , | 5 Comments

Just a quick thought about Google and Microsoft:

Google is a marketing company run by engineers; Microsoft is an engineering company run by marketers.

I had this thought as I was walking to the kitchen. It’s not entirely accurate I’m sure, but a general impression from their outward appearances. Anyway, I thought it was interesting and just thought I’d share.

Posted in Software Development | Tagged , , | Leave a comment

2 Hours With Castle MonoRail

MonoRail is one of those frameworks that I’ve been watching for a while, but I’ve never spent the time to actually dig in and start messing with it. MonoRail, if you haven’t heard, is a MVC framework for ASP.NET web applications, similar to Ruby on Rails. Microsoft is also working on their own ASP.NET MVC framework, but it’s not available yet (or it wasn’t back when I wrote this.)

A little while ago I spent a couple hours with it, just reading the tutorial and trying stuff out. I figured I could make an interesting post on my experience with it. The following is just my rough notes from that. Some might even call it live blogging.

MonoRail

I started by downloading and running the msi installer of rc3.

I created a MonoRail app through the Visual Studio wizard as the documentation suggested. It gave me some NUnit tests, they ran quickly in Resharper’s test runner. Hitting F5 started Visual Studio’s server and brought me to a nice looking page offering examples and documentation.

Urls are interesting. It redirects me to /home/index.castle. It seems to do this in the generated Default.aspx. This implies that I might be able to throw some webforms pages into a MonoRail site. *.castle is mapped to MonoRailHttpHandlerFactory in the web.config. Looks like standardish, somewhat advanced, asp.net stuff so far. I’d probably have to add the .castle mapping in IIS.

Images, stylesheets, and JavaScript are in a folder called Content; seems to load the same as files any old ASP.NET site.

I’m looking at the rendered html and trying to figure out where it came from. There’s a Views folder, inside that a Home folder and an Index.vm file. Index.vm seems to have a few snippets, but not the main bulk of the html. Ah, layouts/default.vm has a lot in it. There’s some variable replacement going on $!{title} and $siteroot, I’ll have to look up the syntax later. Hmm, there’s a $!scripts in the layout and a #capturefor(scripts) in the home view. This is all probably NVelocity stuff. Just found rescue views, nice!

On to the login controller… I can see it works and how the controller code gives the form values to the view through a PropertyBag. How’s it decide to show the form or the result? The form’s action = /Login/Authenticate.caste, the form page is /Login/index.castle. Ah, and there’s two different views.

Just noticed that Visual Studio tabs are confusing when you have three Index.vms open. I just noticed that it can build links to the various pages through $Url.Link(…).

The idea of the controllers seems familiar. I’ve taken a look at it before and I’ve tried to use the MVP pattern with webforms in a few projects. I wonder if the url is tied to the controller? Is there a way to separate it? I could live with it for an internal site, but I might want more control for a public site. For example the url of this post has a lot of info in it that I think might not be in the MonoRail urls. We’ll see.

I really like having more control over the html than webforms gives you.

OK, processing forms, passing variables back to the view and then the browser, got it. Not sure where to go now. Ooh! DataBinding… OK, so it’s different than webforms databind. It binds fields from a form to properties on a class, very nice.

The docs then go on to ActiveRecord. I’ve already used ActiveRecord, so I’m going to skip it. That’s it for the getting started documentation. I think I might move on to Windsor.

Windsor

Windsor while not part of MonoRail is related because they are both part of the Castle Project.Windsor is an inversion of control container.

Getting started part 2 & 3 are not done yet. It’d be nice if I didn’t have to click the link to find that out… Anyway I’ve tried to use IoC before, I used Spring.net to configure a project, and I thought I understood IoC, but I’m not so sure I really did.

The simple example of Windsor registering a form and getting it was neat, but doesn’t show me how or why I would want this. Separation of concerns: nice and all, but can’t I do that without a container? Just create my objects one by one, passing them into the constructor/setting properties myself. Does this get more useful as I get more classes? Where is it practical? If I only have 5 classes that interact, probably not. What about 50 classes and each takes a couple parameters? Assuming I’m not using the configuration features (yet).

When you add configuration, it seems like a handy configuration utility, but I know that isn’t the main purpose of IoC

Wrapping up

Anyway, that’s all the time I have for this. Hopefully someone will find this interesting or at least I can reread it whenever I attempt to use these libraries in a future project. They’re interesting, well documented, and appear easy to use. I don’t have a specific need for either, so I’ll wait. I don’t want to force them into an existing project just because they are cool. I’ll wait until I need them.

Returning to the site just now, I noticed a “Using” link in the header. It looks like there is a lot of good info in here. Maybe it could be linked to from some of the documentation. Maybe a see here for some other useful info?

Posted in Software Development | Tagged , , | 5 Comments

Avoiding the startup time for webservice xml serialization

The short version

In the build tab of a project’s properties is an option called “Generate serialization assembly”, turn it to “On” to theoretically improve performance.

The long version

Recently I’ve been working on speeding up a web application that depends heavily on a huge webservice: 884 “operations” are supportedFirebug + document.getElementsByTagName(“li”).length, the wsdl is 2 megabytes. Startup takes several minutes before anything happens. And it is mostly the startup time causing problems, things go smoothly after that.

While trying to improve things I found this single line causing most of the delay: _myStore = new WebServices3(); And it was only the first time that the webservice was initialized that the delay happened.

I started searching and ran across a knowledgebase article explaining how to create an XmlSerializers assembly. Apparently the XmlSerialization process usually generates code at runtime and this can take a while. The article explained how to pregenerate this code to save startup time later. I tried it, it didn’t seem to help all that much and it was a pain to fit into the build process.

I later found a page on MarkItUp about SGen.exe. It was a new feature in .net 2.0 that I remember hearing about. Then I found this post on kiwidude.com about how Visual Studio didn’t call sgen correctly for him.

I never noticed it before (maybe because I had to scroll down the properties page), but there was a simple drop down in Visual Studio for this. Switching it from Auto to On built the assembly automatically!

XmlSerialization

The end

Once enabled, build time became much longer and startup times were a little better. The change wasn’t as dramatic as I had hoped. I think from here I’m going to create a much smaller, specialized web service since I have access to the web service machine and we’re only using a handful of the 800 methods available.

Posted in Software Development | Tagged , , | Leave a comment

Points to consider when evaluating a JavaScript framework

I found How to choose a JavaScript framework » d’bug (via Ajaxian). In it the author brings up several things to consider when evaluating a framework.

It’s somewhat related to my earlier post on looking for a comparison of JavaScript frameworks, and his was posted just a day before mine.

It’s not the comparison table I was looking for, but if I ever get around to making that table I’ll certainly use some of the points mentioned in it. I think most of the article could apply to any framework developer tool, not just JavaScript frameworks.

Posted in Software Development | Tagged , | Leave a comment