Posts

WeakReference woes!

Take a look at the following code if (myWeakReference.IsAlive) (myWeakReference.Target as SomeClass).DoSomething(); Do you see the mistake? The WeakReference may return "true" for IsAlive, but because the garbage collector runs within its own thread the value may actually get collected before the next line (because WeakReference.Target does not prevent the GC from collecting the value). I've been using WeakReferences quite a lot recently so I was happy to see that the following changes fixed the occasional NullReferenceException occurencies I had been seeing which were quite difficult to track down! SomeClass myInstance = (SomeClass)myWeakReference.Target; if (myInstance != null) myInstance.DoSomething(); The IsAlive property in my opinion is utterly useless (it is the same implementation as Target). I think MS should just remove it, you could say it would break existing code, but I say it would force people to fix it!

Validating XML against an XSD schema

I have a new job. Well, I say "new" but I actually started in December 2005. Anyway, this "new" job requires me to develop compact framework apps. Seeing as Delphi doesn't support CF I am developing my application in VS2005, it's a really nice tool but I miss ECO so much! One thing I had to do was to write an XML importer routine. This would retrieve an XML file detailing tasks due for the next seven days and then import it into the PPC's database. Because I am not responsible for generating the XML, and because it is good practise anyway, I decided to create an XSD file to validate the XML. PDA's have very little storage resources (the flash card is shared between disk and memory) so I decided to read the XML file a line at a time using the XmlReader so that the contents don't exist on the flash memory twice (disk + memory), another benefit of this approach being that I can read it directly from a ZIP file too! In dotnet V2 the class XmlVa

Roles

Image
What is a Customer? If you are some kind of business service provider then it will be a Company, if you are a window cleaner then it will be a Person, but if you are a travel agent it could be either. What about a Supplier? You could argue the same as for a Customer, but you could also argue that a Company/Person could be both a Supplier and a Customer. This is where Roles come in. A Supplier isn't something you are, it is something you do, you supply something; just as a Customer consumes something. I find that it is much better to enable "Things" to perform more than one action, it is rare that life is simple enough for everyone to perform only a single role in life. The typical solution to this is the Party/Role pattern. A good point to make here is that this is a pattern, not a set of classes intend for inheriting from! This means that when you have a Company (alias "Party") you should create a CompanyRole class and associate them, descending Custome

ECO is so fast!

I spent the day (March 2005) training someone how ECO works. Between 9:30 and 12:30 we went through the basics, how to create a model in a package (and why). We then covered the different component types, whilst making a simple client/server app. Between 13:45 and 17:00 we went on to Create a server application Convert the client to connect to the server instead of directly to the DB Make the client synchronise with the changes from the server Create a web service which connected to the server for persistence Create a website which connected to the server for persistence That's a lot of work to get done in 3.5 hours at the best of times, but when you are also explaining why everything is done the way it is then this is surely a reflection of just how quick it is to develop applications using ECO. Everything we did was adhoc. I had no idea what the trainee wanted to cover before he arrived so everything was written from scratch. I'm quite frankly surprised that we managed to

What is ECO?

This question has been asked so many times. Jesper (one of the developers) recently explained it like this... ECO, Enterprise Core Objects, is a model driven framework. In essence, it allows you to specify your application using a UML class model. This model is then transformed to source code, decorated with enough information to re-create the model information at run time. The framework uses the model information (as contained in and re-created from the source code) to drive persistence, presentation, maintain the technical integrity of the business objects, manage bi-directional relations, derived associations and attributes, maintain constraints, offer services like undo/redo, object versioning and quite a bit more. The value added proposition for you is that you can design your application on a 'higher level', without worrying about implementation details. Use the model not only for communication of ideas, but also as a part of your application. You can code your business

Adding runtime error messages to page validation

One of the requirements when writing a website I once created was the ability to model object constraints in OCL and then validating the current page against those constraints. The problem with normal validators is that they are designed to validate individual controls rather than a list of constraints on an object. The approach I took was to create a validator which I can place on any web form and add as many errors to as I required. The first step was to create a WebControl which supported IValidator public class MultiValidator : WebControl, IValidator { } I then added a list of strings to hold the error strings, and a method to add an error. private List Errors = new List (); public void AddError(string message) { Errors.Add(message); }//AddError When ASP .net validates a page it enumerates all IValidators within its own Validators property, and called IValidator.Validate(). To determine if the page is valid or not it then checks IValidator.IsValid. To add cu

Reverse derived columns

The focus of this post will be "Event derived columns". Jan Nordén (Borland) pointed me in the direction of these things recently when I asked him how to solve a GUI problem I had. When I used Bold for Delphi there was this really nice GUI component called a BoldSelectionListBox. This component would let me show a list of items with a CheckBox next to each row, ticking / unticking a box would add / remove an association between the object selected and some other object of my choice. Using this BoldSelectionListBox I would be able to specify a User (for example) as the context and then have a list of all Groups in a kind of CheckListBox. Ticks would appear in all CheckBoxes where the User is belongs to the group listed, and no tick where they are not part of the group. The extra clever part of course is that by ticking a CheckBox Bold would create the link object required to tie the User to the Group, and add it to User.Groups (and of course Group.Users). Seeing that ECO