0

I have a website which lists expenses filtered by year and allows for editing. Over the years, the expense entity evolved. Some properties were removed and some properties were added. If you go back and filter by a previous year, my website displays the expense by how it looked at the time.

In my database, all the different versions of the expense are stored under one table. If a property is removed, the associated column is kept but set to NULL for new entries. If a property is added, a new column is created and historical data is set to NULL.

The logic to determine which columns should be shown is currently in my presentation layer. Same thing for which columns can be edited.

Am I wrong to use the presentation layer for that? Should I split my expense entity by version in a domain layer so that the presentation layer only manipulates a single version?

Current design:

Infrastructure Layer:

ID, Amount, Vendor, Municipality, Purpose

Domain Layer:

ID, Amount, Vendor, Municipality, Purpose

Presentation Layer:

Year 2020: ID, Amount, Vendor

Year 2021: Id, Amount, Vendor, Municipality

Year 2022: Id, Amount, Municipality, Purpose

1 Answer 1

2

Version the data.

How you store the object, relational db, one table, many tables, no-sql, whatever doesn't matter so much as whether you store the version of the object with that data.

For example, in your current scheme you cant tell the difference between a valid 2020 expense and an invalid 2022 expense with missing vendor and purpose.

You will also doubtless get requirements where the version is important.

  • Upgrade all the 2020 expenses to 2022 by adding purpose="old" and municipality= vendor
  • Report on all expenses by Vendor

Obviously if you version all your data ever, just in case you have backwards compatible requirements its going to be a pain. But versioning can be a life saver when you have data where you can't translate from old to new versions or where the integrity of the originally entered information has to be preserved

2
  • Hi Ewan, I forgot to mention that I do have a date field for my expense object. I have no issue determining which version of my expense object I'm currently manipulating. What I'm wondering is whether I should have a domain layer entity for each version of my object? This would remove a lot of business logic from the UI (e.g: show purpose if 2022, allow editing of purpose if 2022) but will clutter the UI with duplicated code (e.g: a, edit form for 2021, an edit form for 2022). Commented Sep 3, 2022 at 14:25
  • Its is just a data object you can prob get away with a generic one. Once you start adding methods to the object you prob need to split it. Relying on the date to determine version still means you have to have that date=>verison mapping somewhere, plus surely you have edge cases, "submitting a back dated expense", "office B started using the new expense form before office A", "test expenses" etc etc Commented Sep 4, 2022 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.