November Happy Hour will be moved to Thursday December 5th.

[Epi1007] Avoid using concrete type Mediachase.Commerce.Orders.OrderAddress

Vote:
 

I'm using madsstorm/CodeAnalyzers.Episerver and got the following warning:

AddressMappings.cs(45, 45): [Epi1007] Avoid using concrete type Mediachase.Commerce.Orders.OrderAddress

I'm using OrderAddress for adding contact address:

contact.AddContactAddress(customerAddress);

Questions:

1: Is the warning something I should be bothered to? Or, is the warning obsolete and I should just skip it.

2: If the warning is proper: Why the concrete type shouldn't used? How can I add address to CustomerContact then?

#284969
Aug 05, 2022 12:10
Vote:
 

You can use the CustomerAddress for adding adresses to a contact. OrderAdress is used in the context of orders and can be created from a CustomerAddress. So the warning probably comes from using the OrderAddres with the CustomerContact.

#284978
Aug 05, 2022 14:16
Vote:
 

Thank you for the answer, Jeroen.

I'm also using IOrderAddress in another context: shipment.ShippingAddress.

So, what's the proper way to set shipment.ShippingAddress property? ShippingAddress is of IOrderAddress type, so I assume we should map CustomerAddress to IOrderAddress - is that right?

#284990
Aug 05, 2022 15:45
Vote:
 

Proper way is to create an address by injecting IOrderGroupFactory and then calling CreateOrderAddress.

IOrderAddress orderAddress = orderGroupFactory.CreateOrderAddress(cart)

Map your customer address to the orderAddress object.

Then add the address to your shipment on your cart.

shipment.ShippingAddress = orderAddress
#285188
Edited, Aug 08, 2022 21:19
Vote:
 

The general advice is to use the new Order API (i.e. interfaces, for example IOrderAddress) instead of the concrete types, like OrderAddress. But that's not an absolute rule 

I'm not quite sure why you can use an OrderAddress for contact.AddContactAddress because there is no implicit conversion between those two. You can however write a simple method to convert an IOrderAddress to a CustomerAddress, like this (untested by me)

        public static void CopyOrderAddressToCustomerAddress(IOrderAddress orderAddress, AddressEntity customerAddress)
        {
            customerAddress.Name = orderAddress.Id;
            customerAddress.City = orderAddress.City;
            customerAddress.CountryCode = orderAddress.CountryCode;
            customerAddress.CountryName = orderAddress.CountryName;
            customerAddress.DaytimePhoneNumber = orderAddress.DaytimePhoneNumber;
            customerAddress.Email = orderAddress.Email;
            customerAddress.EveningPhoneNumber = orderAddress.EveningPhoneNumber;
            customerAddress.FirstName = orderAddress.FirstName;
            customerAddress.LastName = orderAddress.LastName;
            customerAddress.Line1 = orderAddress.Line1;
            customerAddress.Line2 = orderAddress.Line2;
            customerAddress.PostalCode = orderAddress.PostalCode;
            customerAddress.RegionName = orderAddress.RegionName;
            customerAddress.RegionCode = orderAddress.RegionCode;
            customerAddress.State = orderAddress.RegionName;
        }

#285228
Aug 09, 2022 6:31
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.