Click here to Skip to main content
14,635,060 members
Home » Discussions » C#
   

C#

 
GeneralRe: Only one usage of each socket address - error Pin
Gerry Schmitz12-Jul-20 6:33
mveGerry Schmitz12-Jul-20 6:33 
Questionindex was out of bound of the array Pin
niceruyu10-Jul-20 1:00
Memberniceruyu10-Jul-20 1:00 
AnswerRe: index was out of bound of the array Pin
OriginalGriff10-Jul-20 1:35
mveOriginalGriff10-Jul-20 1:35 
AnswerRe: index was out of bound of the array Pin
harold aptroot10-Jul-20 1:39
Memberharold aptroot10-Jul-20 1:39 
AnswerRe: index was out of bound of the array Pin
Richard MacCutchan10-Jul-20 3:16
mveRichard MacCutchan10-Jul-20 3:16 
AnswerRe: index was out of bound of the array Pin
Dave Kreskowiak10-Jul-20 3:22
mveDave Kreskowiak10-Jul-20 3:22 
AnswerRe: index was out of bound of the array Pin
Swapnita Dessai19-Jul-20 0:47
MemberSwapnita Dessai19-Jul-20 0:47 
QuestionHow to map excel column value to custom defined object class Pin
Member 148591514-Jul-20 6:56
MemberMember 148591514-Jul-20 6:56 
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

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
    {
        //default - don't use card payment by deffault
        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); // Which would pass delivery and paylater values
Now we want to pass values from Excel Sheet
Mapper Method

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>))
            {
                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;
            }
        }
    }

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; }
        //public PaymentOptions paymentOptions = default(PaymentOptions);

        [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;}
    }
    
}
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

AnswerRe: How to map excel column value to custom defined object class Pin
Mycroft Holmes4-Jul-20 13:53
professionalMycroft Holmes4-Jul-20 13:53 
GeneralRe: How to map excel column value to custom defined object class Pin
Member 148591515-Jul-20 3:41
MemberMember 148591515-Jul-20 3:41 
GeneralRe: How to map excel column value to custom defined object class Pin
Mycroft Holmes5-Jul-20 12:33
professionalMycroft Holmes5-Jul-20 12:33 
GeneralRe: How to map excel column value to custom defined object class Pin
Member 148591516-Jul-20 1:46
MemberMember 148591516-Jul-20 1:46 
AnswerRe: How to map excel column value to custom defined object class Pin
Mycroft Holmes6-Jul-20 12:49
professionalMycroft Holmes6-Jul-20 12:49 
QuestionHow to get the actual, GENERATED source code from my site? Pin
Member 139493623-Jul-20 10:05
MemberMember 139493623-Jul-20 10:05 
AnswerRe: How to get the actual, GENERATED source code from my site? Pin
Mycroft Holmes3-Jul-20 12:30
professionalMycroft Holmes3-Jul-20 12:30 
AnswerRe: How to get the actual, GENERATED source code from my site? Pin
F-ES Sitecore5-Jul-20 5:31
mveF-ES Sitecore5-Jul-20 5:31 
QuestionTask.Run.Wait Pin
Bernhard Hiller3-Jul-20 0:16
MemberBernhard Hiller3-Jul-20 0:16 
AnswerRe: Task.Run.Wait Pin
OriginalGriff3-Jul-20 0:49
mveOriginalGriff3-Jul-20 0:49 
GeneralRe: Task.Run.Wait Pin
Bernhard Hiller3-Jul-20 2:18
MemberBernhard Hiller3-Jul-20 2:18 
GeneralRe: Task.Run.Wait Pin
OriginalGriff3-Jul-20 4:00
mveOriginalGriff3-Jul-20 4:00 
AnswerRe: Task.Run.Wait Pin
jsc423-Jul-20 6:31
professionaljsc423-Jul-20 6:31 
QuestionStrange characters in JSON RPC server response Pin
pkfox1-Jul-20 3:37
professionalpkfox1-Jul-20 3:37 
AnswerRe: Strange characters in JSON RPC server response Pin
Luc Pattyn1-Jul-20 5:33
sitebuilderLuc Pattyn1-Jul-20 5:33 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox1-Jul-20 6:23
professionalpkfox1-Jul-20 6:23 
GeneralRe: Strange characters in JSON RPC server response Pin
pkfox1-Jul-20 6:46
professionalpkfox1-Jul-20 6:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.