Skip to content

Anti corruption feature

Jon P Smith edited this page Jan 9, 2019 · 2 revisions

The GenericBizRunner framework provides an anti-corruption layer feature (named after the DDD concept of the same name) which isolates the business logic from all higher layers, but especially the presentation layer. It does this by providing two abstract classes that can contains both business logic properties and presentation layer properties. It then implements a copy function that only copies over the business logic properties to the business logic.

NOTE: I call these classes DTOs (Date Transfer Objects). The other name they are known by in ASP.NET is ViewModel.

  • Defining a presentation-focused DTO
  • Setting up the presentation-focused properties
  • How the copy between presentation and business DTOs works

QUICK START - Example presentation-focused DTO

See WebChangeDeliveryDto - an example of a presentation-to-business DTO.

Defining a presentation-focused DTO

The GenericBizRunner library has two abstract classes that you have inherit to set up a presentation-focused DTO

  1. For a presentation-to-business DTO use [GenericActionToBizDto](https://github.com/JonPSmith/EfCore.GenericBizRunner/blob/master/GenericBizRunner/GenericActionToBizDto.cs).
  2. For a business-to-presentation DTO use [GenericActionFromBizDto](https://github.com/JonPSmith/EfCore.GenericBizRunner/blob/master/GenericBizRunner/GenericActionFromBizDto.cs).

NOTE: There are async versions of both abstract classes.

The idea is you define two types of properties

  1. Properties that MATCH the properties in the business-focused DTOs - these will be copies
  2. Properties that are ONLY meant for the presentation side. You will write code to set up these properties, and the GenericBizRunner will call your code at the right time.

Setting up the presentation-focused properties

The two abstract classes have a virtual method called SetupSecondaryData (ToBiz) or SetupSecondaryOutputData (FromBiz). The idea is you to override this method and add your code to set up any presentation properties. GenericBizRunner provides various ways to call this method. Both setup methods get access to the database DBContext that goes with the service. This means you can access the database to get data to display.

For ToBiz DTOs (inherited from GenericActionToBizDto)

When you create the IActionService<IMyBizLogic> then that service has two methods:

  1. service.GetDto<YourToBizPresenationDto>(): This creates an instance of the YourToBizPresenationDto class and then calls the SetupSecondaryData method. This is typically called when you want to show the page for the user to input their data.
    EXAMPLE - See ChangeDelivery GET Action in OrdersController.
  2. service.ResetDto(dto): This re-runs the SetupSecondaryData method. Typically this is done if there were errors and you want to set up the presentation properties again before showing the input page again, but with the errors. EXAMPLE - See ChangeDelivery POST Acton in OrdersController.

For FromBiz DTOs (inherited from GenericActionFromBizDto)

If the business logic finishes with no errors it will call the SetupSecondaryOutputData. This allows you to load extra data to show to the user (if required).

How the copy between presentation and business DTOs works

When you inherit the ToBiz or FromBiz abstract classes you need to tell the class what business DTO you are linking to. For instance the WebChangeDeliveryDto says its linked to the BizChangeDeliverDto, as seen below.

public class WebChangeDeliveryDto : 
    GenericActionToBizDto<BizChangeDeliverDto, WebChangeDeliveryDto>
{
    //... other code left out

This sets up an AutoMapper mapping between the two classes. The classes that match by name between the two classes will be copied. Properties that don't match won't be copied.

*NOTE: You can change the default mapping by overriding the virtual method called AutoMapperSetup. Here is a useful article on configuring the AutoMapper mappings.

Clone this wiki locally