Monday, March 12, 2012

How to handle foreign keys when using DataObjectTypeName

If you have a table that has a foreign key, is it good practice to have a property in your class for that foreign key? I ask this because right now I haven't, and so I have declared a method to insert a record like this:

public static Insert(OrderItem orderItem,int orderId) { }

And I was thinking I could hook this up to an ObjectDataSource that looks like this:

<asp:ObjectDataSource ID="OrderitemDataSource" runat="server" TypeName="OrderItemManager" DataObjectTypeName="OrderItem"> <InsertParameters> <asp:QueryStringParameter name="orderId" QueryStringField="orderId" /> </InsertParameters></asp:ObjectDataSource>

But that will not work, because it seems that, as soon as I specify a DataObjectTypeName, the ObjectDataSource will ignore any extra parameters. It will look for:

public static void Insert(OrderItem orderItem) { }

That means that, in order to insert the foreign key value, it need to be a property of OrderItem. Is there another way around this, or is this the way you'd solve it too? The reason I am not sure, is because in OOP, it's more naturla to have a property of type List<OrderItem> on the Order class.

Thanks for your time!

Hi,

If you have a DataObjectTypeName set, the ObjectDataSource will create an object for this type and set property value for each of these parameters.

I think if you're having a reference to another object in this type, it is fine. It works like a foreign key.


Yes, I know. But how do you feel about this from an OOP perspective? Don't you usually have it the other way around (object Order has a List<OrderLine>, object OrderLine does not have an Order). Foreign key is a database term, OOP should not be coupled to the database.

Labels: , , , , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home