Reputation:
I am wondering what would be the best design for a problem I am having
I have a page in my application in which a form is displayed on the page where a user can input data. Depending on the type of user that logs in, there will be some common text boxes, dropdowns etc. and some user specific input data fields.
So for example, for an internal user all input fields will be displayed and for external users only a limited about of input fields will be displayed.
The issue is that there should be 4-5 different forms displayed depending on the user
Current i have a base page and the form page inherits the page page
public class BasePage
{
PageFactory.InitElements(); etc.
}
Public class FormPage : Base Page
{
// All elements and methods for all types of forms
}
Here is where I am unsure what is the best approach
Should i create separate classes for each form type and inherit FormPage?
I added an example below but my application is very complex but it should give you an idea of the design
public class FormInternalUser : Form Page
{
public void SubmitFormAction(string inputTextBox1, string inputTextBox2, string inputTextBox3, string inputTextBox4, string user)
{
// code
attachFile(user)
}
public override void attachFile(string user)
{
//code
}
// other specific methods
}
public class FormExternalUser : Form Page
{
public void SubmitFormAction(string inputTextBox1, string inputTextBox2, string user)
{
// code
attachFile(user)
}
public override void attachFile(string user)
{
// code
}
// other specific methods
}
or is there a better approach? Maybe Strategy Design Pattern or something similar?
Upvotes: 0
Views: 60
Reputation: 16
I guess your currently form on the page should be depend on UserType, so for example it could be as:
// type of users
public enum UserType {
Type1,
Type2,
Type3
}
// the page from which you will be input the data
public class MainPage {
public IUserDataForm UserDataFormComponent => GetUserDataForm();
// factory method to get the certain form depends on user type
private IUserDataForm GetUserDataForm() {
UserType userType = UserType.Type1; // get a value from somewhere (or pass it into constructor e.g.)
switch(userType) {
case Type1: return new User1DataForm(); break;
case Type2: return new User2DataForm(); break;
case Type3: return new User3DataForm(); break;
}
}
}
// user data class to pass the class instead count of variables the methods
public class UserData {
string Text1;
string Text2;
string TextN;
}
// input data page interface
public interface IUserDateForm {
void FillUserData(UserData data);
}
// base page for input data pages - needs if we have duplicated logic in the all other pages (could be removed if not)
public abstract UserDateForm :IUserDateForm{
// common locators, etc.
public override void FillUserData(UserData data) {
// common input logic
}
}
// implementation of
public class User1DateForm : UserDateForm {
public override void FillUserData(UserData data) {
base().FillUserData(data);
// input logic
}
}
public class User2DateForm : UserDateForm {
public override void FillUserData(UserData data) {
base().FillUserData(data);
// input logic
}
}
public class User3DateForm : UserDateForm {
public override void FillUserData(UserData data) {
base().FillUserData(data);
// input logic
}
}
How to use:
public void Test1 {
UserData data = new UserData() { Text1 = "", Text2 = "", TextN = "" };
MainPage main = new MainPage();
main.UserDataFormComponent.FillUserData(data);
}
Upvotes: 0