I'm using EF (Code first) and I'd like to isolate the properties from the methods (many of the methods reference other libraries that I don't want my database project to have to reference).
As an example, say I have this class
public class Person
{
public string Name { get; set; }
public void SayName()
{
SomeExternalAssembly.Say(Name)
}
}
Because SomeExternalAssembly
has nothing to do with the database I'd like to move the SayName
method to another assembly while maintaining it in a "version" of Person
.
In my ideal world (which doesn't work and is probably a bad idea) I'd inherit Person
to DerivedPerson
then just cast it back to Person
when I need to save it to the DB. While I can perform the cast, when I attempt to add it to the DB I get a System.InvalidOperationException: Mapping and meta data information could not be found for EntityType 'DerivedPerson'
.
Since that didn't work the next best solution I can come up with is just adding a Person
property to my new class:
public class PersonB
{
public Person Data;
public void SayName()
{
SomeExternalAssembly.Say(Data.Name)
}
}
While this would work, I'm looking for something a little cleaner (and ideally less code changes). The next thought I had was to go with inheriting then add a conversion method using reflection.
Does anyone have a better approach?
Say(name, someExternalAssembly.NameSayer)
? Or even better,SomeExternalAssembly.SayName(anyArbitraryObjectWithAName.Name)
that accepts a mere string as input?