Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I have two classes PaymentMethod and EditPaymentMethod with following properties:
public class PaymentMethod
    {
        public int TenantID { get; set; }
        public string Market { get; set; }
        public virtual List<string> CreditCard { get; set; }
        public virtual List<string> DebitCard { get; set; }
        public virtual List<string> BPay { get; set; }
    }

    public class EditPaymentMethod 
    {
        public int TenantID { get; set; }
        public string Market { get; set; }
        public List<Multicheckbox> CreditCard { get; set; }
        public List<Multicheckbox> DebitCard { get; set; }
        public List<Multicheckbox> BPay { get; set; }
    }

    public class Multicheckbox
    {
        public string CheckboxName { get; set; }
        public bool IsChecked { get; set; }
    }

As you can see both the classes differ only in CreditCard, DebitCard and BPay

I want to avoid this code duplication, how can I achieve this using shadowing properties or making it generics?

Any suggestion on the same?

Thanks in advance
Posted
Updated 6-Nov-14 17:25pm
v2
Comments
Maciej Los 7-Nov-14 3:30am    
Sounds like derived class...

1 solution

I made use of generic class as below
XML
public class GenericPaymentMethod<T>
{
    public int TenantID { get; set; }
    public string Market { get; set; }
    public List<T> CreditCard { get; set; }
    public List<T> DebitCard { get; set; }
    public List<T> BPay { get; set; }
}

public class PaymentMethod
{
    public GenericPaymentMethod<string> MyProperty { get; set; }
}

public class EditPaymentMethod
{
    public GenericPaymentMethod<Multicheckbox> MyProperty { get; set; }
}

And in model I accessed in following way
XML
public static EditPaymentMethod GetPaymentMethodDetails(int tenantID) {

    EditPaymentMethod obj = new EditPaymentMethod();
    obj.MyProperty = new GenericPaymentMethod<Multicheckbox>();

    obj.MyProperty.TenantID = 1000;

    obj.MyProperty.Market = "AU";

    obj.MyProperty.CreditCard = new List<Multicheckbox>(){
        new Multicheckbox { CheckboxName = "Amex", IsChecked = true },
        new Multicheckbox { CheckboxName = "Master Card", IsChecked = false },
        new Multicheckbox { CheckboxName = "Visa", IsChecked = true }
    };

    obj.MyProperty.DebitCard = new List<Multicheckbox>(){
        new Multicheckbox { CheckboxName = "Amex", IsChecked = false },
        new Multicheckbox { CheckboxName = "Master Card", IsChecked = true },
        new Multicheckbox { CheckboxName = "Visa", IsChecked = false }
    };

    obj.MyProperty.BPay = new List<Multicheckbox>(){
        new Multicheckbox { CheckboxName = "Savings Cheque", IsChecked = true },
        new Multicheckbox { CheckboxName = "Credit Card", IsChecked = false },
    };

    return obj;
}

And in View
<div>
        <div>
            @Html.Label("Debit Card :")
        </div>
        @for (int i = 0; i < Model.MyProperty.DebitCard.Count; i++)
        {
            <div>
                <div>
                    @Html.CheckBoxFor(x => x.MyProperty.DebitCard[i].IsChecked)
                </div>
                <div>
                    @Model.MyProperty.DebitCard[i].CheckboxName
                    @Html.HiddenFor(x => Model.MyProperty.DebitCard[i].CheckboxName)
                </div>
            </div>
        }
        @Html.HiddenFor(model => Model.AtleastOneDebitCardSelected)
        @Html.ValidationMessageFor(model => Model.AtleastOneDebitCardSelected)
    </div>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900