ILoggable

A place to keep my thoughts on programming

 Subscribe

geekblog
[at]
claassen [dot] net

Powered by Blogger

Monday, July 18, 2005

String Formatting in C#

I google this every time, i need to put a float inside of a String.Format() call. Never found a good reference, so i made a note to the place i got the answer from.

Those days are over! Steve Tibett has an excellent article on the subject

Update: Updated the link, as Steve Tibbet's blog software changed and with it the url.

Sunday, July 17, 2005

New LiveForSpeed InSimLib

Version 0.06b of InSimLib is done. Some bug fixes, some changes to go along with InSim changes in the recently release S2 alpha, and a complete rewrite of the Configuration code, to allow for simpler .NET friendly App.config or Web.config usage.

Current NDoc

Release Notes

Source Release

Binary Release w/ Sample Test App

Enjoy.

Friday, July 15, 2005

Detecting ASP.NET

I've been wondering for a while how you could reliably tell if you are currently running under ASP.NET. This is really only of interest to be because of the ThreadSingleton vs. Static Singleton issue. The best way I've found so far is:
bool isASP_NET = ( System.Web.HttpContext.Current == null )?false:true;

Kind of annoying, because you have to reference System.Web in your Project, which you wouldn't otherwise.

I looked through most of classes that come out of the System and mscorlib assemblies but couldn't find anything good and reliable (i.e. didn't want to use AppDomain and see if the config file was called web.config.. Sounds like an accident just waiting to happen.) Still, there's got to be a better way.

Thursday, July 14, 2005

Building custom configuration section handlers

Been playing around with creating my own configuration section handlers. Call me crazy, but i just have a distaste for storing my hierarchical application configurations in some kind of artificially derived flat Key/Value pair scheme.

I see a lot of applications doing something like:

<AppSettings>
  <add key="MyApplication.SomeValue" value="foo" />
  <add key="MyApplication.DB.Host" value="localhost" />
  <add key="MyApplication.DB.Password" value="secret" />
</AppSettings>
when it would be so much cleaner to do:
<my_application>
  <some_value>foo</some_value>
  <db>
    <host>localhost</host>
    <password>secret</password>
  </db>
</my_application>
The answer to this is System.Configuration.IConfigurationSectionHandler and then you stuff the result in a singleton. A couple of pitfalls with this approach are:

Application vs. ASP.NET: The singleton needs to determine whether it's running as an application or under ASP.NET, so it can decide whether it needs to be a thread singleton or can just be a static variable. Easiest solution here is to allow a way to manually initialize the singleton with a flag. Then you can use Begin_Request in global.asax to tell it to use a thread singleton and default to a static variable otherwise

Allowing for config relocation like AppSettings: AppSettings lets you externalize your name/value pairs via <AppSettings file="../../somefile.config" />. This can easily be handled in System.Configuration.IConfigurationSectionHandler except that you need to look at the path to see if it's relative and then you need to figure out how to get that. The best way to determine the directory that's the same between Console, WinForms and ASP.NET is using AppDomain.BaseDirectory and just appending the relative path to it.

Wednesday, July 06, 2005

CPAN for .NET

Maybe I just don't have my feelers in the .NET community like I did in the perl community. Actually, know I don't... Anyway, I think a CPAN for .NET would be a fantastic community to build. http://csharp-source.net/ looks like a nice site, but it's project oriented. And otherwise, I haven't really found any good aggregators for C#. Sure, there are tons of components, but you gotta google each one.

I'm thinking of a site that's more tightly focused on components. Maybe have a common prefix namespace, then authors would sign up for sub namespaces for their components. You'd have dev and release versions, where a release component projects would have to include nant, ndoc and nunit and have the componnent strongly named. Users could then report back to say whether they were able to build on their system, or even add automated buid/test farms.

Users could then have a certain expectation of what a component from this community archive is like. You'd also have place to start looking for new components. Any time i started something in perl, i first hit search.cpan.org first to see if there was something that already did what i needed or provided stepping stones. And i wouldn't get back a list of projects that build some of the pieces i want but tightly integrate them into a project that otherwise doesn't meet my needs.

It would also be a place that keeps you up to date with the development, instead of having to navigate each developers site, hoping it goes you some piece of information on whether the project is alive. You would have RSS feeds, for project status, etc.

So if I just happen upon some free time... Hold on, gotta catch my breath... that one was funny... erm... Anyway, I'd love to build this, but i just don't know when. Maybe i should float the idea on one of the mono dev lists?

Tuesday, July 05, 2005

Managed Direct3D and World Transformations

I spent some time this weekend playing with Managed DirectX. And I finally figured out a "bug" I've been having whenever i tried to do Direct3D transformations:

Every time I've tried to animate objects, they all seemed to end up moving in lock step. It's got to do with the fact that everything gets moved through world transformations. And every example i found would always go "ok, we'll create an object, and then you do this to move it". Wow, so simple. None of the examples ever did "here are two objects and they move independently". The next step always was "and here is a full fledged game, enjoy going through the undocumented code".

Well, so what happens if you just duplicate the code for the second object and try to move it with a different transformation? Basically, both objects are in lock step. Why? Not entirely sure, of the exact mechanics, but basically the way to fix is to to transform the world back to the origin as the last step. I still don't really understand the mechanics of it all, but that fixes it. So, until i find a better explanation of what happens with all the transformations at the render stage, I'm just happy to go with this.

Realistically, if I was going to go for any serious 3D programming, I'd skip all this base code I'm playing with and just code against Axiom. No point re-inventing the wheel, at least for the level of stuff I'm ever likely to get into.