Post

I think they are totally fine. Most of these queries are driven by specific view requirements and there is no point in littering contexts with functions like get_five_recent_nonshipped_ordera_by_user. Context should be for "serious" business logic.

In your example, the problem with calling multiple context functions that not all context functions are guaranteed to be composable within a transaction. They have effects like broadcasting over pubsub, or ordering requirements etc.

Wherever you do it you’ll have the same problem. If contexts can only make data changes in a way that transactionally composes, then they can’t be where your “business logic” is. I don’t see a difference in an application layer and a domain layer.

Not all applications have exactly one database for everything. Applications will integrate with external services etc. your user registration context function might do things like send emails and track analytics etc.

If you see contexts as something similar to DDD bounded contexts, then I agree, you should not do any cross context transactions, because all bets are off. In that case though it doesn't make sense to talk about an application or domain or infra layer, because a context contains potentially all 3

I don't use phoenix contexts like DDD bounded contexts. I see them as my domain layer and phoenix/web as the application layer. Depending on what kind of application I'm building, I'm pushing infra to the outside as well. In this specific case, cross domain context transactions can be done safely.

What is the difference between a domain layer and an application layer in your usage? I don’t typically see people use them as separate terms. They are typically synonyms.

0 reposts0 likes

I have never seen these terms being used interchangeably tbh. Domain layer is what your app really does, without infra (except maybe db, for practical reasons). Application layer runs domain in terms of current context, a Phoenix app, with background jobs, pubsub, API requests etc.

I see what you're saying, and I can see *a* distinction between the two, but I don't think that is how "Application Layer" is typically used. Regardless of how it's typically used, thats not how I use it. The idea that you have some kind of stateless domain layer and that background jobs or pubsub

is a concern of a particular consumer doesn't really track for me in reality. What happens in the background etc. is part of your domain logic. What elements of your system talk to each other is as well. I know that things out there try to model these things in a sort of "pure" fashion but I don't

think its really all that practical. You have to have tons of mapping layers between some concept of a thing and the implementation of the thing. Like if you have an event oriented architecture and your app wants to emit a notification and you want to make it in some way agnostic to the medium

I define the domain layer as all the essential complexity you need in order to solve the business problems. Persistence is often accidental as it doesn't matter to the problem if it's solved with SQL, document storage or whatever.

Being able to have 2 different interfaces to the domain layer are nice side effects but not necessarily the main goal for me. This pattern with a clean domain layer is not applicable for everything. If you have a simple app, it might not be worth it.

I think it also is a factor of how hard it is to do. With the right tools it’s not hard. When the right thing to do isn’t made easy, it won’t get done.

Persistence layers are not cleanly separated. It’s not ever pragmatic to just “swap” persistence layers. You always end up leaning on guarantees of a given persistence layer to actually implement your application.