Table:
CREATE TABLE Instrument
( Id INT IDENTITY
, Name VARCHAR(50)
, Tenor VARCHAR(10)
//...
Model:
interface ITenor
{
int Length { get; }
string ToString();
}
class DayTenor : ITenor
{
public int Length
{
get { return 1; }
}
public override string ToString()
{
return "DAY";
}
}
class MonthTenor : ITenor
{
public int Length
{
get { return 30; }
}
public override string ToString()
{
return "MONTH";
}
}
class TenorFactory
{
ITenor GetTenor(string tenorString)
{
switch (tenorString)
{
case "DAY":
return new DayTenor();
break;
case "MONTH":
return new MonthTenor();
break;
default:
throw new NotImplementedException(string.Format( "Tenor {0} is not implemented", tenorString ));
}
}
}
class Instrument
{
public int Id { get; set; }
public string Name { get; set; }
public ITenor Tenor { get; set; }
}
class InstrumentMap : ClassMap<Instrument>
{
public InstrumentMap()
{
WithTable( "Instrument" );
Id( x => x.Id );
Map( x => x.Name );
Map( x => x.Tenor );
}
}
This is a great simplification of the problem domain.
The line Map( x => x.Tenor );
will clearly not work, as you cannot implicitly convert the VARCHAR column to an ITenor type. Is there a way I can map to automatically use the Factory to get the ITenor it requires for the specified string in the DB, and use the ToString() from the ITenor class to revert back to the DB?
If not, what refactoring would you recommend to make this feasable? Many thanks