본문 바로가기

Programming Language/C#

[ C# ] Model 구현, GraphQL Api 연동 예제

Data (Class 생성)


// AnomalyGridData.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rl_AMS_UI.Model.Grid
{
    public class AnomalyGridData
    {
        // GridHeader
        public string 생산번호 { get; set; } = string.Empty;
        public string 이상감지구분 { get; set; } = string.Empty;
        public string 이상감지시작일시 { get; set; } = string.Empty;
        public string 이상감지종료일시 { get; set; } = string.Empty; 
        public string 예측결과 { get; set; } = string.Empty; 
        public string MainProgNo { get; set; } = string.Empty; 
        public string SubProgNo { get; set; } = string.Empty; 
        public string SOV { get; set; } = string.Empty; 
        public string FOV { get; set; } = string.Empty; 
        public string OffsetX { get; set; } = string.Empty; 
        public string OffsetY { get; set; } = string.Empty; 
        public string OffsetZ { get; set; } = string.Empty; 
    }
}

 

Model (Class 생성, Api 연동)


// AnomalyGridModel.cs

using Rl_AMS_UI.Model.Grid;
using SciChart.Data.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.Newtonsoft;
using static Rl_AMS_UI.Model.Chart.AnomalyStatsModel;
using Rl_AMS_UI.Model.Chart;

namespace Rl_AMS_UI.ViewModel
{
    public class AnomalyGridViewModel
    {
        public ObservableCollection<AnomalyGridData> GridData { get; set; } = new ObservableCollection<AnomalyGridData>();

        public async Task FetchDataAsync()
        {
            // Api 접근
            var graphQLClient = new GraphQLHttpClient("URL", new NewtonsoftJsonSerializer());

            // GraphQLRequest 객체를 생성해 쿼리와 필요한 변수 정의
            var request = new GraphQLRequest
            {
                Query = @"
                    query Abnormals {
                        abnormals {
                            abnormalBeginDate
                            abnormalCode
                            abnormalDescription
                            abnormalEndDate
                            createAt
                            fov
                            imageString
                            mainProgramNo
                            productNo
                            sov
                            subProgramNo
                            toolOffset
                            updateAt
                        }
                    }"
            };

            // 응답
            var response = await graphQLClient.SendQueryAsync<dynamic>(request);

            foreach (var item in response.Data.abnormals)
            {
                GridData.Add(new AnomalyGridData
                {
                    생산번호 = item.productNo,
                    이상감지구분 = item.abnormalCode,
                    이상감지시작일시 = item.createAt,
                    이상감지종료일시 = item.abnormalEndDate,
                    예측결과 = item.abnormalDescription,
                    MainProgNo = item.productNo,
                    SubProgNo = item.subProgramNo,
                    SOV = item.sov,
                    FOV = item.fov,
                    OffsetX = item.toolOffset,
                    OffsetY = item.toolOffset,
                    OffsetZ = item.toolOffset
                });
            }


        }
    }


}

 

 

Data 바인딩


// AnomalyGrid.cs (UserControl)

using Rl_AMS_UI.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Rl_AMS_UI.UserCustomControls
{
    /// <summary>
    /// AnomalyGrid.xaml에 대한 상호 작용 논리
    /// </summary>
    /// 
    public partial class AnomalyGrid : UserControl
    {
        private AnomalyGridViewModel _model;

        public AnomalyGrid()
        {
            InitializeComponent();

            _model = new AnomalyGridViewModel();
            this.Loaded += async (sender, args) =>
            {
                await _model.FetchDataAsync();
                this.DataContext = _model;
            };

        }

    }
}

'Programming Language > C#' 카테고리의 다른 글

[C#] 전처리기  (0) 2023.12.27
[C#] Event  (0) 2023.12.27
[C#] Delegate(대리자)  (0) 2023.12.27
[C#] 메서드 파라미터 ( ref, out )  (0) 2023.12.27
[C#] Nullable 타입 ( ?, ?? )  (1) 2023.12.27