torhovland / blazor-redux

Connecting a Redux state store with Blazor.
Other
481 stars 48 forks source link

Question: Best alternative to inheriting base class #14

Open Sire opened 5 years ago

Sire commented 5 years ago

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?

Sire commented 5 years ago

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.