user3529475
user3529475

Reputation: 71

Understanding interaction between domain layer and infrastructure layer

I am trying to understand the concept of layered architecture correctly. I think I understand most of the concepts, but there is one or two things that are still unclear to me...

So I understand the purpose of each individual layer: Presentation layer is for UI and user interaction, application layer coordinates application logic and domain objects, domain layer is where all of the business logic goes, infrastructure layer is for persistence and access to external services such as network capabilities etc.

I understand that each layer should only depend on the layers below it, and that layers should be separated with interfaces. What I have a hard time understanding though is the interaction between domain layer and the infrastructure layer...

I am developing a database centric desktop application. My initial approach was to pull all of the information out of the database into an in-memory domain model. The model would then live there for the duration of the application, and then be persisted to the database only once the user saves. I am having my doubts about this approach though... From what I have read, it seems that I should only pull data out of the database when as needed. In other words, pull a information into an object, edit the object, and push the object back into the database once done. What confuses me though, is that the objects become disconnected from their state once they leave the database. So lets say I pull object A out of the database, and edit it. Before I commit it back to the database, another part of the application also pulls object A out of the database. The same two objects are now living in my domain layer, with different references, and with different states. This just seems like a nightmare to me. Am I missing something? Should I only allow one transaction at a time to avoid this issue?

Was my initial approach of pulling all of the data into an in-memory model wrong? My application could have as many as 10 000 records, so I suppose it could lead to memory issues?

A side note: I am not using any ORM at this stage.

Any help or insight would be appreciated!

Upvotes: 1

Views: 989

Answers (1)

jnovo
jnovo

Reputation: 5769

infrastructure layer is for persistence and access to external services such as network capabilities etc.

Persistence layer is for persistence and infrastructure layer is usually a cross-cutting layer (i.e. it spans vertically across different layers) which may include stuff like logging or what you mentioned.

Your concerns regarding data updates are a classic concurrency problem, not specific to a layered architecture. There are many solutions, a typical one is optimistic concurrency control, which checks that the old values haven't been modified before updating with new values. A similar approach is using an auto-incremental value instead the old values.

Upvotes: 1

Related Questions