views:

55

answers:

1

OK, so I have a couple of modules in my application. One is called ProductCatalogue and another is called Contracts. We now have a need for a contract to be associated with a number of products (eg the products which a party to a contract is allowed to order). Within the ProductCatalogue module we have a ProductDAL class which has the following functions

Public Function GetProducts()
    Set GetProducts = GenerateProductsList("SOME SQL")
End Function

Private Function GenerateProductsList(selectQuery)
    Dim list : Set list = New List
    Dim results : results = GetResultsFromDB(selectQuery)
    '... for each row
        Dim product : Set product = New Product
        product.Id = results(field, index)
        list.Add(product)
    'loop ...
    Set GenerateProductsList = list
End Function

Now, I want to get all products associated with a contract so I want to write a function that looks like this

Public Function GetProductsForContract(contractId)
    Set GetProductsForContract = GenerateProductsList("SOME SQL")
End Function

My question is, where should I put this function? I want to use the existing GenerateProductsList() function as it is a lot more complicated than it looks. There my question is "Where should I put GetProductsForContract"?

My options:

1) Put it in ProductDAL.
The problem with this is that ProductDAL suddenly becomes aware of what a contract is and I can see this getting quickly full of functions such as GetProductsForAllContracts, GetProductsForLiveContracts, etc, etc (and more when other modules want to access products) so really I'd like to keep those functions with the rest of the Contracts code.

2) Put it in ContractDAL and make ProductDAL.GenerateProductsList public.
Should I really be exposing this?

3) Create a new class which one method which has the sole responsibility of taking in an SQL dataset and returning a list of products.
Actually isn't this the same as 2?

4) Stop doing it wrong.
I'm not sure how. Show me. Then hold me.

edit: Also, what about AddProductToContract, RemoveProductFromContract where does this go? I'm leaning towards a new ContractProductManager class but what's the best way to access GenerateProductsList()

+1  A: 

This is difficult to answer without knowing more about your app. I would say that if products and contracts are in the same database and always will be, having 2 DALs seems like an arbitrary separation. Since you likely have a relationship between product and contract in the database already, you are not gaining anything from separating the two in the DAL. You can place this new method into either class, imho.

cdonner
Thanks for your answer. I have only the one database but the app is huge which is why there are different DALs for various sections (product, contracts, contacts, documents, etc).
jammus