I have several identical object groups, each of them consists of entities mapped to main table and couple linking tables, for example
//group1
class OrderNote
{
public int OrderNoteId {get; set;}
public virtual ICollecton<OrderNoteTag> OrderNoteTags {get; set;}
}
class OrderNoteTag
{
public int OrderNoteId {get; set;}
public int TagId {get; set;}
}
//////
//group 2
class ClientNote
{
public int ClientNoteId {get; set;}
public virtual ICollecton<ClientNoteTag> ClientNoteTags {get; set;}
}
class ClientNoteTag
{
public int ClientNoteId {get; set;}
public int TagId {get; set;}
}
/////
Now, I need a method which allows me to process the main objects by the same routine, so I won't have to duplicate the same code a lot of times. My idea is to have some base classes for notes and tags, the concrete types will inherit from them, and the method will accept the base type for note. But I can't figure out how to declare and inherit the navigation properties , so they are mapped on the concrete tag type, but can be processed as base type.
Here's something like it should be:
public class TagBase
{
public int NoteId {get; set;}
public int TagId {get; set;}
}
public class NoteBase
{
public int NoteId {get; set;}
public virtual ICollecton<TagBase> NoteTags {get; set;}
}
//then we inherit
public class OrderNoteTag : TagBase {}
public class OrderNote : NoteBase
{
//Here we should pass the concrete type OrderNoteTag to NoteTags collection somehow
}
// Then we have method, where we should be able to pass OrderNote or ClientNote
public void ProcessNote(NoteBase note)
{
foreach(var tag in note.NoteTags){...blah-blah-blah...}
}
Thanks in advance.
public void ProcessNote<TNote>(TNote note) where TNote : NoteBase?