Foundry derivatives and dynamic advanced techniques

I am working on a project in C # 4.0. I have multiple presenter classes that all inherit from the same base class. Each of these presenter classes has a "current" object that is specific to each presenter, but they all also share a common inherited class separate from presenters.

In terrible pseudocode:

class ApplicationPresenter inherits BasePresenter
   Pubilc PersonApplication Current (inherited from Person)
class RecordPresenter inherits BasePresenter
   Public PersonRecord Current (inherited from Person)
class InquiryPresenter inherits BasePresenter
   Public PersonInquiry Current (inherited from Person)

      

... etc.

Is there a way to do this so that I can call "current" from any of them, without requiring type detection, but keeping it consistent with best practices?

The best option I have is to just make it dynamic, since I know that whatever I get will be "current" and do it that way. It is right?

Or is there a way I could create:

class BasePresenter
   Public Person Current

      

And do this cast appropriately?

I know there are ways to get around this, but I'm looking for a clean and correct one this time.

Thank!

+3


source to share


1 answer


Add a generic type type to the base presenter so that any derived concrete presenter can specify the custom type of the current element:



public abstract class PresenterBase<TItem>
{
   public TItem Current { get; private set; }
}

// now Current will be of PersonApplication type
public sealed class ApplicationPresenter : PresenterBase<PersonApplication>
{
}

// now Current will be of PersonRecord type
public sealed class RecordPresenter : PresenterBase<PersonRecord>
{
}

      

+5


source







All Articles