✅ How to improve my code?
I have following 2 methods:
the difference is:
static public UISchemaBuilderBase AddField(
this UISchemaGroupBuilder builder,
CustomField.CustomField field,
CultureInfo culture,
ICustomFieldSetItemParameters parameters = null)
{
var valueRequired = parameters != null
&& parameters.ValueRequired;
switch (field.Type)
{
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
}
return builder;
}
static public UISchemaBuilderBase AddField(
this UISchemaBuilderBase builder,
CustomField.CustomField field,
CultureInfo culture,
ICustomFieldSetItemParameters parameters = null)
{
var valueRequired = parameters != null
&& parameters.ValueRequired;
switch (field.Type)
{
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
}
return builder;
}
static public UISchemaBuilderBase AddField(
this UISchemaGroupBuilder builder,
CustomField.CustomField field,
CultureInfo culture,
ICustomFieldSetItemParameters parameters = null)
{
var valueRequired = parameters != null
&& parameters.ValueRequired;
switch (field.Type)
{
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
}
return builder;
}
static public UISchemaBuilderBase AddField(
this UISchemaBuilderBase builder,
CustomField.CustomField field,
CultureInfo culture,
ICustomFieldSetItemParameters parameters = null)
{
var valueRequired = parameters != null
&& parameters.ValueRequired;
switch (field.Type)
{
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
}
return builder;
}
builder type
but another code is the same2 Replies
If that's the only difference, why not make the method generic?
Or even better, assuming
UISchemaGroupBuilder
inherits from UISchemaBuilderBase
there's no need for the formerI don't think it's good solution
let's see code of two classes:
in this code:
I always call
public class UISchemaGroupBuilder : UISchemaBuilderBase
{
public UISchemaGroupBuilder AddBoolean(
string fieldName,
string fieldTitle,
bool? isRequired = null)
{
AddBoolean(fieldName, fieldTitle, isRequired, _name);
return this;
}
}
public class UISchemaBuilderBase
{
public UISchemaBuilderBase AddBoolean(
string fieldName,
string fieldTitle,
bool? isRequired = null,
string groupName = null )
{
var schemaControl = new UIControlBooleanAttribute();
CreateUISchemaItem(
fieldName,
fieldTitle,
groupName,
isRequired,
schemaControl);
return this;
}
}
public class UISchemaGroupBuilder : UISchemaBuilderBase
{
public UISchemaGroupBuilder AddBoolean(
string fieldName,
string fieldTitle,
bool? isRequired = null)
{
AddBoolean(fieldName, fieldTitle, isRequired, _name);
return this;
}
}
public class UISchemaBuilderBase
{
public UISchemaBuilderBase AddBoolean(
string fieldName,
string fieldTitle,
bool? isRequired = null,
string groupName = null )
{
var schemaControl = new UIControlBooleanAttribute();
CreateUISchemaItem(
fieldName,
fieldTitle,
groupName,
isRequired,
schemaControl);
return this;
}
}
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
case CustomFieldType.Boolean:
var booleanTitle = field.Title.GetValue(culture);
builder.AddBoolean(
booleanTitle,
booleanTitle,
valueRequired);
break;
AddBoolean
from UISchemaBuilderBase
but sometimes I need to call AddBoolean
from UISchemaGroupBuilder