开发者

Reuse code on MVP pattern

I have an user registration functionality on my MVP pattern project and also a problem.

User registration can me made in some different places, and depending on开发者_如何学C where it is, some fields are required or not.

For example, if the user is being registered on the web, a credit card information is required but it is not required it the registration is being made the admin area.

I'm not sure how to make this, how to impose this rule.

thanks!


As @Adam Rackis said, your question is a bit vague, but I'll take a guess at what you mean.

If you're just trying to remove some fields from the form, just add a conditional to your view:

@if(!Model.inAdminArea)
{
<!-- put credit card field, etc here -->
}

Depending on your model's validation settings, you may need to modify the model to create dummy entries for those fields so that you don't get validation errors.

But, this seems like I'm oversimplifying - add some detail to your question.


First I would recommend the interface segregation principle when dealing with views: public interface IForm{ event EventHandler Submit; event EventHandler Cancel; }

public interface IFormData{ TDto Item {get;set;} }

Then use a derived interface:

public interface MyFormInterface : IFormData , IForm {}

Then also presenters can be based on scenarios only and remain domain agnostic. etc...

Hope this helps!


It sounds like you need to have multiple Views and Presenters. The Views can possibly follow some inheritance chain to gain reuse on the Views.

// Base requirements for user registration.
public interface IUserRegistrationView {
    string FirstName { get; }
    string LastName { get; }
    string EmailAddress { get; }
    string Password { get; }
}

public interface ISelfRegistrationView : IUserRegistrationView {
    string CreditCardNumber { get; }
    CardType CreditCardType { get; }
    DateTime CreditCardExpirationDate { get; }    
}

Then you will need two presenters. One for Admin registrations and another for Self Registrations.

As long as your have a supporting business service of some sort to do the actual work (creating users) - then you can do something like this...

public class AdminRegisterNewUserPresenter : BasePresenter
{
    private readonly IUserRegistrationView view = null;
    public AdminRegisterNewUserPresenter(IUserRegistrationView view) { this.view = view; }

    public void RegisterNewUser()
    {
        try 
        {
            UserBusinessService service = new UserBusinessService();
            service.AdminRegisterNewUser(this.view.FirstName, 
                this.view.LastName, this.view.EmailAddress, this.view.Password);
        } 
        catch(Exception e)
        {
            base.HandleError(e);
        }
    }
}

public class SelfRegistrationPresenter : BasePresenter
{
    private readonly ISelfRegistrationView view = null;
    public SelfRegistrationPresenter(ISelfRegistrationView view) { this.view = view; }

    public void RegisterNewUser()
    {
        try 
        {
            UserBusinessService service = new UserBusinessService();
            service.NewUserSelfRegistration(this.view.FirstName, 
                this.view.LastName, this.view.EmailAddress, this.view.Password, 
                this.view.CreditCardNumber, this.view.CreditCardType, this.view.CreditCardExpirationDate);
        } 
        catch(Exception e)
        {
            base.HandleError(e);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜