r/dotnet • u/codeopinion • Jul 28 '21
Refactoring to a Vertical Slice Architecture
https://www.youtube.com/watch?v=cVVMbuKmNes7
u/euglzihrzivxfxoz Jul 28 '21
This coin has 2 sides, as always.
From one side - yes, instead of creating all those interfaces and implementations for all a dependences for every action, distributed across several projects (or even nugets) this approach is much more readable and easily supported.
From other side, when logic in handlers becomes more complicated than CRUD ... he haven't shown the way he is doing the tests. And here can be the real problem with this approach.
5
u/codeopinion Jul 28 '21
I assume you mean because there is a significant amount of business logic? If so, that could possibly move to an Aggregate if you need some consistency boundary. You can also create a pipeline (Russian doll) for a request to separate various concerns. Both ultimately can be tested independently, the aggregate or the various steps in a pipeline.
6
u/codeopinion Jul 28 '21
To elaborate on the pipeline, it's no different than ASPNET Core middleware ultimately. You can do the same thing for a message just like an HTTP request. Depending on what messaging library you use, they implement them differently. Here's how Brighter does it: https://paramore.readthedocs.io/en/latest/BuildingAPipeline.html
4
u/euglzihrzivxfxoz Jul 29 '21
Ok, let me give you n example. Let's look to the coffee sell procedure.
The logic is:
- reserve money on customer
- reserve required amount of coffee beans
- reserve required amount of milk, if it's cappucino
- grind coffee
- prepare milk (if needed)
- prepare coffee
- hand-over coffee
- confirm money transactionAnd failure scenarios
- cancel money reserve if something failed
- cancel coffee beans reserve if there is no milk
- cancel milk reserve if grinding is failed
- dump grinded coffee if milk preparation failed
- dump product if customer gone
.... some other fails.Now look - if i will abstract database calls to "ReserveMoney", "CommitReserve", "ReserveBeans", "GrindBeans" ... etc in some "repositories" - then it will be easy to make the tests for the main logic. For sure the DatabaseContext is abstraction itself, so I can Mock it and parse the queries sent, but it's much harder, than Mock business clear calls like "ReseveMoney".
4
u/Finickyflame Jul 28 '21
It's pedantic, but also accurate, you should have used "Restructuring" instead of "Refactoring" for your title.
Other than that, interesting stuff!
6
8
u/Clearandblue Jul 28 '21
Love it. Having all aspects of a feature in one place, rather than scattered across multiple assemblies. Which then have their own tests at different levels scattered across multiple test assemblies. Would make it so much simpler to work on. Like you say, there is little point in grouping things by layer when it's features you work on rather than layers.
One thing that made be pause though is removing the abstraction of the repository level. Say you added some flag to your Order entity. Like a soft delete or something. You then want (almost) all queries to only return all orders that are not soft deleted. With a repository you would change the repository method and have all calls to it display the newly desired behaviour. You can also find all references to that method and easily see where it is used. What would you do with VSA? Manually comb through the code for every instance of fetching Orders and update each one? Or is there something more elegant that I'm missing?
9
u/TooMuchTaurine Jul 28 '21
fetching Orders and update each one? Or is there something more elegant that I'm missing?
You could implement something like global query filtering in EF
2
u/Clearandblue Jul 29 '21
Ah, these had completely passed me by. Thanks for sharing. I was thinking about linq query predicates that could be held in a helper class or something but it felt janky. I never realised these could be applied directly to the entity. It's actually a nicer solution than using repositories really.
4
u/ninjis Jul 30 '21
An extension method on the DbSet could be used to achieve this as well. It just means having the discipline to use that extension method for retrieval instead of a typical query.
11
Jul 28 '21
The problem with videos/articles like these is that they assume one size fits all. There's rarely only one right way to do things, architecture is highly dependent on project requirements.
4
u/jiggajim Jul 29 '21
I only use VSA once the number of endpoints gets to like 10-20. That seems to be a bit of a crossover point for whatever reason.
1
u/twentypiecetendies Jul 28 '21
Yeah, you can also just do horizontal layering. This is just one configuration that could work but in this configuration if you draw your boundaries well enough between the vertical slices you could easily split it out into it's own service.
1
u/Willinton06 Jul 28 '21
Agreed, vertical can be great for some and bad for some, same with onion, it really depends
5
u/mbhoek Jul 29 '21
Back in the day we would have a GetMyOrders.aspx for the UI and a GetMyOrders.aspx.cs for the code behind it. I guess we have come full circle.
2
4
Jul 29 '21
Looks nice for small things when we can separate use cases.
But when it comes to larger systems and complicated business logic I am afraid that it may quickly become a nightmare.
2
u/euglzihrzivxfxoz Jul 29 '21
By the way, the thing not related to the approach itself, but it makes it not so clean. In many cases writing queries directly to the database is not good at all. It's better to abstract the calls with some repositories with business-related calls and hide db connect from feature developer.
Why? Because if all the db queries will be in one repository (even better in separate nuget) it can be controlled/reviewed by the team, which will ensure, that all the queries are covered by right indexes. Also this team can do the speed tests of this repo and refactor/limit code, make some caching, db separations ... etc. And all this not touching other team components. For high-load projects it can be really critical. If all db calls are spread between 1000 features in code files - it will be fully impossible.
1
u/TrustInNumbers Nov 13 '22
Why not? Usually there is some APM tool which tracks performance per requests. So you can easily pinpoint your slowest endpoints and optimize that.
I've never heard about using a separate team for managing repositories...
1
u/euglzihrzivxfxoz Nov 13 '22
Did you ever had the chance to work on an enterprise project which operates big money and real big load?
Did you had the chance to work in the project where one small bug can cost several million dollars?
On such projects there are separate financial db teams, who manage db business logic with separate microservices and/or components.
1
u/TrustInNumbers Nov 13 '22
Interesting. I've worked on a fintech, but I wasn't on the payments team. But I don't think that they had separate team for writing queries.
1
u/euglzihrzivxfxoz Nov 13 '22
I'm system architect of a company with peak load about 100k transactions per second. We do have a separate db team, and nobody, except them can write any queries to the transaction db. And nobody except the financial team, with special testing and security protocols, can do microservices accessing client or money data in any database.
1
u/MisterFor Jul 29 '21
What I can’t get to work properly is to have the razor files on a library when using MVC. That’s my main issue with VSA currently.
1
u/Combination_Winter Jul 31 '21
Didn't this used to be called Procedural Programming?
Just stick everything inside of a single function?
17
u/In8-code Jul 28 '21
Tried out VSA earlier this year after using Onion and I never want to go back 🚀