| Chris Skardon 的个人资料Chris Skardon日志列表 | 帮助 |
|
|
2007年11月 ArgumentException / ArgumentNullException argument order.When I come to use the ArgumentException or ArgumentNullException I pretty much always get the argument order the wrong way around - is it 'paramName' first, or the 'message'? Every time I do this I silently curse the framework designers and swear I would have made the args the same way around, either 'paramName' first always, or the message first. Then, not 10 minutes ago, I finally twigged why. Or at least a reason why... Typically (at least in my code) the exceptions are used in the following manner: public void Foo(string myArg) { if(myArg == null) throw new ArgumentNullException("myArg", "Argh! You simply *cannot* use a null string here!!"); if(myArg.Length == 0) throw new ArgumentException("Empty strings just don't make sense!!", "myArg"); /* Other Stuff Here */ } But recently I've been being a bit lazy with my ArgNull exceptions; throw new ArgumentNullException("myArg"); OK, not lazy as such, but I've kind of realised adding the message isn't really needed, there's not a lot more I can add that the exception itself doesn't describe, and I reckon this is what was intended when it was designed - sure - you can add a message - but why bother? The ArgException does need the message, and it's far more important than the paramName argument because there are so many different reasons for the exception to be thrown - too long, too short, too many letter 'e's etc. By making the message the first argument, a developer (hopefully) thinks about what to write there, and a subsequent user of the class can get a good reason for the exception. So, now every time I get the args wrong (which - thanks to this revelation - happens little now) I apologise to the designers for my previous curses :) 2007年10月 Build Errors: 399, Unit Test Failures: 187... Time to rethink?Being a software developer is all about writing code right, right?? But what about deleting code? At what point should a developer make the hard (and it is hard) decision to rollback that afternoon's / day's / week's work. Plugging away for a long time is something I would have thought a lot of developers were familiar with, and it's tempting to just keep on going, destroying the developer soul inside you and becoming what is known as a 'cynical developer' - or - 'senior developer'. ;) We've all been in the situation where you implement a new piece of code and it (seemingly) breaks every single piece of code that exists in the company, you do a quick 'build' check, and get only 23 errors, not bad eh? Fix them, get down to the last one - fix that - build, gah! 399 errors?!?! Where the f*ck did they come from? What to do??? Well - what are the options? 1. Continue on the route to success. - The design must be right, you will succeed!!! 2. Rollback / rethink - to hell with it! - It's only been a day so far, why waste more?? Each of the decisions has it's pros and cons, option 1 will likely be a hard slog, but ultimately pleasing to finish. Option 2 might lead to a quicker, nicer implementation. Of course, option 2 might also lead to you having to redo the work - which is why before rolling back it's worth copying the folder with your current changes in to another folder, so you can just go back to it as soon as you like / need. Each decision is based on the current scenario, there will be cases where doing option 1 is right, and where doing option 2 is right, and I think being a good developer is being able to figure out which of the two you should choose. 2007年10月 Code smelly??? Well... yes, yes it is...There I was reading an ex-colleagues blog site, and came across a recent article about the downside of Code-Reviews, and why they don't work. But this got me thinking, why does he frown on the code review process? I used it in my last position to great effect. I found it provided a fantastic support role for less experienced developers (like myself) and for the senior developer at the same time. The major benefit to the senior developer has to be getting the code changed. It's very easy to sit there talking to your peer developer essentially bad-mouthing the inexperienced developer's code, particularly if all you do is look at it, swear at it and then don't bother mentioning it to the inexperienced dev (I fear I'm guilty of doing this on many occasions). The code review process allows the senior developer to first ask why code was written a given way, and secondly suggest better (?) ways of achieving the same goals. The benefit here is that the senior developer gets an opportunity to get the code changed, and the junior developer learns and progresses (hopefully). Code reviews can also allow the senior developer to learn from the junior developer. Quite often (in my experience) I had kept much more up to date with new technologies and framework (.NET) internals, meaning I used bits of the framework, or tools, which the senior dev didn't know about. The benefit the junior developer gets from this role-reversal is that the senior dev can reign in the junior, and actually get a product out, rather than constantly in a loop of newer and newer technologies being deployed. Personally, I agree, Code Reviews do have limitations, and in general they're best for reviews of how the code is implemented, (using StringBuilder rather than string concatenation for example). They can make everyone (in the team's) code much more inline with each other, and that can only be a good thing! 2007年8月 Developmentor - Essential ASP.NET 2.0 - In (slightly more) depth - Part 2 - Configuration & Custom ControlsConfiguration This is something I confess I should have known a bit more about than I did, it's not *massively* different to the WinForms version. However, as it happens I hadn't ever really used the .Config files, so learning from (almost) scratch it was. Brilliantly easy is all I could really say - the configuration API makes it a doddle to get stuff in / out of the config. The structure makes sense and the hierarchy is good too. The fact that you can do config for a given page, or even a sub dir is great stuff. But, onto how the course taught it, basically - started with a basic config file, extended it to show the hierarchy, and then progressed onto how to read and update the data. We next worked into the Custom config sections, which are pretty cool in themselves, allowing us to write custom sections defined how we want, and how to write the classes to handle it. The final part of the config lecture was about protecting the config - important if you're storing the connection strings etc! Overall this was a good lesson - good coverage of what was needed, and showed us how to do basic stuff, but open to reading in more complicated things. Custom Controls It must be noted that this is the first of two parts on Custom Controls (though having said that - we never got to the second part - which was classed as a 'if-we-get-there type lesson). I had messed around with custom controls on my "out of office" web site I was working on. Basically the lesson covered the architecture of the controls on ASP.NET and then onto how to build and use the controls. We began with a coverage of the Page, viewing it as a control though, instead of the previous overview-y way - explaining what constitutes a Page etc. We next entered the realms of creating custom controls - again - something I've done in the past - in WinForms and briefly in WebForms. The curious thing here was how much time was spent doing it by code, rather than with the designer. I can understand this for the first one or two, but for any complicated control surely it's easier to design it via the IDE - it's a pretty good IDE for doing that! We covered adding in specific configuration for controls, and how to deal with it, then we covered how to use the ViewState etc. To be honest a lot of what followed pretty much mirrored WinForms controls - creating composite controls etc adding events and properties. Basically if you can do that, you can do the WebForms equivalent. This was a good topic for beginning control design, and pretty much applicable to WinForms and WebForms, (I guess with different base classes) but if you're using the IDE it's a pretty similar affair. 2007年8月 Developmentor - Essential ASP.NET 2.0 - In (slightly more) depth - Part 1 - Overview, Architecture and WebFormsSo, it's been 2 weeks since I attended the above course in London and said that I would go over it in a bit more depth. I'm at a loose end at the moment and think I'll get right on to it. Overview. Firstly, the course is a 5 day course up in sunny/rainy London (cross out for your own experience) run by Developmentor UK (www.develop.com/uk). Ok, the course itself is actually run in the US as well in a variety of locations, and I assume the course is pretty much the same. The instructor for my week was Niels Berglund, and there were oooh, 12 of us in the class, so not too full. A wide range of abilities there, from those with no C# experience, those who'd develop (and do still) in ASP.NET 1.x and ASP (old skool) and people who'd had little or no ASP.NET experience whatsoever (me). It also stuck with the traditional 'computer' demographic of one female in the group :) The course is run in a kind of 'university' (or at least my uni) way - albeit in a more compressed fashion - a lecture on a particular topic, followed by a lab session. With the lectures lasting anywhere up to 2 hours, and the labs usually 1 hour. Architecture. The first topic is all about the Architecture of ASP.NET, a good place to start - it's always important to learn how the things you're doing work, not just how to make them work. The information covered in this section was a basic overview of how the interweb works with basic pages, inspecting request / response information etc. Then we expand into dynamic content - which for use is ASP.NET. A discussion on how the ASP.NET runtime works with IIS (which is handy) - discussing the handler aspect of it etc. Even showing how to write our own Handler (using the IHttpHandler interface). We then covered the HttpApplication events, BeginRequest etc, and the order they're fired in. Things I've come across with my limited experience of ASP.NET, and good to know the order. Kind of like the Form events I guess, generally made sense. Next was the usual (and expected) 'Hello World' ASP.NET page :) followed with a discussion over code-behind pages etc, classes, inheritance - stuff you really ought to know before developing pages - of most interest in here was how to use the 'Inherits' tag in the <@ Page> element, which proved useful when converting the default demo aspx pages into code behind pages. A brief coverage of precompilation and the deployment structure of a site followed and ended the topic. To be honest, I never really thought of the precompilation aspect, whenever I've 'developed' a site, I've always done my usual WinForms technique of SHIFT+CTRL+B, precompiling be default. Lunch Sandwiches Web Forms The building blocks of ASP.NET, the equivalent to the aptly named 'WinForms'. Basically this topic covered things like the page lifecycle, (events etc), in more detail than before. Basic event handling (again not above how WinForms do it, but with that crazy 'AutoEventHandle' thingy. Web controls were covered briefly - but then this was the Forms topic, not the controls topic (which was to come later), how to += to the control events - exactly the same as WinForms (ace). The next area was the ViewState. An area scary to the WinForms developer, an unknown, a there-is-no-equivalent-in-WinForms object... Topics like disabling the ViewState and even enabling it. That pretty much covered the WebForms topic, not a huge topic, and a lot of it I sorta knew from my practicing (except ViewState stuff), as they most closely link to the WinForms I'm used to. The Day Overall? Fairly slow to be honest, and I guess it's important to be honest about how the day went. I think a lot of the slowness stemmed from the fact that I had messed around with these things before, and from my perspective we were covering the beginning stuff for everyone to get up to a similar standard. The lab work was pretty easy - again - probably down to my limited experience, and the fairly easy concepts introduced. 2007年7月 Essential ASP.NET 2.0 - Day 5The final day.
Here we go, today has already covered Caching and Diagnostics and Debugging, with AJAX being briefly covered this afternoon..
Curiously for the debugging topic, using Visual Studio was ignored, so no inline cool debugging, but mainly using Trace etc to keep track of things, so more of a production debugging example I guess. The caching was interesting, and certainly will be something I will mess around with for my OOO ASP.NET site.
There was some wierd 'casting' code that popped up during the course, which I'll write about later, but just to see what others thing - if yuo saw the following code:
object o = null;
string s = (string) o;
What would you expect to happen? 2007年7月 Essential ASP.NET 2.0 - Day 4Day 4, a heady mix of Master Pages, Themes, Navigation and State management...
Kind of all UI elements, well, except for the state management... But all in all, a good day, I feel that I've gained a lot of background knowledge on the stuff today.
I kind of think that's the point of the courses really, to expect to go on a course and walk out knowing it all is I think a little optimistic. I think (well, from my point of view) you get a good grasp of where to look for things, and what controls etc actually do. I know that when I come to put these days into practice I will still spend a long time looking through my ASP.NET book (Programming ASP.NET) working out how to do things, but at least I will have a rough guide. Presumably some people can go to these things, soak it all up and walk out a pro in their chosen course, but they have to be few and far between - right? Right???
I'll write more on my full overall coverage of the course somepoint soon - probably Monday - and you never know - opinions can change... but for now - over and out! 2007年7月 Essential ASP.NET 2.0 - Day 3 - Part IIOh yes, two posts, one day, exciting for all you readers out there (hello Steve), so what has happened you ask? Well, Security has happened. Oh yes, Forms based, Windows based, None based. We even briefly covered Passport authentication, (when I say briefly I mean that it was mentioned that Passport auth existed). I have to say - I'm pretty impressed with the Forms authentication we've (mostly) delved into, it seems pretty powerful - and (fortunately) pretty easy to get into... I'll probably write about all the topics a bit later on, saying where it went right, wrong etc etc but wanted to dazzle you all (again, a quick nod to Steve), with two posts.. Put a spork in me, I'm done. Essential ASP.NET 2.0 - Day 3Welp, Day 3, and yes things are speeding up nicely, already done and dusted with Validation ( a nice easy topic ) we're onto Data Binding, a big'un. It's good though - these are the issues I had when I started writing my ASP.NET version of the internal Out of Office system. How to view only specific columns etc. I managed to cope with this in my OOO page by using some controls the company had bought (XCeed UltraGrid (I think)), which gave me an easier and more intuitive (from a WinForms point of view) API. I've always found the DataGrid controls to be unwieldy, but now I'm slightly (emphasis on the slightly) coming around to them... Next up is security - I'm interested in this a lot - hopefully it'll cover how to allow users to login and link to the Active Directory stuff... (Win authentication etc).. We'll see, fingers crossed etc.. What are my impressions of the course so far? 2007年7月 Essential ASP.NET 2.0 - Day 2Well, things picked up at the end of yesterday, resulting in a couple of 'lab' sessions - based on the Architecture and the Web Forms aspects. To be honest, the Web Forms is certainly the stuff that interests me the most... I'm currently working on getting the labs stuff setup on this machine - which will give me more opportunity to practice - I have run into SQL Express issues though - just got the install - so fingers crossed I can get it working! Anyhews, today has started with a 'review' period, (hour or so) and now we're onto 'Configuration' (Web.Config etc). Custom controls and Validation are next... Looking forwards to those - I've only tinkered with custom controls briefly...! Never validation! 2007年7月 Essential ASP.NET 2.0 - Day 1So, here I am, at the essential ASP.NET 2.0 course run by developmentor. This is a course I won from the DevWeek conference I attended ooh, 4 months ago now... exciting stuff. Why did I choose ASP.NET over say WPF, C# 3.0 etc? Well - basically I wanted to get experience in the 'other half' of .NET, the bit I feel I'm missing. Anyhews, so far - Day 1, seems to be going ok, we're starting slowly with the architecture of ASP.NET - important I guess if you actually want to be able to code properly you gotta know the basics. It is a 'slow' bit, as there is little to no coding at the moment. Though I suspect this will increase soon! More info later.... 2007年6月 Synchronizing with external changes...Has anyone else seen this message? It appears when go back to my Visual Studio instance containing my (admittedly too large) solution of 170 odd projects (or maybe it's not too large??). The mouse cursor goes into 'busy' mode and I basically can't do anything with visual studio (it goes into 'Not Responding' mode - hence the pale look of the screen shot). I use Sourcegear Vault as my source provider, and also run 'Resharper 2.5' and the SQL 2005 addins in this instance. It's really frustrating, and I haven't seen anything online about this, i.e. I have yet to find anyone else who has discussed it. A couple of my colleagues here do suffer from the same problem, so at least I know it's not isolated to me. Anyone else suffer from this pain? 2007年5月 WinForm Panels and the Designer - GRRRRRI've inherited some user interface (UI) code created by another developer a while ago, and it's a pretty standard affair - you know the sort - functional. It doesn't look pretty, but it does what it says on the tin and well, that's all it needs to do. Anyhews, one of the 'core' features of the UI is how a 'set' of controls (nicely wrapped in a panel) are displayed based on the selection of a drop-down box. Now, one of the inherent troubles with this design is the way these panels are displayed - one behind the other. This makes for interesting times when using the WinForms Designer. Specifically what I had done was move one of the panels (we'll call 'A') to see the panel behind it (sticking with convention this shall be 'B'). After doing the editing I needed to, I dragged the panel A back over the B, ensuring the positioning was dead centre - easy to do with the VS2005 designer. So, time to start the form, ahh. Panel A is visible, B is invisible, no matter what the combobox says. Curiously, it doesn't show anything at all, just a blank panel. This isn't good - I didn't really want to have to undo all my changes. Needless to say - a long drawn out procedure of checking my code, running through the debugger began. Yes, the panel was having it's visibility changed:
but, stepping through the debugger, the actual value wasn't changing... Why not??!!? Ok, has my code made it gone wrong? - lets try seperating the panels, and putting them next to each other -> start debugging -> works fine... hmmm, ok retry dragging B back into position. Debug. @#$%!! Not there again. What's going on??! Then suddenly it hits. It is me, when I've been dragging the panels, I've been dragging panel B not over panel A, but into panel A. Panel B was actually inside Panel A. Whooops. I guess because the size of the two panels was exactly the same I couldn't see the fact that B was inside A. The solution - ensure that neither was in the other, then set the Location property of both to be the same, rather than drag it. Nuts. 2007年5月 .NET 3.5 Installation IssuesRecently I've downloaded the 'Orcas' Beta 1 release of Visual Studio, so I can play around with .NET 3.5 and the new Visual Studio. I chose to install this on a VPC, good practice and all! So I setup a Vista Enterprise VPC, from scratch, got it all setup, added it to my companies domain and then set about installing Orcas. Then I ran into trouble, I kept on getting an error when attempting to install:
I searched through many a google result, many a 'Live search' result, but to no avail, I found lots of people who had, had different problems installing, but no-one had mentioned my problem... The question really was; Which service wasn't running? Obviously this would have been easier if the error message had actually mentioned this! I found a lot of people saying that they needed to install the vista updates from the Orcas CD, so I tried to do this, but again got the 'Service cannot be started' message. WHAT SERVICE!!!! Right, ok, these are 'updates' to vista, so the liklihood is that it's in the Windows Update, so I can at least give that a go... Start -> Windows Update, Error! This isn't good - have I damaged Vista in some way??? Best hope not... Fortunately this time I got an error code - ( though admittedly I've forgotten it here :( ) - however the help on Vista found the error code perfectly, and stated that the Windows Update service was stopped. Ok, restart it then - Services -> Windows Update Service -> Disabled... Disabled? Marvellous, then it suddenly clicks into place, presumably this is the service that .NET 3.5 and Orcas have been complaining about, and the service is disabled as a policy of the company. Right, so remove the VPC from the domain, and stick it into a workgroup, reboot, Windows Update - YAY - we're back in business. Now running the Orcas setup it appears to be running, and yes, literally as I just typed this the 3.5 framework has been installed, so fingers crossed the rest will work just fine. 2007年4月 Time zones in .NETThe problem: converting a given string representing a date and time to a UTC time. Time zones are funny things, annoyingly complicated and frustrating to program with. Obviously there are solutions and workarounds, mainly working with UTC (Universal Time Code - which is the same as GMT (Greenwich Mean Time) and WET (Western European Time) and I'm sure another 100 or so others!). For those who are taking their first steps into the minefield of time zones in .NET they might be lulled into a false sense of security with the 'TimeZone' class available to developers. It seems promising, hell, a whole class called TimeZone! Easy money - surely something like: TimeZone est = new TimeZone("EST"); //-4/5 hours from UTC Or something like: DateTime estTime = TimeZone.Convert("GMT", "EST", gmtTime); But no, we unfortunately don't have this capability built in. It's a bit annoying, as we know the hosting O/S of the framework (be it *nix or Windows) knows the TimeZone conversion information, we've all played with the clock once or twice in the past and seen the world displayed with all its zones. So, with the framework leaving us high and dry, what can we do? Firstly – looking around the interweb for time zone related information takes us on a tour of many sites dedicated to the world’s time zones, such as ‘The World Clock’, ‘World Time Zone’ and the ‘Time Zone Converter’ sites – generally just information providers – useful for finding out times etc, but not too practical for the programmatical (I’m pretty certain that isn’t a word) approach. Then a bit of delving takes us to the Zoneinfo page (also known as TZ), a public domain database containing the relevant time zone information. Now we’re getting somewhere. It has an impressive amount of implementations, from the GNU C Library, to Mac OS X, in fact, ‘The World Clock’ website (linked above) uses TZ as a backend. Ok, so we’ve got our database, what next? Well – looking down the list of libraries etc using TZ there aren’t any .NET based ones at all – nuts – this means we might have to create the API to the TZ database ourselves, or if we’re lucky – someone has already done this for us. So a quick search for: ‘zoneinfo C#’ brings up a likely candidate – TZ4Net Library. Ahh HA! This is more like it – hopefully with this we can do the conversions we were after! Time now for a quick program just to check this does indeed do what we’re after, and yes! It provides exactly what I wanted. The OlsonTimeZone class will use any ‘DateTime’ passed as an argument as a DateTime from the same zone. Perhaps better explained: If you create two OlsonTimeZone instances: OlsonTimeZone zoneEST = OlsonTimeZone.GetInstance("EST"); And then convert an arbitrary DateTime (in this case 9am) DateTime toConvert = new DateTime(1902, 1, 1, 9, 0, 0); We get the output: Converted: 09:00:00 So, zoneEST read the 09:00:00 as 9am EST time, zonePST as 9am PST. The reason this is important is because it makes no difference what the DateTime class thinks its own TimeZone is, whether it’s UTC or local, OlsonTimeZone will just ignore it. The sample program below goes through a collection of values stored in an array printing out some information about the time zone selected – nothing special at all. using System; class Program string currTimeZoneInfo = (TimeZone.CurrentTimeZone.IsDaylightSavingTime(now)) ? Console.WriteLine("Time: " + now + ", " + currTimeZoneInfo); foreach (string timezone in timeZones) bool isDst = zone.IsDaylightSavingTime(now); Console.WriteLine("UTC: " + utc); Console.WriteLine("Press ENTER to Exit"); All that’s left is to say thanks to Zbigniew Babiej for writing the API! |
|
|