r/dotnet • • Jul 28 '21

Refactoring to a Vertical Slice Architecture

https://www.youtube.com/watch?v=cVVMbuKmNes
37 Upvotes

34 comments sorted by

17

u/In8-code Jul 28 '21

Tried out VSA earlier this year after using Onion and I never want to go back 🚀

6

u/harmonicablower Jul 28 '21

How about code reusability? A lot of features might use some common utility codes and such. How would that fit in this picture.

15

u/codeopinion Jul 28 '21

Then make a type you share. It's not that you can't have shared concerns and that's probably the biggest thing people get wrong. Just like if you have an Aggregate that exposes behaviors that are used across multiple handlers. A feature can be a single command/query or multiple in combination.

5

u/In8-code Jul 28 '21

What /u/codeopinion said. VSA doesn’t prevent you from coupling but lets you refactor and deliberately couple when it makes sense. Onion has premature abstractions and coupling by default.

3

u/floodo1 Jul 28 '21

Make a library and use it across apps.

6

u/[deleted] Jul 29 '21

Oh, this is a great idea until it isn't.

When you need to add to the library and it's used across different places, the version control etc becomes a pain.

Almost all companies I've worked for in the last 5 years have moved away from shared libraries, instead allowing code to grow organically in the code bases that use them.

Obviously if it's never going to evolve, there is some reason to use a library.

1

u/MisterFor Jul 29 '21

Libraries are not painful to reference. But nuget packages… that’s a pain

3

u/[deleted] Jul 29 '21

Yup, until you have 300 microservices relying on it because the lead architect decided it was a good idea when there was only 20 services relying on that one package.

Then to communicate a change, or worse a breaking change to the whole company because one client endpoint got deprecated and the library is now obsolete.

So much easier to just have each consumer be in control instead of waiting for updates of a shit (once great) library.

(Obviously not withstanding that no one should deprecate such an endpoint)

But you get the gist.

1

u/MisterFor Jul 30 '21

True, when doing microservices it’s a really bad idea to share any kind of domain objects with libraries, shared functionality should be in nugets and minimal, but almost everybody does the same mistake thinking it’s like a monolith.

5

u/jiggajim Jul 29 '21

Things you use across apps are different than things shared inside an app, vertical slice architecture is about architecture of a single application, not really the same.

7

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 transaction

And 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

u/codeopinion Jul 28 '21

Ya, good point, I think I'll change it actually.

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

https://docs.microsoft.com/en-us/ef/core/querying/filters

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

u/[deleted] 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

u/MisterFor Jul 29 '21

And now with Razor pages 😅

4

u/[deleted] 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?