Can any of you guys give me your opinion on how to model this simple situation?
Lets say you are dealing with race car drives.
Each driver drives a specific model of car, and each model is a brand of car.
So Bill might drive a Boxter, which is a Porche.
=== skip this part if you don't care about the DB schema ===
In the DB, the Driver has a CarModelId value that is a FK to the CarModel table. The CarModel table has a CarBrandId which is a FK to the CarBrand table
So each driver has a car model, and each model has a brand.
=== end DB part ==
Now, when creating the Driver class, and the form to edit the drivers info, would you say that the Driver is the parent of a Car object, which contains both the Model and Brand, or would you just have the Driver contain 3 properties: iCarId, sCarModel, sCarBrand ?
If you think I should use an object instead of the 3 properties, how would I "mark" that object. I don't think it should be readonly b/c I want to be able to change what car is assigned to that driver, but I don't want to update the car object itself (e.g. I want to change Bill from driving a Boxter to driving a 911; I don't want to change the NAME of the Boxter to "911" in the database)
Any suggestions which path would be corret?
One more thing to keep in mind, that I think has been lost so far, is that the car's model & brand are singular to the Driver. You wouldn't want to be able to assign a Boxter model and Ford brand. The DB model you described does tightly-couple the brand to the model, so a Boxter directly corresponds to Porche as would be expected. But, from the driver's perspective, he/she is assigned a Porche Boxter and the user should not be given the option of assigning from a list of models AND a list of makes.
With this in mind, I would agree that the sample app included in the book describes this scenario very well and your model will suffice for a very basic application. You should look at the N-to-N approach; however, as it probably models real-world better.
Anyway, what you'd have is a Driver object with a Model property that references the particular object assigned to that Driver. In your drop-down list of cars to assign to the driver in your UI, a read-only ModelList can be set as the data source. Set your unique key as the drop-down list's key/value and the ToString() method for the text. Then, override the ToString() method in the Model object to:
public override System.String ToString()
{
return Make.ToString() + " " + this.Name;
}
The result will be a list of names like "Porche Boxter" which will make more since to your users when assigning the cars.
Hope this helps.
I understand that N-to-N cardinality might seem like a good way to design this, but in the context of my application doesn't really apply.
Unless I missed it, the examples in the book use collections where the child object knows which parent it belongs to.
So if I was going to say that each driver could have a collection of cars, and I added an extra table to the DB to do this, then the example would line up pretty well.
But, instead of having a CarAssignment table, or something like that, the Driver has the CarId as part of his table, b/c he can only have 1 car.
So is it correct that what I should be doing is
1) create a ReadOnlyChild object to represent the Car (meaning that you can't change the car name or brand)
2) make the property on the parent a read/write (meaning you can assign a different car)
Does that sound right?
I built a form to edit my Driver, but now I have a problem with editing.
I had planned on having a car dropdown list to select from, built from NameValueList, but I don't think I can do that b/c my driver has a property for Car, not a property for CarId.
So should I be databinding against a dropdown that has a collection of Car objects, rather than a NameValueList?
Copyright (c) Marimer LLC