Dependency Injection C# Visual Studio For Mac

  1. Visual C++ For Mac
  2. Visual Studio On A Mac
  3. Visual Studio For Mac Free
  4. C# Dependency Injection Frameworks
  5. X Code
  6. C# Dependency Injection Tutorial

Dependency injection container can set the Logger if it is available (registered to DI container before). Good Practices: Use property injection only for optional dependencies. Thanks for your help. I am looking for another help from you. I am looking for sample code for various way of implementing Dependency injection using unity. We know that Dependency injection can be implemented with three way or may be more and those are. Using dependency injection is quite straightforward in C++. Just define an interface (a pure abstract base class) that you use as reference or pointer (or smart pointer) argument to the constructor or init function of the class you want to dependency inject into.

Active3 years, 7 months ago

I'm working on a visual studio add-in that takes SQL queries in your project, plays the request and generates a C# wrapper class for the results. I want to do a simplest possible dependency injection, where projects using my add-in supply a class that can provide the project's db connection string, among other things.

This interface is defined in my add-in...

And the question : How do I define and instantiate the concrete implementation, then use it from the add-in?

Progress?

The interface above is defined in the add-in. I've created a reference in the target project to the add-in, written the concrete implementation, and put the name of this class in the target project web.config. Now I need to load the target project from the add-in to use my concrete class.

If I use Assembly.Load()...

I can successfully load my class, but I lock the target assembly and can no longer compile the target project.

If I create a temporary app domain...

I get a file not found exception on the call ad.Load(), even though the bytes of my dll are in memory.

If I use CreateInstanceFromAndUnwrap()...

I get an

InvalidCastException. 'Unable to cast transparent proxy to type QueryFirst.IQueryFirst_TargetProject'

This makes me think I'm very close? Why would an explicit cast work fine with Assembly.Load(), but fail when the same assembly is loaded in a newly created AppDomain?

Mac
Community

Visual C++ For Mac

bbsimonbbbbsimonbbVisual
12.6k6 gold badges40 silver badges56 bronze badges

1 Answer

I'm assuming that your add-in is going to be triggered in someway in order to start working with SQL Queries.

I'd recommend that you bundle a separate .exe file with your add-in and do the processing in there.

Here's why:

  1. Personally, I've had a lot of issues with AppDomains similar to what you're running into with file locking and the head ache of Temp Domains. The other issue you'd likely run into is once you load an Assembly into an AppDomain, you can't unload. By using a separate process (that dies when it's finished) you don't have to worry about the problem.
  2. Depending on the type of Projects you want to support, those Projects will have dependencies. Managing references to dependent dlls will be a lot easier if you can just point your standalone .exe at a directory (ie the bin directory).
  3. If you hook into Visual Studio's Build Events (DTE.Events.BuildEvents.OnBuildBegin) you can kill your process and release the locks on the dll files. Or you could have your process first make copies.
  4. Testing/debugging is a lot easier with a stand alone file. You don't need to worry about trying to debug by attaching to Visual Studio (How to debug a Vsix project).

You can use the following methods to start/kill processes:

I think you can reference the output of a Console Project directly from your VSIX Add In Project via the References Anatomy of a VSIX Package. Otherewise, you might need to do some custom MSBuild to get the .exe included within the VSIX file.

Visual Studio On A Mac

Once it's included, you can find the .exe because it should be in the same path as your executing VSIX (Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) and I would pass it the Path to the loaded Project's bin directory.

Visual Studio For Mac Free

As an aside, this isn't Dependency Injection. If you want to use DI inside a VS Extension, you can use whatever framework you'd like, but I think MEF is natively supported. Personally, I prefer Ninject. Define your Kernel inside the Package class and the use it to load your top level class.

Community
Philip Pittle

C# Dependency Injection Frameworks

Philip Pittle
7,0746 gold badges38 silver badges91 bronze badges

X Code

Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

C# Dependency Injection Tutorial

Not the answer you're looking for? Browse other questions tagged c#visual-studiovisual-studio-addins or ask your own question.