Data Access Application Block for .NET v2.0 에서 볼 수 있는 팁

  • Post author:
  • Post category:
  • Post comments:0 Comments
  • Post last modified:December 11, 2004

1. sealed 키워드
sealed 키워드로 SqlHelper 클래스를 상속받지 못하게끔 해놨다.

public sealed class SqlHelper
{
……
}

2. 권한을 private으로 설정한 생성자
SqlHelper 클래스는 static 메써드만을 지원하므로, 사용자가 SqlHelper 클래스의 인스턴스를 생성하지 못하도록 private 생성자만을 정의했다.

// Since this class provides only static methods, make the default constructor private to prevent
// instances from being created with “new SqlHelper()”
private SqlHelper() {}

3. 예외 처리
샘플 코드를 보면 Inner Exception을 다루는 법이 나와 있다. 다음과 같이 처리한다.

catch(Exception ex)
{
string errMessage = “”;

for( Exception tempException = ex; tempException != null ; tempException tempException.InnerException )
{
errMessage += tempException.Message + Environment.NewLine + Environment.NewLine;
}
}

4. using 키워드
using키워드를 사용하여 SqlConnection 개체를 생성한다. 이렇게 하면 using이 종료될 때 자동으로 Dipose가 호출된다.

using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

// Call the overload that takes a connection in place of the connection string
return ExecuteScalar(connection, commandType, commandText, commandParameters);
}

MSDN는 using 키워드에 대해 다음과 같이 설명한다.

using 문을 종료할 때 개체에서 Dispose가 호출되도록 using 문에 인스턴스를 만듭니다. using 문의 끝에 도달하거나 또는 문 중간에서 예외가 throw되어 제어가 문 블록을 벗어나면 using 문이 종료될 수 있습니다.

인스턴스화할 개체는 System.IDisposable 인터페이스를 구현해야 합니다.

Author Details
Kubernetes, DevSecOps, AWS, 클라우드 보안, 클라우드 비용관리, SaaS 의 활용과 내재화 등 소프트웨어 개발 전반에 도움이 필요하다면 도움을 요청하세요. 지인이라면 가볍게 도와드리겠습니다. 전문적인 도움이 필요하다면 저의 현업에 방해가 되지 않는 선에서 협의가능합니다.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments