c# - interfaces to fix inheritence mess? -


i'm working on project following (very simplified) structure:

baseclass  subclassa : baseclass subclassb : baseclass 

there ui (with lot of logic) uses subclassa, , saves component takes baseclass parameter but casts argument subclassb. fails ui passing in subclassa.

ui:

mycomponent.save(subclassa)  component: save(baseclass baseclass) { subclassb subclassb = (subclassb)baseclass; 

...

the current implementation creates instance of subclassb in ui , pass across - leads lots of code such as

subclassb.property1 = subclassa.property1a 

i'm contemplating creating common interface 2 sub classes implement. lot of work think flatten current deep hierarchy. reworking either ui or component use other sub type work structures different (though many fields map). interface approach right way go? feel there might i'm missing.

if subclassa , subclassb related ability save, yes, baseclass better interface both sub-classes implement.

it won't solve immediate problem straight away: component casting base class (the wrong) derived class. looks there several levels of refactoring here. patching code component casting subclassa making 1 use wasteful, think. changing component can operate on single common type big win there.

flattening deep hierarchy bring lots of other benefits, - making simpler. if there end being few interfaces implement, that's not bad thing. beware of lots of interface types hunting in packs, however.

in short, reworking both ui , component - , other code, - work in terms of small number of interfaces, no knowledge of implementing classes, pay dividends.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -