1

I am creating a simple P2P file sharing system, where one peer can send some Objects (e.g., String, custom objects, etc...) to another peer.

The peer, that receives the Object, can then manipulate it, extract information from it and store it.

To receive and send objects, the classes Sender and Receiver are used, and these two compose my Network Layer. These two classes implement Sockets and TCP communication.

I tried to decouple the Network Layer from the Business Logic, but I am facing a design problem: whenever a serialized object is received by the Receiver and is sent to the Business Logic, where should be (and by which actor) mapped into a known Class?

Should it be the responsibility of the Network Layer to understand the class of this incoming serialized object, our should it be the Business Logic that (maybe using an "instanceof") converts the incoming objects to a well-known object?

Is there any pattern or best practice to apply in this situation?

4
  • You might be interested in studying the OSI model. The conversion to/from a message specification is done by the presentation layer, which is between the application and network layers. Commented Oct 19, 2022 at 23:33
  • Thanks for the answer, but I still have a question about the OSI Model. When a message goes from the top (may it be application layer) to the bottom (may it be physical layer), then it's easy to see it as an N-tier architecture, where upper layer knows how to use bottom layer and not vice versa. But when the message arrives at the other side, and this time has to go from bottom to top, how is that possible that the bottom layer does not know the upper one? If it does not know it, how can it send back the message from bottom to top? Commented Oct 20, 2022 at 6:58
  • In general the bottom will hold onto its stuff until someone above asks for it. Or it could allow the top to register a callback function, i.e. event. Commented Oct 20, 2022 at 7:24
  • "Should it be the responsibility of the Network Layer to understand the class of this incoming serialized object" - if you define the Network Layer as a layer in your application that provides networking services to the Business Layer, then the answer is yes. The data format/type should be defined in the Business Layer, though. E.g., you might have some interface there (in the BL) that the network layer implements (dependency inversion). Now, within the network layer, you can have generic networking code - that code shouldn't know anything about the data format. Commented Oct 21, 2022 at 23:51

3 Answers 3

1

The more your network layer knows about classes that have nothing to do with networking the less focused is it on networking. This also keeps it from being reusable.

But something has to know about them. Why does that something need to know about networking?

Protect things from knowing too much and it’ll be easier to change things because few things know about what you’re changing.

It’ll also be easier to find things because you made a place for them rather then just dumping them wherever.

1

In .NET you can use the .NET serializer which converts your string or byte array back into an object of which your network layer does not need to know the type. This object can then be passed up the stack to a business class that does know the type and can cast it. This should be done using an event in your receiver class.

If you do not have this you can leave deserialization to an intermediate layer. Your network layer would just transport strings or byte arrays and your intermediate layer (a helper class for your business logic that knows the type) can deserialize. Same on the other end with serialization.

1

There are different possible answers here based on your project structure or architecture. However, in general you will find that precisely one of these layers is aware of both sides of the mapping. If they're both aware of both sides, then you need to revisit your layer separation and general project structure.

Logically, the mapper can only reside in a location which is aware of both the source and target of the mapping.

For example, in a basic N-tier architecture where the network layer has a project reference to the business layer (like an API project would, which I'm assuming is analogous here as you receive external requests); then the Business would have defined the DTO that it wants to receive, and the onus would be on the network layer to map the serialized data to the Business DTO. It couldn't be the Business doing it, because then the Business would need to be aware of the structure of the serialized data, which negates the layer separation you put in place between the business and network layers.

But like I said, different project structures lead to different locations for these implementation details.

3
  • Thanks Flater, this means that in your example, even if it is an N-tier architecture there can be a Network Layer aware of some structures of the upper layer (being these DTOs) and, in the same time, the Business Layer aware on how to use the Network Layer. Right? In other words, if a layer is aware of both sides of the mapping, is that necessary a problem? Commented Oct 20, 2022 at 6:55
  • @SimoneBrigante There will logically always have to be one layer that knows both sides of the mapping, because else it wouldn't be able to contain the mapping logic (which requires references to both fields). The example of a network layer is a bit ambiguous though, as this can be interpreted as being an infrastructure layer or an API layer (as you "receive requests from the network); which makes it hard to give you a singular concrete answer. The right location for the mapping depends on your project structure and how these two layers relate to one another. Commented Oct 20, 2022 at 22:18
  • @SimoneBrigante: Think of it like this: You have a Business Layer (BL), and a Network Layer (NL) - an overall layer that provides networking services for the business layer. The BL doesn't know about the NL, but instead provides extension points. The NL uses those to "plug into" the BL. So, within the NL, you'd have "surface level" components that would reference the BL types and/or implement some interfaces from the BL. These would do the to-and-from mapping. And then you'd also have the more low level generic networking components that are unaware of your business layer types or the mappers. Commented Oct 22, 2022 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.