Share via


コード スニペット: SpecificFinder の実装

最終更新日: 2010年4月19日

適用対象: SharePoint Server 2010

この記事の内容
.NET Connectivity Assembly での例
ASP.NET Web サービスでの例
WCF サービスでの例
その他のコード例

以下のコード例では, .NET Connectivity Assembly と Web サービスに、SpecificFinder メソッド インスタンスを実装する方法を示します。

.NET Connectivity Assembly での例

public Customer GetCustomerByID(String id)
{
    foreach (Customer customer in customers)
    {
        if (customer.CustomerID.Equals(id) && !customer.IsDeleted)
        {
            return customer;
        }
    }

    throw new ArgumentOutOfRangeException("id", "Customer not found");
}   

ASP.NET Web サービスでの例

[WebMethod]
public Customer GetCustomerByID(String id)
{
    foreach (Customer customer in customers)
    {
        if (customer.CustomerID.Equals(id) && !customer.IsDeleted)
        {
            return customer;
        }
    }

    throw new ArgumentOutOfRangeException("id", "Customer not found");
}

WCF サービスでの例

以下のコードは、サービス コントラクト インターフェイスでの操作定義を示します。

[OperationContract]
Customer GetCustomerByID(string id);

以下の例は、メソッド インスタンスの実装を示します。

public Customer GetCustomerByID(String id)
{
    foreach (Customer customer in customers)
    {
        if (customer.CustomerID.Equals(id) && !customer.IsDeleted)
        {
            return customer;
        }
    }

    throw new ArgumentOutOfRangeException("id", "Customer not found");
}

その他のコード例

  1. 外部システム - データベース/SQL Server

    たとえば、Microsoft SQL Server データベースの Contact エンティティに対しては、SpecificFinder メソッドは以下のようになります。

    public static Contact ReadItem(int contactID)
    {
        const string ServerName = "MySQLServerName";
        AdventureWorksDataContext dataContext = new AdventureWorksDataContext
              ("Data Source=" + ServerName + ";" +
               "Initial Catalog=AdventureWorks;Integrated Security=True");
    
        Contact Contact =
            (from contacts in dataContext.Contacts.AsEnumerable()
             where contacts.ContactID == contactID
             select contacts).Single();
        return Contact;
    }
    
  2. 外部システム - フラット ファイル

    public static IEnumerable<FlatFileEntity> ReadList()
    {
        List<FlatFileEntity> flatFileEntityList = new List<FlatFileEntity>();
        TextReader textReader = 
            new StreamReader(@"c:\data\flat-file-data-source.txt");
        string row;
    
        while ((row = textReader.ReadLine()) != null)
        {
            FlatFileEntity flatFileEntity = new FlatFileEntity();
    
            string[] entityData = row.Split(',');
    
            flatFileEntity.ID = entityData[0];
            flatFileEntity.Company = entityData[1];
            flatFileEntity.FirstName = entityData[2];
            flatFileEntity.LastName = entityData[3];
            flatFileEntity.Address = entityData[4];
            flatFileEntity.City = entityData[5];
            flatFileEntity.State = entityData[6];
            flatFileEntity.ZipCode = entityData[7];
            flatFileEntity.Phone = entityData[8];
            flatFileEntity.LastUpdated = DateTime.Parse(entityData[9]);
            flatFileEntityList.Add(flatFileEntity);
        }
    
        textReader.Close();
    
        foreach (FlatFileEntity entity in flatFileEntityList)
        {
            if (entity.ID == id)
                return entity;
        }
        return null;
    }
    

関連項目

概念

SpecificFinder の実装