Partager via


Extrait de code : Implémentation d’un Finder

Dernière modification : lundi 19 avril 2010

S’applique à : SharePoint Server 2010

Dans cet article
Exemple pour un assembly de connectivité .NET
Exemple pour un service Web ASP.NET
Exemple pour un service WCF
Exemples de code supplémentaires

Les exemples de code suivants montrent comment implémenter une instance de méthode Finder dans un assembly de connectivité .NET et dans un service Web.

Exemple pour un assembly de connectivité .NET

public Customer[] GetCustomers(String name, int? limit)
{
    if (name != "")
    {
        List<Customer> customersWithName = new List<Customer>();

        foreach (Customer customer in customers)
        {
            //$Name$
            if (name.StartsWith("$"))
            {
                if (name.EndsWith("$"))
                {
                    if (customer.Name.Contains(name.Substring(1, 
                        (name.Length - 2))))
                    {
                        customersWithName.Add(customer);
                    }
                }
                else
                {
                    if (customer.Name.EndsWith(name.Substring(1, 
                        (name.Length - 1))))
                    {
                        customersWithName.Add(customer);
                    }
                }
            }
            else if (name.EndsWith("$"))
            {
                if (customer.Name.StartsWith(name.Substring(0, 
                    (name.Length - 1))))
                {
                    customersWithName.Add(customer);
                }
            }
            //Name
            else if (customer.Name == name)
            {
                customersWithName.Add(customer);
            }
        }

        if (limit < 1)
            return customersWithName.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customersWithName.Count;
                i++)
                limitedcustomers.Add(customersWithName[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();

        }
    }

    else
    {
        if (limit < 1)
            return customers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customers.Count; i++)
                limitedcustomers.Add(customers[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        }
    }
}

Exemple pour un service Web ASP.NET

[WebMethod]
public Customer[] GetCustomers(String name, int? limit)
{
    if (name != "")
    {
        List<Customer> customersWithName = new List<Customer>();

        foreach (Customer customer in customers)
        {
            //$Name$
            if (name.StartsWith("$"))
            {
                if (name.EndsWith("$"))
                {
                    if (customer.Name.Contains(
                        name.Substring(1, (name.Length - 2))))
                    {
                        customersWithName.Add(customer);
                    }
                }
                else
                {
                    if (customer.Name.EndsWith(
                        name.Substring(1, (name.Length - 1))))
                    {
                        customersWithName.Add(customer);
                    }
                }
            }
            else if (name.EndsWith("$"))
            {
                if (customer.Name.StartsWith(
                    name.Substring(0, (name.Length - 1))))
                {
                    customersWithName.Add(customer);
                }
            }
            //Name
            else if (customer.Name == name)
            {
                customersWithName.Add(customer);
            }
        }

        if (limit < 1)
            return customersWithName.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customersWithName.Count; 
                i++)
                limitedcustomers.Add(customersWithName[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();

        }
    }

    else
    {
        if (limit <1)
            return customers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customers.Count; 
                i++)
                limitedcustomers.Add(customers[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        }
    }
}

Exemple pour un service WCF

Le code suivant illustre la définition d’opération dans l’interface de contrat de service.

[OperationContract]
Customer[] GetCustomers(string name, int? limit);

L’exemple suivant illustre l’implémentation de l’instance de méthode.

public Customer[] GetCustomers(String name, int? limit)
{
    if (name != "")
    {
        List<Customer> customersWithName = new List<Customer>();

        foreach (Customer customer in customers)
        {
            //$Name$
            if (name.StartsWith("$"))
            {
                if (name.EndsWith("$"))
                {
                    if (customer.Name.Contains(
                        name.Substring(1, (name.Length - 2))))
                    {
                        customersWithName.Add(customer);
                    }
                }
                else
                {
                    if (customer.Name.EndsWith(
                        name.Substring(1, (name.Length - 1))))
                    {
                        customersWithName.Add(customer);
                    }
                }
            }
            else if (name.EndsWith("$"))
            {
                if (customer.Name.StartsWith(
                    name.Substring(0, (name.Length - 1))))
                {
                    customersWithName.Add(customer);
                }
            }
            //Name
            else if (customer.Name == name)
            {
                customersWithName.Add(customer);
            }
        }

        if (limit < 1)
            return customersWithName.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customersWithName.Count; 
                i++)
                limitedcustomers.Add(customersWithName[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();

        }
    }

    else
    {
        if (limit < 1)
            return customers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        else
        {
            int limitvalue = Convert.ToInt32(limit);
            List<Customer> limitedcustomers = new List<Customer>();
            for (int i = 0; i < limitvalue & i < customers.Count; i++)
                limitedcustomers.Add(customers[i]);
            return limitedcustomers.FindAll(
                cust => cust.IsDeleted == false).ToArray();
        }
    }
}

Exemples de code supplémentaires

  1. Système externe—Base de données/SQL Server

    Par exemple, pour l’entité Contact dans une base de données Microsoft SQL Server, la méthode Finder peut ressembler à ce qui suit.

    public static IEnumerable<Contact> ReadList(int itemLimit)
    {
        const string ServerName = "MySQLServerName";
        AdventureWorksDataContext dataContext = new AdventureWorksDataContext
              ("Data Source=" + ServerName + ";" +
               "Initial Catalog=AdventureWorks;Integrated Security=True");
    
        IEnumerable<Contact> Contacts =
            from contacts in dataContext.Contacts.Take(itemLimit)
            select contacts;
        return Contacts;
    
    }
    

    Voici un exemple légèrement plus complexe de méthode Finder ayant des paramètres filtrables.

    SELECT ProductID, Name, ProductNumber, ListPrice FROM Product 
    WHERE (ProductID >= @MinProductID) AND (ProductID <= @MaxProductID) 
    AND (Name LIKE @Name) AND ProductNumber 
    LIKE @ProductNumber)
    
  2. Système externe—Fichier plat

    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();
    
        return flatFileEntityList.toArray();
    }
    
  3. Exemple de pagination

    public static IEnumerable<Document> ReadFolder(
        string parentFolderID, int pageNumber)
    {
        //Required to allow SQL CE to work in an ASP.NET environment
        AppDomain.CurrentDomain.SetData(
            "SQLServerCompactEditionUnderWebHosting", true);
    
        DMSEntities ctx = new DMSEntities(connString);
    
        int tid = int.Parse(parentFolderID);
        string username = GetCurrentDMSUsername();
    
        /* SQL CE does not support paging so we must return all results,
           create an anonymous type, and then take the page. In production,
           you would do this all in a single LINQ statement. */
        var a = ((from d in ctx.Documents
                  where (d.ParentId == tid)
                  select new { 
                      d.Name, d.Id, d.ParentId, d.IsFolder }).
                      OrderBy(d => !d.IsFolder)).ToList();
    
        //Get authorized documents by page.
        var p = a.Where(c => 
            IsAuthorized(username, c.Id) == true).
            Skip((pageNumber - 1) * PageSize).Take(PageSize);
    
        List<Document> docs = new List<Document>();
    
        foreach (var i in p)
        {
            Document doc = new Document();
            doc.ID = i.Id.ToString();
            doc.Name = i.Name;
            doc.ParentID = i.ParentId.ToString();
            if (i.IsFolder)
                doc.Type = "Folder";
            else
                doc.Type = "Document";
            doc.Version = "Published";
    
            docs.Add(doc);
        }
    
        return docs;
    }
    

Voir aussi

Concepts

Implémentation de Finder