Blazor wasm where to save UI models and business logic

Viewed 119

I'm working on blazor wasm project and i want to create a WPF version of it later So my question is how i can structure my project so i can change it later easily to WPF

  • Should i put all the UI models and business logic in class library so that i don't have to copy past in the WPF project ? I'm looking for any best practices to achieve that

sorry if this is not the correct place to ask this question i'm new to all this i tried looking on internet but i did not find anything usefull

1 Answers

While there is no iron-proof way to make things just work as soon as you transition your code to WPF, there are ways to make it easier on you. The easiest thing you can do is instead of putting your C# code in the @code section, you put it in a different file. When you create a Blazor page or component, behind the scenes there is a class created call ExamplePage (if your page is called Example.razor). You can just create a separate Example.cs file and inside you can declare public partial class Example. Partial is the keyword there. The compiler will see this and use the code in that class as if you had put it in your @code section.

Similarly, try to make these classes as small as possible and delegate the real code to services, which you then inject into your page. Ideally, your Blazor page/component should just be laying things out based on the model. The model gets populated by the various services.

As far as the actual Razor/UI code you write, I can't think of a good way to make sure you can easily translate that to xaml. But if your business logic is separate, that's half the battle.

Related