Search code examples
c#.net-4.0asp.net-web-apionion-architecture

Onion Architecture External Services


Im currently working on a project which is based on Onion Architecture . The above image Shows the Solution.

In the Infrastructure We have External Service . But the WebAPI has access only to Core .

But in the Web API project i want to access the some of the models exposed by the external services ? How can we achieve this without adding reference to Infrastructure in the Web API .

Or we implemented Onion Architecture wrong?


Solution

  • conceptually you are on the right track, but the implementation isn't a hard a fast rule. to start you don't need 5+ projects at most you need 3 (web ui css/js/views, logic/controllers, code, and tests). and really you probably only need 2 (the application, the tests)

    the idea of layers is conceptual, not physical. And there is not a hard and fast rule that says the layers must be completely segregated. rather the core focus of the application is what the application does. as you get into the details of how that is implemented you move to the outer layers.

    in this instance you need to access data retrieved from an external service. create an abstraction for the external service IExternalServiceAdaptor. The interface may reside in the domain or server layer, but the implementation might reside in an infrastructure or outer layer where the details of how to call the external service are encapsulated within an implementation of IExternalServiceAdaptor.

    If you stick with your physical separation you would have an interface in Core and the implementation in Infrstructure.