I have mapper method which maps different types of property like bool,enum,string,int
But now I want to map my one of the custom class which is being inherited in several places
What Required: Currently, it is not mapping values of Payment Options, and also I do not know how will I access param.paymentoptions which will have delivery and payLater information
Back Story:
Earlier we were creating instance and assigning values in POM only, now we want to use Excel to pass value and map and then use in our methods
Object classes Which I want to map
- namespace Framework.Model.PaymentOptions
- {
- public class PaymentOptions
- {
- public PaymentPortal portal;
- public DeliveryMethod delivery = DeliveryMethod.Billing;
-
- public PaymentOptions()
- {
- }
- public PaymentOptions(Site site)
- {
-
- }
- }
- }
Which is inherited in below class
- namespace Framework.Model.PaymentOptions
- {
- public class KlarnaOptions : PaymentOptions
- {
-
- public bool byCard = false;
- public bool payLater = false;
- public bool sliceIt = false;
- public KlarnaOptions()
- {
- portal = PaymentPortal.Klarna;
- }
- }
- }
Earlier we used to access and assign the values as below
Assigning Values
- public class WorkflowParameters
- {
-
- public Site _site = default(Site);
- public PaymentOptions paymentOptions = default(PaymentOptions);
- }
-
- var param = new WorkflowParameters()
- {
-
- paymentOptions = new KlarnaOptions()
- {
- delivery = DeliveryMethod.Billing,
- payLater = true
- }
- };
Use in Methods would be as below
- Checkout(param.paymentoptions);
- Now we want to pass values from Excel Sheet
- Test Data Model
- namespace Framework.Model.Excel
- {
- public partial class TestDataModel
- {
-
- public TestDataModel() {
-
-
- }
-
-
- [DataNames("TestName")]
- public string TestName { get; set; }
-
-
-
-
- [DataNames("paymentOptions")]
- public PaymentOptions paymentOptions { get; set; }
-
-
- [DataNames("SiteGroupId")]
- public SiteGroup SiteGroupId { get; set; }
-
- [DataNames("NickName")]
- public string NickName { get; set; }
-
- [DataNames("byCard")]
- public bool byCard { get; set; }
-
- [DataNames("payLater")]
- public bool payLater { get; set; }
-
- [DataNames("sliceIt")]
- public bool sliceIt { get; set; }
-
- [DataNames("portal")]
- public PaymentPortal portal { get; set; }
-
- [DataNames("delivery")]
- public DeliveryMethod delivery{get;set;}
- }
-
- }
-
- public static class PropertyMapHelper
- {
-
- public static void Map(Type type, DataRow row, PropertyInfo prop, object entity)
- {
- List<string> columnNames = AttributeHelper.GetDataNames(type, prop.Name);
-
- foreach (var columnName in columnNames)
- {
- if (!String.IsNullOrWhiteSpace(columnName) && row.Table.Columns.Contains(columnName))
- {
- var propertyValue = row[columnName];
- if (propertyValue != DBNull.Value)
- {
- ParsePrimitive(prop, entity, row[columnName]);
- break;
- }
- }
- }
- }
-
- private static void ParsePrimitive(PropertyInfo prop, object entity, object value)
- {
- if (prop.PropertyType == typeof(string))
- {
- prop.SetValue(entity, value.ToString().Trim(), null);
- }
- else if (prop.PropertyType == typeof(bool) || prop.PropertyType == typeof(bool?))
- {
- if (value == null)
- {
- prop.SetValue(entity, null, null);
- }
- else
- {
- prop.SetValue(entity, ParseBoolean(value.ToString()), null);
- }
- }
- else if (prop.PropertyType == typeof(long))
- {
- prop.SetValue(entity, long.Parse(value.ToString()), null);
- }
- else if (prop.PropertyType == typeof(int) || prop.PropertyType == typeof(int?))
- {
- if (value == null)
- {
- prop.SetValue(entity, null, null);
- }
- else
- {
- prop.SetValue(entity, int.Parse(value.ToString()), null);
- }
- }
- else if (prop.PropertyType == typeof(decimal))
- {
- prop.SetValue(entity, decimal.Parse(value.ToString()), null);
- }
- else if (prop.PropertyType == typeof(double) || prop.PropertyType == typeof(double?))
- {
- double number;
- bool isValid = double.TryParse(value.ToString(), out number);
- if (isValid)
- {
- prop.SetValue(entity, double.Parse(value.ToString()), null);
- }
- }
- else if (prop.PropertyType == typeof(DateTime) || prop.PropertyType == typeof(Nullable))
- {
- DateTime date;
- bool isValid = DateTime.TryParse(value.ToString(), out date);
- if (isValid)
- {
- prop.SetValue(entity, date, null);
- }
- else
- {
- isValid = DateTime.TryParseExact(value.ToString(), "MMddyyyy", new CultureInfo("en-US"), DateTimeStyles.AssumeLocal, out date);
- if (isValid)
- {
- prop.SetValue(entity, date, null);
- }
- }
- }
- else if (prop.PropertyType.IsEnum)
- {
- var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
- var enumValue = Enum.Parse(type, value.ToString(), true);
- prop.SetValue(entity, enumValue, null);
- }
- else if (prop.PropertyType == typeof(Guid))
- {
- Guid guid;
- bool isValid = Guid.TryParse(value.ToString(), out guid);
- if (isValid)
- {
- prop.SetValue(entity, guid, null);
- }
- else
- {
- isValid = Guid.TryParseExact(value.ToString(), "B", out guid);
- if (isValid)
- {
- prop.SetValue(entity, guid, null);
- }
- }
- }
- else if(prop.PropertyType == typeof(PaymentOptions))
- {
-
- }
- }
-
- public static bool ParseBoolean(object value)
- {
- if (value == null || value == DBNull.Value) return false;
-
- switch (value.ToString().ToLowerInvariant())
- {
- case "1":
- case "y":
- case "yes":
- case "true":
- return true;
-
- case "0":
- case "n":
- case "no":
- case "false":
- default:
- return false;
- }
- }
- }