Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于MVP模式的代码重用_C#_.net_Mvp - Fatal编程技术网

C# 基于MVP模式的代码重用

C# 基于MVP模式的代码重用,c#,.net,mvp,C#,.net,Mvp,我的MVP模式项目中有一个用户注册功能,这也是一个问题 用户注册可以在一些不同的地方进行,并且取决于注册的位置,某些字段是必需的或不需要的 例如,如果用户正在web上注册,则需要信用卡信息,但不需要信用卡信息,因为注册是在管理区域进行的 我不知道如何做到这一点,如何实施这一规则 谢谢 正如@Adam Rackis所说,你的问题有点含糊不清,但我猜一下你的意思 如果您只是想从表单中删除一些字段,只需在视图中添加一个条件: @if(!Model.inAdminArea) { <!-- put

我的MVP模式项目中有一个用户注册功能,这也是一个问题

用户注册可以在一些不同的地方进行,并且取决于注册的位置,某些字段是必需的或不需要的

例如,如果用户正在web上注册,则需要信用卡信息,但不需要信用卡信息,因为注册是在管理区域进行的

我不知道如何做到这一点,如何实施这一规则


谢谢

正如@Adam Rackis所说,你的问题有点含糊不清,但我猜一下你的意思

如果您只是想从表单中删除一些字段,只需在视图中添加一个条件:

@if(!Model.inAdminArea)
{
<!-- put credit card field, etc here -->
}
@if(!Model.inAdminArea)
{
}
根据模型的验证设置,您可能需要修改模型,为这些字段创建虚拟条目,以避免出现验证错误


但是,这似乎过于简单化了-请为您的问题添加一些细节。

首先,我建议在处理视图时使用接口隔离原则: 公共接口格式{ 事件处理程序提交; 事件处理程序取消; }

公共接口数据{ TDto项{get;set;} }

然后使用派生接口:

公共接口MyFormInterface:IFormData,IForm {}

然后,演示者也可以只基于场景,保持领域不可知。 等等


希望这有帮助

听起来你需要有多个视图和演示者。视图可能遵循某种继承链,以获得视图上的重用

// 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; }    
}
然后您将需要两名演示者。一个用于管理员注册,另一个用于自注册

只要您有某种类型的支持业务服务来完成实际工作(创建用户),那么您就可以这样做

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);
        }
    }
}

您将需要编写一些代码,并使您的问题更具体+1,以便让UI处理更改,最大限度地减少所需的后端代码量。