Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency Injection into Community Toolkit Visual Studio Extension ToolWindow is not working #531

Open
AvtsVivek opened this issue Nov 25, 2024 · 0 comments

Comments

@AvtsVivek
Copy link

AvtsVivek commented Nov 25, 2024

If I create a regular VSIX project(not a community one)

Basic Vs Extension Package

add a tool window,

Adding Simple Tool Window

and then add Autofac Dependency Injection infrastructure. This works fine as expected.

I basically picked up the idea from this article by Akos Nagy.

My full working example is here.

Note the tool window is derived from ToolWindowPane and it has a dependency on the UserControl as follows.

public class TrialToolWindow : ToolWindowPane
{
    /// <summary>
    /// Initializes a new instance of the <see cref="TrialToolWindow"/> class.
    /// </summary>
    public TrialToolWindow(TrialToolWindowControl trialToolWindowControl) : base(null)
    {
        this.Caption = "TrialToolWindow";

        // This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
        // we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on
        // the object returned by the Content property.
        this.Content = trialToolWindowControl;
    }
}

But then, I wanted to explore the same with community tool kit extension packages.

So I created a Community tool kit project with Toolwindow as follows. This is different from the above.

Community tool kit with tool window project

Note here the tool window derives from Community.VisualStudio.Toolkit.BaseToolWindow and looks as follows.

public class MyToolWindow : BaseToolWindow<MyToolWindow>
{

}

In this case again, the tool window has a dependency on User Control, and if it is hard coded(no DI) then it works fine.

Now I want to add Autofac infrastructure, and here is where the problem begins. The tool window does not show up. My project for this case is here

As tried to dig deeper into the BaseToolWindow class, I found that the problem is here when an object is newed up as follows.

_package = package;
_implementation = new T() { Package = package };

We know newing up stuff like that is a cardinal sin in DI, so I think is the problem.

But I am not sure the community development team did a mistake?

Or am I missing something here?

Is there some other/better way to do DI, that I am missing here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant