I had a person with long Java experience help me with a C # project at one site. Some of the code used a fluid interface, but I wasn't quite convinced. I tried to create a sample code by referring to the code at that time.
Specifications) Update the DB by merging the screen values with the results obtained from the DB.
/**Main processing**/
private Dao _dao = new Dao();
public void Main()
{
//Get the current information from the DB.
var param = new UpdateParameter(_dao)
.SetHeader(1)
.SetHeader2("a")
.SetHeader3(2);
//Reflect the information on the screen.
SetValueFromView(param);
//Update the DB.
_dao.Update(param);
}
private void SetValueFromView(UpdateParameter param)
{
//Header1 of the parameter that got the value of the screen,2,Reflect in 3.
//Code omitted
}
/**Parameter class that summarizes the DTO of the DB update table**/
public class UpdateParameter
{
public Header Header { get; private set; }
public Header2 Header2 { get; private set; }
public Header3 Header3 { get; private set; }
private readonly Dao _dao = null;
public UpdateParameter(Dao dao)
{
_dao = dao;
}
public UpdateParameter SetHeader(int id)
{
Header = _dao.GetHeader(id);
return this;
}
public UpdateParameter SetHeader2(string code)
{
Header2 = _dao.GetHeader2(code);
return this;
}
public UpdateParameter SetHeader3(int id)
{
Header3 = _dao.GetHeader3(id);
return this;
}
}
/** Header1,2,3 DTO**/
public class Header
{
public int Id { get; set; }
}
public class Header2
{
public string Code { get; set; }
}
public class Header3
{
public int Id { get; set; }
}
/**Dao for data acquisition and update**/
public class Dao
{
public Header GetHeader(int id)
{
//Get header
return new Header() { Id = id };
}
public Header2 GetHeader2(string code)
{
//Get header 2
return new Header2() { Code = code };
}
public Header3 GetHeader3(int id)
{
//Get header 3
return new Header3() { Id = id };
}
public void Update(UpdateParameter param)
{
//Execute DB update process. Code omitted
}
}
private Dao dao = new Dao();
public void main() {
//Get the current information from the DB.
UpdateParameter param = new UpdateParameter(dao)
.setHeader(1)
.setHeader2("a")
.setHeader3(2);
//Reflect the information on the screen.
setValueFromView(param);
//Update the DB.
dao.update(param);
}
private void setValueFromView(UpdateParameter param) {
//Header1 of the parameter that got the value of the screen,2,Reflect in 3.
//Code omitted
}
public class UpdateParameter {
private Header header = null;
private Header2 header2 = null;
private Header3 header3 = null;
private final Dao dao;
public UpdateParameter(Dao dao) {
this.dao = dao;
}
public UpdateParameter setHeader(int id) {
this.header = dao.getHeader(id);
return this;
}
public UpdateParameter setHeader2(String code) {
this.header2 = dao.getHeader2(code);
return this;
}
public UpdateParameter setHeader3(int id) {
this.header3 = dao.getHeader3(id);
return this;
}
public Header getHeader() {
return header;
}
public Header2 getHeader2() {
return header2;
}
public Header3 getHeader3() {
return header3;
}
}
/** Header1,2,3 DTO**/
public class Header {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class Header2 {
private String Code;
public String getCode() {
return Code;
}
public void setCode(String code) {
Code = code;
}
}
public class Header3 {
private int Id;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
}
/**Dao for data acquisition and update**/
public class Dao {
public Header getHeader(int id) {
//Get header
Header header = new Header();
header.setId(id);
return header;
}
public Header2 getHeader2(String code) {
//Get header 2
Header2 header2 = new Header2();
header2.setCode(code);
return header2;
}
public Header3 getHeader3(int id) {
//Get header 3
Header3 header3 = new Header3();
header3.setId(id);
return header3;
}
public void update(UpdateParameter param) {
//Execute DB update process. Code omitted
}
}
It seems that the interface is such that UpdateParameter flows, but this code has the following problems.
You can see the advantage of a flowing interface if it is a class like StringBuilder. On the other hand, it feels strange to use it in a class that only has simple information such as UpdateParameter.
/**Main processing**/
private Dao _dao = new Dao();
public void Main()
{
//Get the current information from the DB.
var param = new UpdateParameter();
//Get data from DB in main process.
param.Header = _dao.GetHeader(1);
param.Header2 = _dao.GetHeader2("a");
param.Header3 = _dao.GetHeader3(2);
//Reflect the information on the screen.
SetValueFromView(param);
//Update the DB.
_dao.Update(param);
}
private void SetValueFromView(UpdateParameter param)
{
//Header1 of the parameter that got the value of the screen,2,Reflect in 3.
//Code omitted
}
/**Parameter class that summarizes the DTO of the DB update table**/
public class UpdateParameter
{
public Header Header { get; set; }
public Header2 Header2 { get; set; }
public Header3 Header3 { get; set; }
//Remove Dao reference
}
/** Header1,2,3 DTO**/
public class Header
{
public int Id { get; set; }
}
public class Header2
{
public string Code { get; set; }
}
public class Header3
{
public int Id { get; set; }
}
/**Dao for data acquisition and update**/
public class Dao
{
public Header GetHeader(int id)
{
//Get header
return new Header() { Id = id };
}
public Header2 GetHeader2(string code)
{
//Get header 2
return new Header2() { Code = code };
}
public Header3 GetHeader3(int id)
{
//Get header 3
return new Header3() { Id = id };
}
public void Update(UpdateParameter param)
{
//Execute DB update process. Code omitted
}
}
private Dao dao = new Dao();
public void main() {
//Get the current information from the DB.
UpdateParameter param = new UpdateParameter();
//Get data from DB in main process.
param.setHeader(dao.getHeader(1));
param.setHeader2(dao.getHeader2("a"));
param.setHeader3(dao.getHeader3(2));
//Reflect the information on the screen.
setValueFromView(param);
//Update the DB.
dao.update(param);
}
private void setValueFromView(UpdateParameter param) {
//Header1 of the parameter that got the value of the screen,2,Reflect in 3.
//Code omitted
}
public class UpdateParameter {
private Header header = null;
private Header2 header2 = null;
private Header3 header3 = null;
//Remove Dao reference
public void setHeader(Header header) {
this.header = header;
}
public void setHeader2(Header2 header2) {
this.header2 = header2;
}
public void setHeader3(Header3 header3) {
this.header3 = header3;
}
public Header getHeader() {
return header;
}
public Header2 getHeader2() {
return header2;
}
public Header3 getHeader3() {
return header3;
}
}
//The following code is omitted
I just deleted the Dao reference of UpdateParameter and got the DB on the main processing side, but this code is convincing.
I have about a year of Java development experience, so I'm wondering if the code shown in the sample is normal for Java. Also, what is the reason for using a fluid interface even if the problems described in "What I was interested in" are left as they are.
[Previous article (code collection that is a little worrisome)] (http://qiita.com/csharpisthebest/items/7d9d2c7e4da4b89488fd)
[Next article (redundant assignment statement)] (http://qiita.com/csharpisthebest/items/f9ebc741e40037553d5b)
Recommended Posts