Last post Jan 06, 2021 03:52 PM by bruce (sqlwork.com)
Jan 05, 2021 10:00 PM|edhalsim|LINK
I hope this isn't a dumb question. I have a web application, written in Blazor, that calls a RESTful web API. I want to share classes on both the client and the server, but some functionality is needed on the server only and some on the client only. What's
the best way of doing this (or is there a better approach)? Would I create an abstract base class that contains all the common elements and then create concrete classes for both a client version and a server version? For example, I have a GameUpdate class
that's used on both the client and the server that has a Save to database method that's only applicable when it's on the server.
Jan 06, 2021 02:08 AM|bruce (sqlwork.com)|LINK
The api classes should be poco objects with no methods. Then use extension methods instead. You can have common, client and server extension methods.
Jan 06, 2021 03:20 AM|YihuiSun|LINK
Would I create an abstract base class that contains all the common elements and then create concrete classes for both a client version and a server version?
You can create a .NET standard class library project called
BlazorDemo.Models in the same solution.
After adding references to the BlazorDemo.Models project from the
Blazor Web application project and ASP.NET Core RESTful service, you can
use these model classes(in the BlazorDemo.Models project) in the Blazor Web application project and ASP.NET Core RESTful service.
I found a tutorial with detailed examples, you can
click this link to view it, it should be helpful to you.
Jan 06, 2021 03:23 PM|edhalsim|LINK
I like this idea. How would you handle properties or private variables that the class needs on say the server, but not the client?
Jan 06, 2021 03:52 PM|bruce (sqlwork.com)|LINK
You can use partial classes or inheritance to add private properties to the server version. But it sounds like you want to break SOC and tightly bind the client and server code. You really should be using interfaces and extension methods.