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开发者_JAVA百科 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.
You may take a look at AutoMapper.
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
}
}
精彩评论