Design or Template question

Design or Template question

Old forum URL: forums.lhotka.net/forums/t/2177.aspx


wjcomeaux posted on Friday, January 19, 2007

Hello all:

I am currently generating my stored procedures and business objects with CodeSmith. An issue I now face is that my templates for business objects is looking at all unique fields in the database and treating them as an identity (passing it in as a parameter to the Insert stored procedure). However, my stored procedure does not account for this new parameter.

For instance, I have an ApplicationRequest object that inherits Request. In the database the ApplicationRequest table has a Request_ID field that is a foreign key into the Request table.

In my business object I simply want to store the integer of the Request_ID in the ApplicationRequest and lazy load the base Request object if/when I need it (actually right now I am not lazy-loading but just loading it).

So, should be business object ApplicationRequest be trying to send in thie NewRequest_ID parameter (as an OUTPUT parameter) to the stored procedure (since NewRequest_ID is NOT the primary key on the table)? How do you guys work with this in code generation?

As far as I know, CSLA will first create the base Request object when I try to Save the ApplicationRequest. This would give me access to the Request_ID when CSLA then attempts to save the ApplicationRequest. There is no need to wait for a NewRequest_ID from the database since I already know the value. However, this parameter is generated in the business object and I'm not sure how to prevent it in the template other than by NOT adding these parameters unless the field is the Primary Key only.

Thoughts?

Thanks, Will

wjcomeaux replied on Friday, January 19, 2007

It seems to be that TemplateBase line 459 (if your version is the same as mine)

if (!prop.IsDbComputed)

is showing that the column IsDbComputed which makes the code fall through to the else statement below

outputStatement += GetParameterStatement(prop, "New", "", false, level);

This causes a parameter to be sent into the InsertProcedure of "New" + Column_Name by generating the C# code

cm.Parameters.AddWithValue("@New22Request_ID", _request_ID);

cm.Parameters["@New22Request_ID"].Direction = ParameterDirection.Output;

In my case NewRequest_ID when all I want is to send in the Request_ID (which also gets generated).

For now I will comment out this line and see what happens but I'd like some clarification on what is going on here.

Thanks, Will

wjcomeaux replied on Monday, January 22, 2007

Commenting out the line in the template causes a wealth of other problems that I didn't really want to work through, as it's obviously not the best solution. My other alternative would be to remove the unique index on that column in the database (across multiple tables) which would then mean that the database no longer enforces referential integrity (I would not be able to guarantee that the Request_ID in the NewApplication table matches one from the Request table).

Does anyone have a solution to this? I can see both sides of the coin here but I am not sure how to derive a resolution and this is a big show-stopper for me with code generation.

Thanks, Will

thomasjrr replied on Monday, January 22, 2007

Will,

  The referential integrity you refer to is "business logic" in a sense and if you are wanting to fully embrace CSLA, you would want all your logic in the business object to begin with.  As long as your object ensures that the appropriate keys are being created, the database integrity check is in my opinion just a redundant step.  If however you are not the only system using this database and you cannot trust other programs to enforce the rule, then you are forced to leave the integrity checks in the database.  Just my 2 cents...

 

Bob

wjcomeaux replied on Monday, January 22, 2007

Bob, you were spot-on with the multiple systems idea. I am writing a new set of business objects but the data and other applications are already in place so I can't really just delete the unique index here. I can in theory as I BELIEVE the other code around this database also handles the new ID rules but I can't be positive. Nor can I be positive that the next guy to come along will automagically know these rules. Therefore I really need a solution where I can have my business rules in the BOs and also still have referential integrity in the database. Ohh, and I'm lazy so I don't want to code all of this by hand. Plus if I tell me boss we can't use CodeSmith he might blow a seal.

So, what now. There has to be a way for the CodeSmith templates to accomodate this behavior. I find it difficult to believe that people are just abandoning there database constraints.

Thoughts?

Will

Copyright (c) Marimer LLC