Link to the same interface in different assemblies

I want to implement an architecture using various .NET assemblies (i.e. modules). Some of these modules must provide services that are used as .NET interfaces by other modules. Modules must be loaded and registered dynamically at runtime; I do not want to have “hard-coded” dependencies between them.

Example:

  Module1.dll:
    Defines a class implementing interface IService1
  Module2.dll:
    Uses the class provided by Module1 through the interface IService1 

The problem is where to put the definition IService1: Both modules need this definition. But since it Module2should also work in the absence Module1(the availability of the service is checked at runtime), I do not want to Module2.dllrefer directly to Module1.dll.

One possibility is to split each module into two assemblies (interface and definition), but that will mean that I double the number of DLLs that I don't want.

I also thought about using a separate "Interface Dll", that is, one single assembly containing all the interface definitions, but then again, if I change one interface or add new modules (with new iterfaces), I need to update this central DLL and therefore , all other modules (since they all depend on it ...)

Module1, Module2, , . .

Edit , : , Module1a.dll, Module1b.dll .. IService Module2a.dll, Module2b.dll .. ...

+3
5

dll, OO.

, IService1 Module1? IServer1 , DLL, .

DLL, , 1 .

, Module1.dll, dll ().

, , , , (, api ..). .

+1

, . , . , , , .

, , , Castle DynamicProxy, Reflection, IoC, Ninject, .


, "" Module1.dll, Module2.dll

public interface IFooProvider {
    void Foo GetFoo();
}

, , . DI IoC, .

:

public interface IServiceLocator {

    object LocateProvider<ContractType>();

    void RegisterProvider<ContractType>(object implementation);
}

, Module1, , ServiceLocator , , Module2 LocateProvider, , , Module1.

- :

public class Module1Implementation : IProviderContract {
    void Foo GetFoo() { return new Foo(); }
}

public class Main {
    public void Main() {

        var locator = ServiceLocator.GetLocator();
        locator.RegisterProvider<IFooProvider>(new Module1Implementation());

    }
}

Module2.dll:

public class Consumer {

    public IFooProvider FooProvider { get; set; }

    public Consumer() {
        var locator = ServiceLocator.GetLocator();
        FooProvider = locator.LocateProvider<IFooProvider>();

        // if Module1.dll is loaded, the locator should supply 
        // Module1Implementation for you
    }
}

"" DLL , 1 DLL, . ServiceLocator "", .

, , , , DLL, , Castle DynamicProxy Reflection. , , Google.: -)


? DLL, . , :

  • , , , .
  • , , , ""
  • , "" , .

! , , .

+1

Module2.dll IService Module1.dll Module2.dll. Module2.dll (, ), , Module1.dll, IService:

// in module2.dll
var serviceInstance = (IService)Activator.CreateInstance(typeToLoad);
0

Module2.dll?

IService1 - ( ), Module2.dll, , Module2.dll.

: , , . , () , , (.. ).

0

, "" , - , .

Module1 CoreInterfaces.dll( ) Module2 CoreInterfaces.dll

0

Source: https://habr.com/ru/post/1712622/


All Articles