Introduction
A requirement of a certain client was as follows:
- Display a view that some of its content hide and show dynamically.
- The show and hide is determined by a value selected from a drop-down list.
Example
////////////////////////
<select id="ddlShowHideDiv">
<option value="0">select </option>
<option value="1">show div1</option>
<option value="2">show div2</option>
</select>
<div id="div1">
div 1 content
</div>
<div id="div2">
div 2 content
</div>
<script>
$("#ddlShowHideDiv").change(function() {
if ($(this).val() == "1")) {
$("#div1").css("display", "block")
$("#div2").css("display", "none")
}
if ($(this).val() == "2")) {
$("#div1").css("display", "none")
$("#div2").css("display", "block")
}
})
</script>
///////////////////////////////////
The problem in this implementation is that it,
- Breaks Single Responsibility Principle, since the rendering is the responsibility of the controller.
- It has broken the Open-Closed Principle when the Js Code has been modified to deal with a new value-added to the drop-down.
Solution Implementation
The design of the solution fixed the problem through the following,
- Moving the rendering part to the controller
- Applying Factory Pattern in order to maintain the application of Open Closed Principle
Solution implementation is illustrated through the following walkthrough.
Solution Implementation WalkThrough
Create a new ASP.NET MVC Core application in Visual Studio 2019 as follows - (Name the project ApplyingFactoryPatternToRepresentDifferentUI).


Under the Models folder, create a new Model Class called a base model.
The following code is to be placed in the class created,
public abstract class BaseModel {
public string CommonField1 {
get;
set;
}
public string CommonField2 {
get;
set;
}
}
The purpose of this class is to contain the common fields which always appear in the view.
Create the following 2 classes in the same place as the previous one,
public class DerivedModel1: BaseModel {
public string Model1Field1 {
get;
set;
}
public string Model1Field2 {
get;
set;
}
}
public class DerivedModel2: BaseModel {
public string Model2Field1 {
get;
set;
}
public string Model2Field2 {
get;
set;
}
}
The purpose of these 2 classes is to contain fields that show and hide conditionally.
Create a folder called creator with the interfaces and classes as below,
//The interface code
interface IModelCreator {
public IActionResult Create();
}
//The DerivedModel1Creator class code
public class DerivedModel1Creator: Creator.Intrface.IModelCreator {
public IActionResult Create() {
PartialViewResult viewResult = new PartialViewResult();
viewResult.ViewName = "DerivedModel1";
return viewResult;
}
}
//The DerivedModel1Creator class code
public class DerivedModel2Creator: Creator.Intrface.IModelCreator {
public IActionResult Create() {
PartialViewResult viewResult = new PartialViewResult();
viewResult.ViewName = "DerivedModel2";
return viewResult;
}
}
The interface will act as an extension base that, when implemented, will render different content conditionally.
Create 2 new partial views named DerivedModel1, DerivedModel2 respectively under the views home folder.
The DerivedModel1 View code,
//// @model ApplyingFactoryPatternToRepresentDifferentUI.Models.DerivedModel1 @{ ViewData["Title"] = "DerivedModel1"; } <h1>View Model1</h1>
<hr />
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label asp-for="Model1Field1" class="control-label"></label>
<input asp-for="Model1Field1" class="form-control" />
<span asp-validation-for="Model1Field1" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Model1Field2" class="control-label"></label>
<input asp-for="Model1Field2" class="form-control" />
<span asp-validation-for="Model1Field2" class="text-danger"></span>
</div>
</div>
</div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} } ////
Join the conversation! Your thoughts help the community grow.