SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

現役エンジニア直伝! 「現場」で使えるコンポーネント活用術(CData)(AD)

EntityFramework対応で、よりC#やVB.NETのコードからアクセスしやすくなった「CData for Salesforce」

「CData ADO.NET Provider for Salesforce」の紹介

  • このエントリーをはてなブックマークに追加

コードからEFを利用する

 準備が完了したので、SalesforceContextを使ってデータを取得するModelクラスを作成します。

リスト4 SalesforceModelクラス
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Runtime.CompilerServices

Namespace Models
    Public Class SalesForceModel
        Implements INotifyPropertyChanged

        Public Property Items As New ObservableCollection(Of Campaign)

        Public Sub GetCampaign()
            Dim ents = New SalesforceContext()
            Dim campaignQuery = From item In ents.Campaign Order By item.Id Select item

            Me.Items = New ObservableCollection(Of Campaign)(campaignQuery)
            OnPropertyChanged("Items")
        End Sub

        Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) _
            Implements INotifyPropertyChanged.PropertyChanged
        Private Sub OnPropertyChanged(<CallerMemberName> _
                                      Optional propertyName As String = Nothing)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub
    End Class
End Namespace

 SalesforceContextはLINQで操作できる形のオブジェクトになるので、取得後にLINQを使って並び替えを行っています。その後、ObservableCollection型のItemsプロパティに設定し画面からBindingにより参照できるようにしておきます。

Binding用ViewModel

 ModelとViewを連携するためのViewModelを作成します。

リスト5 MainViewModel
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports CZ1411Spread.Models
Imports System.Collections.ObjectModel
Imports CZ1411Spread.Common

Namespace ViewModels
    Public Class MainViewModel
        Implements INotifyPropertyChanged

        Private WithEvents Model As New SalesForceModel

        Public Property Items As ObservableCollection(Of Campaign)
            Get
                Return Me.Model.Items
            End Get
            Set(value As ObservableCollection(Of Campaign))
                Me.Model.Items = value
            End Set
        End Property

        Private _GetCommand As RelayCommand
        Public Property GetCommand() As RelayCommand
            Get
                If _GetCommand Is Nothing Then
                    _GetCommand = New RelayCommand(AddressOf Me.GetCampaign)
                End If
                Return _GetCommand
            End Get
            Set(value As RelayCommand)
                _GetCommand = value
            End Set
        End Property

        Public Sub GetCampaign()
            Dim userID = "hatsune@wankuma.com"
            Dim password = "hogehoge"
            Me.Model.GetCampaign(userID, password)
        End Sub

        Private Sub Model_PropertyChanged(sender As Object, …
            OnPropertyChanged(e.PropertyName)
        End Sub

        Public Event PropertyChanged(sender As Object, …

        Private Sub OnPropertyChanged(<CallerMemberName> Optional propertyName  …
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub
    End Class
End Namespace

GridViewを使ったView

 ObservableCollectionをGridViewに表示するViewを定義します。

リスト6 MainWindow.xaml
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sg="http://schemas.grapecity.com/windows/spreadgrid/2012" 
    x:Class="Views.MainWindow"
    Title="CZ1412Salesforce" Height="300" Width="498">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <DataGrid HorizontalAlignment="Left" 
                  VerticalAlignment="Top"
                  ItemsSource="{Binding Items}"
                  CanUserSortColumns="True" />
        <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10">
            <Button Content="GET" Command="{Binding GetCommand}" />
        </StackPanel>
    </Grid>
</Window>

実行結果

 このサンプルを実行すると、データが表示されていない画面が表示されます。[GET]ボタンをクリックすると、ViewModelのGetCommandが実行されてデータの取得が行われます。

実行結果
実行結果

まとめ

 EntityFramworkを利用することでADO.NETインターフェースのConnectionやCommand、データセットなどを意識する必要がなくなります。共通クラスとしてデータアクセス部分を隠蔽することで意図しないSQLの発行を未然に防ぐことができますし障害時の切り分けや対応もしやすくなります。ある程度以上の規模になるとEntityFrameworkは必要な手法になるので、CDataとあわせて効率よい開発を実現してみてはいかがでしょうか。

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
この記事の著者

初音玲(ハツネアキラ)

 国内SIerのSEでパッケージ製品開発を主に行っており、最近は、空間認識や音声認識などを応用した製品を手掛けています。 個人的には、仕事の内容をさらに拡張したHoloLensなどのMRを中心に活動しています。 Microsoft MVP for Windows Development ブログ:http://hatsune.hatenablog.jp/

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

【AD】本記事の内容は記事掲載開始時点のものです 企画・制作 株式会社翔泳社

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/8733 2016/03/14 11:36

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング