실제로이 스레드에 제목이 매우 딱딱하다는 것을 알았습니다. 현재 C#에서 닷넷 프로그래밍 코스를 사용 중입니다. 우리는 저장소 서비스 패턴을 사용하여 Windows 양식과 Entity Framework를 사용하여 간단한 라이브러리를 작성하는 작업을 수행했습니다.수업 시간에 항목의 유효성을 검사하는 것이 좋습니다.
나는 책 엔티티를 데이터베이스에 추가하여 일명 새 책을 라이브러리에 추가하는 형식을 가지고 있습니다. 이 수업에서 내가하는 일은 사용자가 실제로 텍스트를 입력했는지 ISBN 번호가 올바른 형식인지, 책이 이미 존재하지 않는지 확인하기 위해 입력란을 확인하는 것입니다. 내가 결정하려고 시도한 것은 프로세스가 수행되는 방식을 구조화하는 방법입니다. 새 책을 제출하기 위해 클릭하면 원래 유효성 검증을 수행하는 on_click 메소드에 if 문이 여러 개있었습니다.
private void btn_bookSubmit_Click(object sender, EventArgs e)
{
string newBookName = this.tb_bookTitle.Text;
string newBookAuthor = this.tb_bookAuthor.Text;
string newBookISBN = this.tb_bookISBN.Text;
string description = this.tb_bookDesc.Text;
if (bookIsNotValid(newBookISBN, newBookName, newBookAuthor))
{
MessageBox.Show(this.validationError);
return;
}
if (bookService.BookTitleExists(newBookName))
{
MessageBox.Show("A book by this title already exists in the library");
return;
}
if (bookService.ISBNExists(newBookISBN))
{
MessageBox.Show("This ISBN belongs to another book in the library. Please double check the ISBN number and try again");
return;
}
if (this.authorService.doesAuthorExistByName(newBookAuthor))
{
DialogResult result = MessageBox.Show
("This author does not exist in the database. Do you want to add this author?",
"Author Does not Exist", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes) this.authorService.addAuthor(newBookAuthor);
if (result == DialogResult.No)
{
MessageBox.Show("New book entry cancled. In order to enter a new book into the system a valid Author must be specified");
return ;
}
}
bookService.addBook(newBookISBN, newBookName, newBookAuthor, description);
MessageBox.Show(
string.Format("{0} succesfully added to the library", newBookName),
string.Format("{0} added Successfully", newBookName),
MessageBoxButtons.OK);
this.clearFields();
}
나는 생각했다. 한 가지 방법에 대해 꽤 많은 코드가 있습니다. 그래서 폼 클래스에서 더 많은 개인 함수로 그것을 분할이 아닌처럼 보였다 방법으로 결국 :
private void btn_bookSubmit_Click(object sender, EventArgs e)
{
string newBookName = this.tb_bookTitle.Text;
string newBookAuthor = this.tb_bookAuthor.Text;
string newBookISBN = this.tb_bookISBN.Text;
string description = this.tb_bookDesc.Text;
if (!isBookValid(newBookISBN, newBookName, newBookAuthor)) return;
if (!isTitleValid(newBookName)) return;
if (!isISBNvalid(newBookISBN)) return;
if (!isAuthorNew(newBookAuthor)) return;
bookService.addBook(newBookISBN, newBookName, newBookAuthor, description);
MessageBox.Show(
string.Format("{0} succesfully added to the library", newBookName),
string.Format("{0} added Successfully", newBookName),
MessageBoxButtons.OK);
this.clearFields();
}
지금은 내 수업에 꽤 몇 가지 방법이있다. 그 좋은 연습 이니? 그것은 제게는 훨씬 깨끗해 보이지만, 클래스를 다룰 때 메소드를 살펴 보는 것이 한 함수 내에서 일어나는 모든 것을 보는 것보다 어렵습니다. 제가 생각한 또 다른 것은 모든 유효성 검사를 많은 함수 대신 하나의 함수로 옮기는 것이 었습니다. 그렇지만 불린 반환과 함수를 다르게 중지하는 방법을 처리해야했습니다.
저는 2 년 동안 내 프로그램을 시험해 보았습니다. 자바 스크립트, PHP, html5, C++, C, 이제 C#에서 내가 가장 즐긴 것을 알아 내려고 노력했습니다. 모든 프로그래밍에서 가장 많은 것을 얻은 것은 제가 아름답고 효율적인 코드를 좋아한다는 것입니다. 나는 아직 그 일을 할 수 없을지 모르지만 나는 그것을 배워야 할 내 가장 열심히 노력하고있다. 그래서 당신이 알아 차릴 수있는 다른 엿 같은 관행은 저에게 알려주세요. 지금까지 모든 것이 클래스에서 잘 작동하며, 실제 질문은 내가 유효성 검사 프로세스를 구현하는 방식입니까? 좋은? 젠장? 느린?
글자를 책으로 매개 변수로 사용하는 Add 메서드를 쉽게 추가 할 수 있지만 서비스 클래스에서 새 책 인스턴스를 만드는 것이 더 적절하다고 생각했습니다. 나는 또한 모든 "메시지 박스 또는 피드백"이 서비스 클래스가 아닌 인터페이스에 포함되어야한다고 생각했다. 실제 유효성 검사 논리 자체는 데이터베이스에서 중복을 확인하는 등 서비스 계층에서 발생합니다. 그러나 내 인터페이스에서 서비스 레이어에 대한 액세스를 사용하여 예를 들어 복제본이 있는지 여부에 대한 응답으로 부울을 가져옵니다. –
네, 당신이 어디에서 왔는지 이해합니다. 저는 최근에 이걸 가지고 고심하고 있습니다. 확인한 시간과 저장 한 시간 사이에 상황이 바뀔 수있는 상황 (예 : 서버에 DB 백 엔드가 있다고 가정 해 봅시다)으로 상황이 계속 될 수있는 것처럼 보입니다. 그냥 예외를 처리하고 메시지 나 뭔가를 보여줄 수 있다고 가정합니다. –