수정/삭제/업데이트를 동시에 하기 위해 만든 클래스입니다.
한번에 수정/삭제/업데이트를
bool IsNew { set; get; }
bool IsDeleted { get; set; }
bool IsEdited { get; set; }
가지고 확인 가능합니다.
환경설정
데이터 클래스는 아래의 두개 인터페이스를 구현해야 가능
INotifyPropertyChanged(using System.ComponentModel). , IDataContextState (걍 제가만든..)
가 구현되어져 있어야 프로퍼티가 수정되었을때 IsEdit 변경가능
아래인터페이스는 수정/추가/삭제 상태변경을 확인하기 위해 만든 인터페이스 입니다.
/// <summary>
/// 데이터베이스의 상태관리를 합니다.
/// </summary>
public interface IDataContextState
{
bool IsNew { set; get; }
bool IsDeleted { get; set; }
bool IsEdited { get; set; }
}DataGridView에서 프로퍼티가 수정되면 INotifyPropertyChanged 이벤트가 발생합니다.
IsEdit를 변경시키고 있습니다.
INotifyPropertyChanged 구현
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
this.IsEdited = true; //상태변경
}
}IDataContextState 구현
#region IDataContextState Members
private bool _IsNew = false;
private bool _IsDeleted = false;
private bool _IsEdited = false;
public bool IsNew
{
set
{
this._IsNew = value;
}
get
{
//고유증감 번호라든지 생성날짜가 없으면 신규로 본다
return this._CreateTimesamp == null && !this.IsDeleted;
}
}
public bool IsDeleted
{
set
{
this._IsDeleted = value;
}
get
{
return this._IsDeleted;
}
}
public bool IsEdited
{
set
{
this._IsEdited = value;
}
get
{
return this._IsEdited;
}
}
#endregionRepositoryManager.cs 구현
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace PersonnelManagement.Data
{
/// <summary>
/// TODO RepositoryManager 데이터를 관리하는 클래스
/// </summary>
/// <typeparam name="T"></typeparam>
public class RepositoryManager<T> : BindingList<T>
{
BindingList<T> DeleteBindingList = new BindingList<T>();
/// <summary>
/// 신규추가
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public void IsNew(T t)
{
if (t != null)
{
Type type = t.GetType().GetInterface("IDataContextState");
if (type != null)
{
((Contract.Data.IDataContextState)t).IsNew = true;
this.Add(t);
}
}
}
/// <summary>
/// (사용안함 ) INotifyPropertyChanged 로 자동 수정 상태변경
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public void IsUpdate(T t)
{
if (t != null)
{
Type type = t.GetType().GetInterface("IDataContextState");
if (type != null)
{
((Contract.Data.IDataContextState)t).IsEdited = true;
}
}
}
/// <summary>
/// 실제로 삭제하지 않고 상태만 변경
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public void IsDelete(T t)
{
if (t != null)
{
Type type = t.GetType().GetInterface("IDataContextState");
if (type != null)
{
((Contract.Data.IDataContextState)t).IsDeleted = true;
foreach (var item in this.ToList() )
{
bool result = Object.Equals(item , t );
if (result) {
((Contract.Data.IDataContextState)item).IsDeleted = true;
DeleteBindingList.Add(t); //삭제된건 DeleteBindingList 담아둔다.
this.Remove(t);
break;
}
} //end for
}
}
}
public void AddRange(List<T> list)
{
foreach (var item in list)
{
this.Add(item);
}
}
/// <summary>
/// 삭제 아이템을 포함 전체 리스트 반환
/// </summary>
/// <returns></returns>
public List<T> GetBindingList()
{
//삭제된 내용 담기
foreach (var item in this.DeleteBindingList)
{
this.Add(item);
}
return this.ToList();
}
}
}
사용방법
바인딩
//리스트 형식의 데이터를 받아옴
List<Contract.Data.Doc.DocCategory> list = docManager.GetDocCategory(string.Empty);
//담기
DocCategoryRepositoryManager.AddRange(list);
//그리드 뷰의 바인딩하기
docCategoryBindingSource.DataSource = DocCategoryRepositoryManager;가져오기
List<DocCategory> resultList = this.DocCategoryRepositoryManager.GetBindingList();
이걸 사용하기 위해서는 아래의 인터페이스가 구현되어져 있어야함
추가
//추가
Contract.Data.Doc.DocCategory dc = new Contract.Data.Doc.DocCategory();
DocCategoryRepositoryManager.IsNew(dc);삭제
foreach (DataGridViewRow row in this.dgvDocCategory.SelectedRows)
{
DocCategory dc = row.DataBoundItem as Contract.Data.Doc.DocCategory;
if (dc != null)
{
DocCategoryRepositoryManager.IsDelete(dc);
}
}'C#.NET' 카테고리의 다른 글
| [VS 2005,비주얼 스튜디오] 잘못된 바인딩 핸들입니다. (0) | 2010.06.01 |
|---|---|
| [Application/ Application 기본정보 알기] 응용프로그램의 시작위치를 알고 싶을떄? (0) | 2010.05.27 |
| [#if/조건부 지시문] 디버그모드 릴리즈모드 사용하기 (0) | 2010.05.24 |
| [AssemblyName] 파일이 어셈블리인지 확인 / 외부어셈블리 정보 확인 (0) | 2010.05.20 |
| [Microsoft Translator] Bing API를 이용한 초간단 번역기 만들기 (2) | 2010.05.13 |
| 엑셀출력시 0 사라짐 (0) | 2010.04.08 |
| enum 이름 가져오기 와 리소스(Resources)사용법 (0) | 2010.04.02 |
| [MSDN] EventHandler (0) | 2010.03.31 |
| [C#]사용자 아이피 알아오기 (0) | 2010.03.17 |
| [MSDN][BindingList] 데이터 바인드 개선 (0) | 2010.03.12 |