Design Pattern - Structural - Adapter

   

Scenario:

Create a converter adapter to transform data format between two different interfaces.

Solution:

This pattern allows interface of an existing class to be used by other interface.

Example: Another api returns xml data and our processor supports json or any other format, then create and adapter which would do the transformation from the incoming to what we expect.
  1. Converter

  2.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    using System.Xml.Linq;
    using System.Xml.Serialization;
    using Newtonsoft.Json;
    
    namespace TestProject
    {
        public interface IConverter<T>
        {
            T XmlToJson(string data);
        }
    
        public interface ICustomerApi
        {
            string GetCustomer(int customerId);
        }
    
        public class Converter<T> : IConverter<T> where T : class
        {
            public T XmlToJson(string data)
            {
                var doc = XDocument.Parse(data);
                doc.Descendants().Attributes().Where(x=> x.IsNamespaceDeclaration).Remove();
                dynamic json = JsonConvert.SerializeXNode(doc);
                return JsonConvert.DeserializeObject<T>(json);
            }
        }
    
        public class CustomerResult
        {
            public Customer? Customer { get; set; }
        }
    
        public class Customer
        {
            public int Id { get; set; }
            public string? Name { get; set; }
        }
        public class CustomerApi : ICustomerApi
        {
            public string GetCustomer(int customerId)
            {
                var customer = new Customer();
    
                if (customerId == 1)
                {
                    customer = new Customer
                    {
                        Id = customerId,
                        Name = "Ram"
                    };
                }
                else if (customerId == 2)
                {
                    customer = new Customer
                    {
                        Id = customerId,
                        Name = "Shyam"
                    };
                }
    
                //return JsonConvert.SerializeObject(customer);
    
                var serializer = new XmlSerializer(typeof(Customer));
    
                using var writer = new StringWriter();
    
                serializer.Serialize(writer, customer);
    
                return writer.ToString();
            }
        }
    }

  3. Get data from CustomerApi and transform it to Json through adapter.
  4.  
    1
    2
    3
    4
    5
    6
    7
    var customer = new CustomerApi();
    
    var data = JsonConvert.DeserializeObject<Customer>(customer.GetCustomer(1));
    
    Console.WriteLine($"Customer Id: {data.Id}, Name: {data.Name} ");
    
    Console.ReadLine();

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...