Comments on: C# Design Patterns – Adapter https://code-maze.com/adapter/ Learn. Code. Succeed. Tue, 06 Aug 2024 10:28:06 +0000 hourly 1 https://wordpress.org/?v=6.7.5 By: Tadeusz https://code-maze.com/adapter/#comment-5510 Thu, 21 Apr 2022 13:40:02 +0000 https://code-maze.com/?p=47505#comment-5510 In reply to Marinko Spasojević.

I thought you tried to avoid the complexity in the article and the usage of IXmlToJson is implied, but also thought that the consumer is the important part of the adapter pattern. Otherwise, the code in ConvertXmlToJson can be just inlined in Main() and it won’t make a difference. I find your site very helpful by the way with lot of useful content. Thank you for sharing all that

]]>
By: Marinko Spasojević https://code-maze.com/adapter/#comment-5509 Thu, 21 Apr 2022 13:03:15 +0000 https://code-maze.com/?p=47505#comment-5509 In reply to Tadeusz.

I understand your point but just because we used the result from an adapter class to print the result it doesn’t mean that it can’t be used in a class that needs XML data converted to JSON data. We just printed the results for the example’s sake and to avoid additional complexity. If we want to be perfectly accurate with the architecture neither GetXML nor ConvertToJSON should print anything, they should return values inside the adapter class so it could get them back to the consumer, but that would add just an additional complexity to this article, which I tried to avoid.

]]>
By: Tadeusz https://code-maze.com/adapter/#comment-5508 Thu, 21 Apr 2022 12:54:23 +0000 https://code-maze.com/?p=47505#comment-5508 In reply to Marinko Spasojević.

My point is that the adapter should rather be XmlToListAdapter as JsonConverter seems to be the consumer. Adapters implement interfaces that are required by consumers. Adapter in your example implements IXmlToJson, but that interface is not consumed anywhere.

]]>
By: Marinko Spasojević https://code-maze.com/adapter/#comment-5507 Thu, 21 Apr 2022 12:40:09 +0000 https://code-maze.com/?p=47505#comment-5507 In reply to Tadeusz.

We can add an additional IJsonConverter interface and provide a bit different way to supply the manufacturers list to the JsonConverter class and then pass it via DI and not instantiate it inside the adapter class, but this is just a different variation of the same behavior inside the adapter class. I agree it would be better for sure, but I still think that this article shows exactly how the adapter class should behave and what is its purpose. Now, the way you want to implement it is completely up to you.

]]>
By: Tadeusz https://code-maze.com/adapter/#comment-5506 Thu, 21 Apr 2022 11:49:00 +0000 https://code-maze.com/?p=47505#comment-5506 Thank you for all the examples, but I don’t believe this is the good example of the adapter pattern. Adapter itself should know nothing about JsonConverter. It should convert from XML to IEnumerable<Manufacturer> so that JsonConverter can then consume it. Also, it is a common practice that the adapter implements the target interface (IEnumerable<Manufacturer>) in our case.

]]>
By: Marinko https://code-maze.com/adapter/#comment-1462 Tue, 10 Sep 2019 02:28:57 +0000 https://code-maze.com/?p=47505#comment-1462 In reply to Ajay.

Hello Ajay. We have a class that provides us with the XML format data and the class which transforms a List to the JSON format data. Between those two we have to create a bridge (adapter) class to transform XML to a List and than we can send that list to the JSON convert class. The ConvertToJson is our custom method inside the XMLToJsonAdapter class.

]]>
By: Ajay https://code-maze.com/adapter/#comment-1461 Mon, 09 Sep 2019 21:06:39 +0000 https://code-maze.com/?p=47505#comment-1461 Hi,
Thanks for this article . I have one dought that why we are using adapter here. On the top we are doing XML to json format in class . We can use that. Please provide answer to me .
Thanks ,
Ajay

]]>
By: Marinko https://code-maze.com/adapter/#comment-1312 Wed, 24 Jul 2019 21:43:35 +0000 https://code-maze.com/?p=47505#comment-1312 In reply to Edwar Andres Pineda Rincon.

Hello Edwar, thank you for that suggestion. My mistake. All the best.

]]>
By: Edwar Andres Pineda Rincon https://code-maze.com/adapter/#comment-1311 Wed, 24 Jul 2019 17:09:40 +0000 https://code-maze.com/?p=47505#comment-1311 Hi. Adapter pattern is structural pattern not creational. Thanks for the examples.

]]>
By: Marinko https://code-maze.com/adapter/#comment-774 Tue, 05 Mar 2019 09:14:16 +0000 https://code-maze.com/?p=47505#comment-774 In reply to ede.

Hello ede. Thank you for reading this article and for commenting as well. To answer your question, think of the XmlConverter as a provided API and not something that we have coded in our project. Having that in mind, the XmlConverter class could be sealed as much as we know, and then you will have no possibility to inherit from it. The second thing is that there is a multiple reasons why composition is better then inheritance (you can read about that in great detail in variety of articles on the internet) thus many different design patterns use composition over inheritance as well. Finally, we are not saying that this is ONLY and THE BEST way, no way 😀 You can implement Adapter pattern in different ways but this is just one of them.

]]>