2

I have an API project and a class library project that contains interfaces. So I have a third project that implements interfaces and a project that implements the same interfaces of project 3. Therefore, projects 3 and 4 know about project 2.

My problem is that I want to call the implementations through my class library project (the second), but this project does not know the implementation projects.

How can I call implementation projects through my second project?

enter image description here

The idea is that I have many implementation projects, so I just want to create another new project and implement the interface without having to change the code very much in project 2.

I'm using .NET Core 2.0 in all projects

Guilherme Ferreira
  • 1,503
  • 2
  • 18
  • 31
  • 1
    You are doing something wrong. The assembly that has the abstractions should never care about the implementations – maccettura Aug 24 '18 at 20:03
  • Not quite sure, but are you looking for dependency injection like Ninject? – Progman Aug 24 '18 at 20:09
  • @maccettura Does my second project need to know all the projects that implement the interface? – Guilherme Ferreira Aug 24 '18 at 20:11
  • @Alberto The assembly that contains the abstractions should be accessible by assemblies that implement those interfaces, but not the other way around. You couldn't do that if even if you wanted to because that is a "circular dependency". We would need much more detail and concrete examples to be able to suggest an alternative approach – maccettura Aug 24 '18 at 20:13
  • @maccettura see my image. I could create a project to only contain the interfaces. But I would love not to have to make my project 2 refer all the implementation projects. Is there a way to do this? Did you get my point? – Guilherme Ferreira Aug 24 '18 at 20:29
  • 1
    @Alberto you need to read up on dependency injection and IOC containers. Your Web API assembly should reference the abstraction assembly _and_ the implementation assemblies. The Web API project is where you will use an IOC container to map concrete implementations to abstracts – maccettura Aug 24 '18 at 20:39
  • @maccettura Ok, thanks. – Guilherme Ferreira Aug 24 '18 at 20:41

2 Answers2

2

This is a classic 'plugin' system.

Define the interface in project 2 (the 'hosting' project)

3 and 4 reference 2 in order to get the interface definition (also classically to get services offered by the hosting project)

Now you need to make the host (2) dynamically load the plugins.

You can do this 'by hand'. = Enumerate the DLLs is a specific directory and find the ones that implement the interfce.

Or you can use various frameorks to do it. MEF is a well know one. Look here for more info Implementing Plugin Architecture - Dynamic DLL loading

pm100
  • 48,078
  • 23
  • 82
  • 145
  • I don’t think this is the right approach at all. OP just needs to strengthen their dependency injection chops and pick an IOC container. – maccettura Aug 24 '18 at 21:20
  • 1
    If OP needs 3 and 4 active simultaneosuly then IOC is not typically the correct approach. – pm100 Aug 26 '18 at 23:25
0

It is difficult to know exactly what you should do without knowing what you are trying to accomplish, but...

Create a project, call it 2.a that contains just the interfaces. Assuming that Project 2 uses the implementations in both 3 and 4, it takes out a reference on 2.a, 3 and 4.

GlennSills
  • 3,977
  • 26
  • 28