Architectural pattern - data transfer object.
Background –
There are many angles you can look into performance issue.
Many times we get complains that, your app is slow. One of the major area to look into performance improvement is inter process communication, sending
data between two processes in client-server architecture is a costly operation, because many calls are synchronous (where client wait server to
give processed data back) and even if it is asynchronous, time taken to transfer
data matters.
One way to solve this problem is to reduce the number of calls
between two processes, and pass more data whenever we are calling inter process
methods, but for that we shall have well-defined interface with long list of
method parameters. This approach is not very good because first, we have to very
careful with the sequence of parameter while calling it (If we have 7 ints in function parameter list, as all take same type of parameter if you miss the sequence, it could lead to wrong result) second, doesn’t matter
how much information you pass to any function it will return only one result from server
side (We can return only single value from function).
A good solution is to create a data transfer object (DTO) that
will hold all necessary information which are required to call remote method. Make function
signature to accept data transfer object and function shall return processed data transfer
object back to client. Then client will query all required data from DTO
object locally.
For example, let’s think about a client-server architecture
for a simple banking application. Without DTO object, it has to query all
information from server.
But after we have customer DTO, we will call server only
once and query all required data from DTO locally.
class BankCustomerDTO { public: private: string m_accountNumber; string m_name; string m_pan; double m_balance; string m_debitCardNumber; string m_branchAddress; }
DTO will not have any business logic in it, except store and
retrieve data. It has to be a plain data class.
About Data transfer object architectural pattern –
At some places we call it value object (VO). At first place, you may think that DTO has reduced the performance, as it is one indirection between client and server, and now call has to go
throw DTO to server, before it was direct call to server. But the amount of remote call
reduction matters, it will give good performance eventually as you will reduce
inter process method calls. Remember remote calls are very costly.
Thanks for reading it. To read more on design patterns and basic design principles please see my web page. You can also join me on FB or on G++. Please drop comments for any question related to this blog.
Comments
Post a Comment