Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

angular - Can we extend a model with the same ease as using an extraProperties Map, using the NGXS patterns?

Background

We are planning to use Angular whatever-version-we're-on-now, Typescript, NGXS. Still in the API design phase, so we don't have many concrete code examples.

My team is having a philosophical argument over using an "extraProperties" Map to add more properties to a basic "ListItem" type vs. extending the ListItem type into specialized types ("FooListItem").

My teammates prefer the map because they believe it will lead to less work/less code because we can write functions that apply to all ListItem and those functions can look at the extraProperties.

I prefer extension because we get type checking, a documented contract in the API, etc.

I'm also hopeful that there is a way to do this with equal or less up front* work. In my naive world, we could have a class ListItem implementing basic logic and extend that to FooListItem which can override the basic logic with its own functions as needed. But in the ngxs world, our model should be separated from our logic.

*I'm firmly convinced that it will result in less long term work, but that's a much harder argument.

End background.

If we extend ListItem such that subclasses have extra/separate logic that applies to them, where would this logic go (according to the NGXS pattern)? Can we do it in roughly equal up front work/code to using a Map?

For a specific example, consider a function that returns the display value for an item to show in a list. For our basic ListItem type, we want the string "value - description". For CityListItem, we want "value - description, stateName" where stateName is an additional field.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...