torhovland / blazor-redux

Connecting a Redux state store with Blazor.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: Best alternative to inheriting base class

Sire opened this issue · comments

commented

I want to avoid using inheritance (@inherits MyAppComponent) because my pages and components often already use inheritance. (Plus we favor composition anyway right?)

Most of my pages inherit from class that inherits from BlazorComponent. Containing all the page/view functions, similar to a code-behind class in regular ASP.NET as recommended here.

I tried injecting and just instantiating the MyAppComponent(), neither works. Perhaps I'm missing something obvious @torhovland?

commented

Solved it. Inheriting from BlazorComponent isn't required, you can use it's interfaces instead. So in my code-behind classes I just inherit from this class that contains both the state component AND the BlazorComponent interfaces.

public class BlazorComponentBase : StateComponent, IComponent, IHandleEvent, IHandleAfterRender, IDisposable

I can help update the documentation if you think more will run into this problem.