質問

In a module of a win forms application I have a hierarchy of classes like BaseRecovery, LoanRecovery and FineRecovery. Both LoanRecovery and FineRecovery inherits from BaseRecovery. All these models have one view called RecoveryForm where you can enter/view loans and fines of employees. and I'm planing to use one Presenter class called RecoveryPresenter which inherits from BasePresenter.

LoanRecovery class will be as follows...

Class LoanRecovery : BaseRecovery
{
    public string LoanID {get;set;}
    public DateTime IssueDate {get;set;}
    public Decimal Amount {get;set;}
    .
    .
    .
}

So I'd do the following in Programe.cs.

IBaseRcovery recovery=null;
IRecoveryForm recoveryForm = new RecoveryForm();
IDataService dataService = new DataService();

BasePresenter presenter = new RecoveryPresenter( recovery, recoveryForm, dataService );
presenter.Show(); // This will display the form recoveryForm

In the presenter I would say

public RecoveryPresenter( IBaseRecover model, IRecoveryForm view, IDataService dataService )
{
    this._Model = model;
    this._View = view;
    this._DataService = dataService;            
    WireUpViewEvents();

}

Now lets say if I need to give a loan, I would run SetModelPropertisFromView() mehtod in the BasePresenter class which use reflection. But before that I should pass an instance of LoanRecovery class (i.e. a Model).

I know that the run time polimorphism could be used in this case but I have no idea how to to that! Please could you let me know a way to to this?

Is it ok if I do in Presenter like this?

    public void Issue()
    {
        if (_View.Type == "Loan")
        {
            _Model = new LoanRecovery();
        }

        if (_View.Type == "Fine")
        {
            ...
        }

        if (_View.Type == "Insurance")
        {
            ...
        }

        SetModelPropertiesFromView(_Model, _View, _DataService); //Update model properties from the view
        dataService.InsertLoan(_Model);
    } 
役に立ちましたか?

解決

Looking at your code you are already using polymorphism through the IRecovery interface. Basically polymorphism is a way of interacting with different types of objects through a common interface. This can be through an actual interface definition, for example IRecovery, or a base class such as BaseRecovery. You can read more here:

Polymorphism (C# Programming Guide)

From looking at your code samples I would make one or two changes. First make your BaseRecovery class abstract so that I instance cannot be created. This will force you to use either LoanRecovery or FineRecovery which are more meaningful, concrete, implementations of IRecovery. In your programe.cs replace the new BaseRecovery with either new LoanRecovery or new FineRecovery based on what you want to display in your view.

When it comes to updating the model with values from your presenter all you have to do is run your reflection code. The SetModelPropertiesFromView method should take a IRecovery and in there you call GetType() which will return type information for whatever type of object IRecovery is pointing at. From this type information you can then enumerate the properties and update each property with the values from your view.

EDIT 1: It just occurred to me that you run into a problem with this. The reason being you are using the same view for updating two different models so how does the view know which model it is updating and therefore which properties need to be set. The only thing I can think of is your SetModelPropertiesFromView looks at the type information and if it's one type then update some properties, but if it's another type then update other properties.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top