CodeSmith Community
Your Code. Your Way. Faster!

Code reuse for different entities

rated by 0 users
This post has 2 Replies | 2 Followers

Top 50 Contributor
Posts 77
Points 1,730
Polar Posted: 12-15-2008 5:46 PM

Hi,

For the following db structure:   Quote -> QuoteLine -> Product <- OrderLine <- Order, I want to be able reuse the code for editing a Quote and an Order. The Order entity has a lot more fields than the Quote so it doesn't make sense to combine them into one table, but the logic for adding and removing lines and products is the same.

I'm thinking I need to create interfaces that Quote/Order and QuoteLine/OrderLine inherit, which I can then write all the logic against. I am not sure how would I then use the entity service class so it would know which provider to use.

Anyone has any ideas?

Thanks

  • | Post Points: 35
Top 10 Contributor
Posts 671
Points 18,765

Well, this is an interesting one....

You could come up with your own Shared interface and it to the Entity project.  Then, in the Entity class that you can change, you could implement that interface.

The UI could then be used to generically handle the interfaces, but in the end, somewhere, your going to have to know what type is passed in and do an if....then call this service.  I just don't know how else you could do that.

Another idea is make your Service implement a generic service interface, then pass that around as well.  Just don't know what kind of UI your working with.....just tossing around ideas.

jeff

----------------------------------------------------------------------
 Member of the .NetTiers team | Visit http://www.nettiers.com
----------------------------------------------------------------------

  • | Post Points: 35
Top 50 Contributor
Posts 77
Points 1,730

Thanks for your input. I'm gonna provide more information and db structure that is more accurate as I have simplified the Order/Quote example.
I have 3 main entities (Quote, Order and Project) that have the identical child hierachy of Line -> SubLine -> Item, Line -> LineOption (All One-to-Many). I could make 3 sets of the Line hiearchy, one for each main entity that would be pointless as they have an identical structure. I also want to create code that will hangle Line, SubLine, Item and LineOption CRUD, so having a single set of these tables would be better than having to make 3 sets of code where only the entity changes (OrderLine vs QuoteLine vs ProjectLine etc).

I did some research and came down to 2 options:

1) Have 3 foreign key field in the Line table (QuoteID, OrderID, ProjectID), where one of them has to be not null to pass validation and save to database. I would also add another column for storing the LineType what will specify whether it is an OrderLine or the other 2. Extra logic has to be performed to check the LineType when working with it.

2) Create 3 joining tables (OrderLine, ProjectLine, QuoteLine) that have a One-to-One relationship to Line. This will allow me to add columns that are specific to each main entity if such requirements arise (and come to think about it, I'm pretty certain they will). This methods will require that I need to load 4 levels (OrderLine -> Line -> SubLine - Item) instead of 3.

Both methods will allow me to reuse code for dealing with the lines on the lower level and I still will be able to have full DeepLoad/Save support.

I think that option 2 is a better choice because it offers more customization.

Thanks

  • | Post Points: 5
Page 1 of 1 (3 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems