-
Notifications
You must be signed in to change notification settings - Fork 50
Using a different base component class #52
Comments
I would guess that this is not possible atm. But I see that this would make sense, especially regarding interoperability with other libraries. Gonna make a concept and implement it. |
Turns out, this is a non trivial problem that I don't really have a proper solution for. I would need the possibility to inherit from a generic which isn't supported in C#. Besides that, the only possible solution that seems feasible to me would be to copy the code over into |
No you can't inherit class My ideas is maybe we can use composition over inheritation. The binding functionalities could be provided by a individual object, say, an instance of the class @inject Binder<MyViewModel> Binder;
<div>@Binder.Bind(vm=>vm.Text)</div> |
This isn't trivial either. Moving out the binding related code into a separate class is not easy since you would need a reference back to the component from |
I think the syntax may get a little bit messy, but acceptable. Pseudo code: class Binder<T> where T : ViewModelBase
{
public Action StateHasChangedCallback { get; init; }
public Binder() {}
public Binder(Action stateHasChangedCallback) { this.StateHasChangedCallback = stateHasChangedCallback; }
public void OnInitialized() { /*...*/ }
public void OnParametersSet() { /*...*/ }
/* and other lifecycle methods */
}
abstract class MyComponentBase: SomeThirdPartyComponentBase
{
public Binder<MyViewModel> Binder { get; }
public MyComponent()
{
this.Binder = new Binder<MyViewModel>(this.StateHasChanged);
}
public override void OnInitialized()
{
base.OnInitialized();
this.Binder.OnInitialized();
}
public override void OnParametersSet()
{
base.OnParametersSet();
this.Binder.OnParametersSet();
}
/* and other lifecycle methods */
} It's the component's responsibility to set up the binder correctly, including supplying a For projects which doesn't need to work with other 3rdparty component base classes, they can still stick with the pre-built |
I'm thinking about using a source generator to prevent duplicating code this way (since it would be the same for
Yeah, totally missed that in the current implementation. My plan was to include it in the PR for this issue too. |
Talking about code generation, it might be worth trying to write a Fody weaver. It's cumbersome to write one, but aside from the built-in components, other developers can also benefit from using the weaver to create their own mvvm component base classes easily. |
Since we now got code generators in .NET I'm not a huge fan of Fody weavers any more. They served their purpose, but the built in source generators should do the job. The docs cover our use case. My plan would be to ship that source generator too (either in a separate NuGet package or directly with the main one). This way, any integration would happen by defining a partial class with a given attribute that would be picked up by the source generator. |
Great, looking forward to it |
Sorry for the super delayed response, had a lot of moving parts recently. Some months ago I made a working implementation in https://github.com/klemmchr/MvvmBlazor/tree/52-using-a-different-base-component-clas however it was not working with the demo projects. I made some research and I'm quite sure it has to do with the way how those analyzers are being referenced inside a project. However, I was not able to resolve that problem. Would be great if you could have a look ✌🏻 |
@klemmchr I cloned the branch but it does not seem to compile:
et cetera Is that the problem we are hunting? |
So the problem here is misusing of the In the With that fixed, some minor issues expose, like I've fixed those problems here, the solution now compiles, except for a test project which I did not bother to look: #54 |
Looks great, kinda curious how did you debug this? |
You place |
Ok, thats so simple that I'm kinda embarassed that I didn't thought of this 😄 |
MvvmComponentBase
inherits fromComponentBase
. What's the best practice if we want to use a different base component class, sayOwningComponentBase
orAbpComponentBase
as we are using the ABP framework, yet still want to keep the functionalities provided byMvvmComponentBase
?The text was updated successfully, but these errors were encountered: