I am building asp.net core 2 modular application, I was having one module but now I am about to create another module and I get to the point that I need to develop a communication between the two modules, I need to use a model class from module A in module B, how can I achieve this without any dependencies between the modules I do not want to put the model class in a shared project nor creating references between modules.
Asked
Active
Viewed 2,816 times
2
-
So, both your modules have different namespace, assembly name? or derived from common root namespace? – Sonal Borkar Dec 03 '18 at 11:11
-
Each module has it's own assembly,namespace,models and services but all modules have reference to a core project and an abstraction project both have general repositories and dtos – malballah Dec 03 '18 at 11:39
-
Can you move your model class to core project having dtos? Ideally, model classes acts as dtos – Sonal Borkar Dec 03 '18 at 11:42
-
That would solve the issue but is it a good practice, what if I deployed the application for some users without the module having this model? – malballah Dec 03 '18 at 11:44
-
If you move the model to core project then your module no longer contains model class definition. So, you will not require to include Module A or Module B for all deployment. Although, Core Project will always be needed in any case. – Sonal Borkar Dec 03 '18 at 11:48
1 Answers
1
Modules should not depend on another module, it can depend only on contracts. Some example. You have module A
with intrerface IModuleAService
and class that implements it, ModuleAService
. Module B
requires for his service ModuleBSerice
implementation of IModuleAService
. So create separate assemblies:
ModuleA.Abstractions
: containtsIModuleAService
and other contractsModuleA
: depends onModuleA.Abstraction
, contains classModuleAService
that implementsIModuleAService
ModuleB
: depends onModuleA.Abstraction
.
And at your startup class you need register all modules. IoC will make all workds for you. If later you would like to seperate app into two, it will be easy. Case your need to
implement proxy to access ModuleA
.

Markeli
- 558
- 4
- 16
-
Thank you, in your suggested architecture how can I get a list of a particular model for example Order which is in the models of the module A only? – malballah Dec 03 '18 at 11:42
-
Is it a good practice to have a shared project which contains all models required by any module? – malballah Dec 03 '18 at 11:56
-
Yes, it is. You separate abstraction and realisation. It's folows SOLID principles (class should depend on asbtraction, not realisation). In ModuleA.Abstraction declare service IOrdersService with method ICollection
GetOrders(), in ModuleA implement this interface with desired techonlogies (EF, NHibernate, etc). – Markeli Dec 03 '18 at 12:45 -
There is simillar question with some recommendations about modular architecture: https://stackoverflow.com/questions/20095572/modular-application?rq=1 – Markeli Dec 03 '18 at 12:46
-
Hello again, suppose that I have a model in moduleA and that model require the another model from moduleB, you know in entity framework sometimes we need to put the an object from the foreign key model in another model, so the questions is where should I put the required model if I put it in ModuleB then I cannot see it from ModuleA if I put it in ModuleB.Abstraction then I think this will break the Modularity, correct? – malballah Sep 02 '19 at 08:36