N-Teir Architecture with ASP.NET Web Interface

I'm just trying to bow my head around this concept. I wrote several different web APIs, but they were always used by the site and interacted via JSON. My question is how to structure the implementation when the web API is consumed by the Windows service.

In this case, an existing database already exists, so I want to use the Entity Framework Database First approach.

I am creating a class library project for models and using the Entity Framework to view an existing database and create all the necessary classes.

Then I create a web API project and add my class library with all the models to it. Until this moment, I'm good.

My question is, when I am going to create a Windows service that will interact with the web API, how can I access classes from my class library model project? I know that I could add this project to my Windows service, but this does not seem to be the right approach, because it will bypass the web API pretty much.

I think my question is, do I want to create and pass an Employee object to my web API (so that it can insert it into the database) from my Windows Service, how does a Windows service get an Employee object without adding the Library class for a Windows service project?

+7
source share
2 answers

In an n-tier solution, you do not skip domain objects at physical boundaries, but you implement data transfer objects (DTO) that will contain only the required information by the consumer / caller.

Usually you will create a shared library that will have all the data transfer objects, and both the server and the client will reference this.

After that, it's all about using the JSON serializer to serialize and / or deserialize data transfer objects.

Domain objects are always mapped to data transfer objects because they are lighter than a complete object. Ask yourself: if the consumer only needs the name and middle name of someone, why do you need to send more data over the wire?

In addition, it is important to avoid server dependencies in client applications and services.

Some helpful tips:

+9
source

Usually you create exra model classes that are used for the web API. These model classes often contain only a subset of these objects. In addition, this difference allows you to create a truly RESTful API.
Within the web API controller classes, a mapping between Model and Entity is displayed.
The Windows service refers only to a project with Model classes, and not to Entity classes.

0
source

All Articles