Where to put this business rule?

Where to put this business rule?

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


ajj3085 posted on Wednesday, March 12, 2008

Hi,

I have a rule, which is kinda odd.  My users want to be able to put a product bundle within another product bundle.  That's fine, but obviously I can't allow a circular reference to crop up.  I was thinking of having a validation rule that runs when the item is added to the bundle.  I'll also need to check again during the database update.  I'm not quite sure where to put this rule though; in the parent object, or the child.  What property would I even attach it to?

Thanks
Andy

rsbaker0 replied on Wednesday, March 12, 2008

Interesting. We have a similar issue, but are in the middle of a port from a Visual C++ 6.0 application so I haven't gotten to this one.

I'd think the rule would go with the "ParentBundleId" or equivalent property, since setting this is what would create the circular reference, so I guess this means it would go into the child.

I've also used the dependency mechanism to associate rules with unrelated properties (perhaps not even changeable) for the convenience of using the error provider (e.g. show brokenness in first column of a grid, even if the rule was broken elsewhere).

ajj3085 replied on Thursday, March 13, 2008

I've been thinking about this a lot yesterday (since I need to figure it out to move forward).  Here's what I've come up with; a validation rule doesn't make a lot of sense, and here's why.  There's nothing the user can do to fix the rule short of removing the child completely. 

So, the only time the rule could possibly be checked is when the child is first created, and during a save.  So I'm not sure that this is a normal validation rule, because it doesn't need to be checked at any time in between.

Given that, my current thought is to throw an exception, but I suppose I could still use the validation rule method, but only if I could make sure this particular rule didn't get run at some other point.

rsbaker0 replied on Thursday, March 13, 2008

ajj3085:
...
So, the only time the rule could possibly be checked is when the child is first created, and during a save.  So I'm not sure that this is a normal validation rule, because it doesn't need to be checked at any time in between.

That seems OK to me. I have validation rules that are nop's unless IsNew is true.

The validation rule would prevent the object from being saved, and if you tried CSLA would just throw an exception for you.

Side issue - If you are using a recursive implementation for nesting the bundles, then of course the data could be almost arbitrarily deep and detecting a cycle can be non-trivial from a SQL standpoint, so you might have to walk up the parent chain yourself till you get the end or find an object you've already visited.

ajj3085 replied on Thursday, March 13, 2008

Ya, I think I can switch from one method or the other pretty easily.

Bundles can contain bundles ad nauseum, but the Sql is pretty easy.  I was able to to use CTEs to make the search easy:

ALTER FUNCTION [dbo].[IsPartOfBundle] (
    @ProductId int,
    @ProductBundleId int
)
RETURNS bit
AS
BEGIN

    DECLARE @Result bit
    DECLARE @Count int;

    WITH BundlesAllParts(
        ProductId
    ) AS
    (
        SELECT    ProductId
        FROM    Product
        WHERE    ProductId = @ProductBundleId

        UNION ALL

        SELECT    p.ProductId
        FROM    Product p
            INNER JOIN ProductBundleItem i on p.ProductId = i.ChildProductId
            INNER JOIN BundlesAllParts ap on i.ParentProductId = ap.ProductId
    )
    SELECT    @Count = COUNT( * )
    FROM    BundlesAllParts
    WHERE    ProductId = @ProductId

    IF @Count = 0 BEGIN
        SET @Result = 0
    END ELSE BEGIN
        SET @Result = 1
    END

    RETURN @Result

END

SomeGuy replied on Thursday, March 13, 2008

Wouldn't you also want to filter your ProductList or ProductBundleList that the user selects from to not include any Bundles that would cause a problem?

E

 

ajj3085 replied on Thursday, March 13, 2008

I thought about that, but I decided I'd rather not get calls asking why they can't find the product they want to add. Smile [:)]

Copyright (c) Marimer LLC