C
C#8mo ago
Sygmond

Is using a blazor component from a service a good idea?

I have a DocumentManagerComponent.razor that is used in multiple places across the app. It has an OnDelete EventCallback that is used for marking a row as deleted and in other places to directly delete a row from database. That depends of the type of page where the component is used, the event it used with different methods. The lead developer asked to remove this component from everywhere, place it in MainLayout, hook it up with a service (DocumentManagerService) and use this service to manage documents: open, view, edit, delete. Is this a good idea? Do you see any issues that would result from this? The initial goal was to have less code for implementing the component but now with a service, it doesn't look as having less code. In fact, moving the delete logic inside the service creates issues with the component not accepting different settings once initially set for a use case, probably because the same instance used for one page can't adapt well to another page where the service is used. This lead me to subscribe to an event placed inside the service and do the delete logic outside of the service. I looked around and I couldn't find any example of using a component as a service.
1 Reply
Sygmond
SygmondOP8mo ago
Short version: Is better to use components via a service compared with the standard practice of placing the component where is needed and using it directly? The service would have to be injected in the component and in the page where the component needs to be used. The service would hold the business logic, the component will act as UI only and any functions needed in the component would be executed from the service.
Want results from more Discord servers?
Add your server