2011-02-11 4 views
0

나는 주위를 둘러 보며 일을하는 데이터row를 가지고 있으며 강하게 입력하고 싶지만 강하게 테이블을 타이핑 할 필요는 없다.강력하게 데이터를 입력하는 것이 좋습니까?

isnull 메서드 등을 사용하여 강력한 형식의 행을 자동 생성하는 도구가 있습니까?

+0

엔티티 프레임 워크가 수행해야하는 것이 아닙니까? –

답변

0

강력한 형식의 DataRow/DataSet을 사용하여 컴파일 시간을 확인할 수 있으므로 유용 할 것입니다.

아래의 코드 (단지 샘플)는 제가 어떻게하는지 보여줍니다. 데이터베이스 정보의 모든 클래스, 특히 저장 프로 시저의 출력을 생성하는 도구가 있습니다. 그래서 필드를 아래의 PostDtw 클래스의 속성에 매핑되는 결과 집합을 반환하는 저장 프로 시저가 있습니다.

내 블로그에서 도구 (소스 포함)를 사용할 수 있습니다. 또한 유사한 방식으로 DataReaders 래퍼를 생성합니다. 여기에서 도구를 얻을 수 있습니다. Data Access Layer CodeGen

아래의 "기본"방법은 수업 사용 방법을 보여줍니다. foreach 루프에서 클래스의 속성에 액세스하지만 속성 뒤에 getter가 DataRow를 사용하는 방법에 주목하십시오. 당신은 기본적으로 DataTable의를 사용하지만,

IEnumerable<PostDtw> 

로 "변환"줄 방법

방법 "GetPosts1"와 "GetPosts2는"보여줍니다. GetPosts1은 기본적으로 동일한 인스턴스를 사용하지만 GetPosts2는 각 행에 대해 새 인스턴스를 만듭니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 

namespace ConsoleApplication5 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 
     var posts = GetPosts1(); 
     foreach (var post in posts) 
     { 
     Console.WriteLine(post.PostId); 
     Console.WriteLine(post.PostTitle); 
     Console.WriteLine(post.PostSlug); 
     Console.WriteLine(post.PostDate); 
     } 
    } 

    static IEnumerable<PostDtw> GetPosts1() 
    { 
     DataTable postsDt = GetPostsDataTable(); 
     PostDtw postDtw = new PostDtw(); 

     foreach(DataRow row in postsDt.Rows) 
     { 
     postDtw.DataRow = row; 
     yield return postDtw; 
     }   
    } 

    static IEnumerable<PostDtw> GetPosts2() 
    { 
     DataTable postsDt = GetPostsDataTable(); 
     foreach (DataRow row in postsDt.Rows) 
     yield return new PostDtw(row); 
    } 

    static DataTable GetPostsDataTable() 
    { 
     throw new NotImplementedException(); 
    } 
    } 

    /// <summary> 
    ///This is the Base Class for all DataTable Wrappers 
    /// </summary> 
    public class BaseDataTableWrapper 
    { 
    public DataRow DataRow { get; set; } 

    public BaseDataTableWrapper() 
    { 
    } 

    public BaseDataTableWrapper(DataRow row) 
     : this() 
    { 
     DataRow = row; 
    } 
    } 

    #region [GetPost] 

    /// <summary> 
    ///This class is a wrapper around a DataTable, 
    ///Associated with the stored procedure - GetPost 
    ///This class provides a strongly typed interface to access data from the DataTable 
    ///containing the result of the given stored procedure. 
    /// </summary> 
    public sealed class PostDtw : BaseDataTableWrapper 
    { 
    public Int32 PostId { get { return (Int32)DataRow[0]; } set { DataRow[0] = value; } } 
    public DateTime PostDate { get { return (DateTime)DataRow[1]; } set { DataRow[1] = value; } } 
    public String PostSlug { get { return (String)DataRow[2]; } set { DataRow[2] = value; } } 
    public Int32 UserId { get { return (Int32)DataRow[3]; } set { DataRow[3] = value; } } 
    public String PostTitle { get { return (String)DataRow[4]; } set { DataRow[4] = value; } } 
    public String PostText { get { return (String)DataRow[5]; } set { DataRow[5] = value; } } 
    public Boolean PostIsPublished { get { return (Boolean)DataRow[6]; } set { DataRow[6] = value; } } 
    public Boolean PostIsPublic { get { return (Boolean)DataRow[7]; } set { DataRow[7] = value; } } 
    public String PostTitleImg { get { if (DataRow[8] != DBNull.Value) return (String)DataRow[8]; else return default(String); } set { DataRow[8] = value; } } 

    public PostDtw() 
     : base() 
    { 
    } 

    public PostDtw(DataRow row) 
     : base(row) 
    { 
    } 
    } 

    #endregion [GetPost] 

} 
1

제 생각에는 ADO 데이터 형식에 대한 강력한 형식의 클래스를 만드는 것이 좋습니다.

  1. 손으로 코드가 원하는 동작을 캡슐화 DataRow이든의 서브 클래스 : 당신이 할 수있는 두 가지 방법이 있습니다.
  2. 데이터의 XSD 파일을 작성하고 Visual Studio에서 강력한 형식의 클래스를 구성하게하십시오.

첫 번째 방법의 장점은 사용자가 원하는 것을 정확하게 표시하는 사용자 지정 API를 제공한다는 것입니다. 두 번째 방법은 더 빠릅니다.

+0

두 번째 방법을 사용하면 데이터 세트 (DataTable 및 DataRow보다 많은 오버 헤드가 발생 함)에만 국한되지 않습니다. 테이블의 DataTable 및 DataRow 개체를 인스턴스화 할 수도 있습니다. 두 번째 방법이 매우 바람직합니다. – user3308043

관련 문제