0

I'm making an MVC2 app to manage billing schemes. These billing schemes can be of various types, e.g. daily, weekly, monthly, etc. and all types have their specific properties. My class structure looks like this:

public abstract class BillingScheme { /* basic billing scheme properties */ }
public class BillingSchemeMonthly : BillingScheme
{
    public string SpecificMonths;
}
//etc. for other scheme types

I retrieve a billing scheme through the base class using the billing scheme ID, and it gives me an object of the correct type.

The property SpecificMonths maps to a database varchar field that contains a ;-separated string of month numbers. I want to split this to an array so I can create a list of checkboxes and I'd preferably do this with a facade property in a viewmodel. I could do it right inside the view but that seems to go against the MVC pattern.

The problem is I can only create this viewmodel specifically for a BillingSchemeMonthly, and I can't cast a BillingSchemeMonthly to a BillingSchemeMonthlyViewModel. I'd rather not implement a clone method in the viewmodel since the entire billing scheme is quite large and the number of properties may grow.

Thanks in advance for any help.

2 Answers 2

1

You may take a look at AutoMapper.

Sign up to request clarification or add additional context in comments.

1 Comment

Good suggestion! Will look into this.
0

Ideally when converting from Models to ViewModels you'll have a seperate converter class.

In our MVC projects we have two types of converter, IConverter<TFrom,TTo> defining a Convert method, and ITwoWayConverter<TFrom,TTo> defining ConvertTo and ConvertFrom methods.

We then inject our converters into the necessary controller and use them to do all our conversions.

This keeps everything nice and tidy and allows a clear seperation of concerns.

When we impliment this, we'd create a new class specifically for the type of conversion we're looking to do:

    public class MonthlySchemeBillingToMonthlySchemeBillingViewModelConverter :      
                     IConverter<MonthlySchemeBilling,MonthlySchemeBillingViewModel>
        {
         public MonthlySchemeBillingViewModel Convert(MonthlySchemeBilling)
         {
           // Impliment conversion
         }
        }

1 Comment

That's basically equal to implementing a Clone method, isn't it? I was hoping to avoid manually filling all the properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.