단위 테스트 코드를 이용한 사용 예제
Midnight Peach 2008. 10. 9. 00:02
[주의] 이 글은 MP의 현재 버전 (3.X)에 맞지 않습니다. 단순 참고용으로만 활용하십시오 |
지금부터는 위에서 생성된 코드를 가지고 실제로 CRUD 작업을 하는 코드를 보도록 합시다.
따로 샘플을 만들 필요 없이 MP 개발 중에 사용한 단위 테스트 코드 조각을 예제로 사용하면 될 것 같습니다.
(이하의 코드들은 비주얼 스튜디오 2008의 테스트 프로젝트를 이용하여 작성한 단위 테스트 코드 입니다.)
먼저 각 테스트가 수행되기 전 항상 DB를 초기화하는 메서드를 만들어 놓고 시작하겠습니다.
ManagerRepository.TruncateAll() 메서드는 Memo 테이블의 모든 데이터를 삭제하고 identity 값을 0으로 설정하는(DBCC CHECKIDENT ('Memo', RESEED, 0)) 저장 프로시저에 대한 래퍼입니다.
Get
public List<T> Get() public List<T> Get(int startRowIndex, int maximumRows) public List<T> Get(Expression<Func<T, bool>> where) public List<T> Get(Expression<Func<T, bool>> where, int startRowIndex, int maximumRows) public List<T> Get<U>(Expression<Func<T, U>> orderBy, bool ascending) public List<T> Get<U>(Expression<Func<T, U>> orderBy, bool ascending, int startRowIndex, int maximumRows) public List<T> Get<U>(Expression<Func<T, bool>> where, Expression<Func<T, U>> orderBy, bool ascending) public List<T> Get<U>(Expression<Func<T, bool>> where, Expression<Func<T, U>> orderBy, bool ascending, int startRowIndex, int maximumRows) |
Get은 엔터티의 컬렉션을 반환하는데 매개 변수로 필터링과 정렬과 페이징을 지정할 수 있습니다.
따라서 Get은 총 2 * 2 * 2 = 8 개의 오버로드를 가지고 있습니다.
하나씩 살펴보면 다음과 같습니다.
GetSingle
public T GetSingle(Expression<Func<T, bool>> where) public T GetSingle(PK 매개변수) |
GetSingle은 조건식에 맞는 한 개의 엔터티를 구합니다.
일반적으로 PK가 조건식으로 지정되며, 조건에 맞는 엔터티가 하나도 없으면 null이 반환되고, 두 개 이상이면 InvalidOperationException이 발생합니다.
조건은 람다식으로 지정하거나 변수로 지정할 수 있습니다.
GetFirst
public T GetFirst() public T GetFirst(Expression<Func<T, bool>> where) public T GetFirst<U>(Expression<Func<T, U>> orderBy, bool ascending) public T GetFirst<U>(Expression<Func<T, bool>> where, Expression<Func<T, U>> orderBy, bool ascending) |
조건식에 맞는 엔터티를 지정된 정렬 방법에 따라 정렬했을 때 첫번째 엔터티를 구합니다.
지정된 조건을 만족하는 엔터티가 없으면 null 값이 반환됩니다.
GetLast
public T GetLast() public T GetLast(Expression<Func<T, bool>> where) public T GetLast<U>(Expression<Func<T, U>> orderBy, bool ascending) public T GetLast<U>(Expression<Func<T, bool>> where, Expression<Func<T, U>> orderBy, bool ascending) |
첫번째가 아닌 마지막 엔터티를 반환하다는 점을 제외하고는 GetFirst와 동일합니다.
GetCount
public int GetCount() public int GetCount(int startRowIndex, int maximumRows) public int GetCount(Expression<Func<T, bool>> where) public int GetCount(Expression<Func<T, bool>> where, int startRowIndex, int maximumRows) |
지정된 조건식에 맞는 엔터티의 갯수를 구합니다.
startRowIndex와 maximumRows 매개변수는 지정되더라도 아무런 동작을 하지 않습니다.
(ObjectDataSource가 요구하는 메서드 시그니처를 맞추기 위해 있습니다.)
Create
public T Create() public T Create(DateTime defaultDateTime) |
엔터티를 생성합니다.
리플렉션을 사요하여 엔터티의 필드 중 string형의 필드는 string.Empty로, DateTime형 필드는 DateTime.Today 혹은 지정된 DateTime 값으로 초기화합니다.
기본값이 설정된 상태로 엔터티를 생성할 때 편리합니다.
Clone
public T Clone(T source) |
엔터티를 복사합니다. ColumnAttribute 특성이 지정된 속성만 복사합니다. 주로 Update와 Delete 메서드 내부에서 사용됩니다.
Insert
public virtual void Insert(T entity) |
엔터티를 데이터베이스에 삽입합니다.
Identity 컬럼은 삽입 후 자동으로 설정됩니다.
NULL을 허용하지 않는 컬럼에 값이 설정되지 않으면 SqlException 예외가 발생합니다.
Create 메서드로 생성된 엔터티는 기본값이 설정되어 있으므로 (string과 DateTime형) 이러한 문제를 피할 수 있어 편리합니다.
Update
public virtual void Update(T entity) |
엔터티를 변경합니다.
Delete
public virtual void Delete(T entity) public void Delete(PK 매개변수) |
엔터티를 삭제합니다.
삭제할 엔터티 객체를 지정하거나 PK 매개변수를 지정할 수 있습니다.