Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

16 juillet 2007 1 16 /07 /juillet /2007 23:14
[ Code Fluent ]
Requêter en . NET 3.0
avec Linq To Objects  
 

j’ai parlé un peu vite on peut quand même bien sur requêter avec  Linq To Objects sur les classes collections générées avec Code Fluent (il suffit de caster)
le but étant de pouvoir trier et filtrer sur la couche métier
           ContactCollection Contacts = ContactCollection.LoadAll();
 
            var Query =
                from c in Contacts.Cast<Contact>()
                where c.ContactName.StartsWith("R")
                orderby c.ContactName
                select c;
 
 
            dataGridView1.DataSource = Query.ToList<Contact>();
           
 
Se créer des methodes recevant  un délégué afin de simuler les predicates des generics de .NET 2.0
-          Avec une méthode FindAll (possible en .NET 1.0) dans la classe Collection (ici ContactCollection)
        public delegate bool Predicate(Contact obj);
 
        public ContactCollection FindAll(Predicate predicate)
        {
            ContactCollection result = new ContactCollection();
            foreach (Contact oContact in this)
            {
                if (predicate(oContact))
                    result.Add(oContact);
            }
            return result;
        }
 
 
Utilisation :
private void button1_Click(object sender, EventArgs e)
        {
            ContactCollection Contacts = ContactCollection.LoadAll();
            ContactCollection result = Contacts.FindAll(new stringPredicate(stringCondition.StartsWith, "ContactName", "R").Predicate);
            dataGridView1.DataSource = result;
           
        }
 
-          Avec les extensions methods (à partir de .NET 3.0)
 public static class extensionMethods
    {
        public static ContactCollection FindAll(this ContactCollection source, System.Predicate<Contact> predicate)
        {
            ContactCollection result = new ContactCollection();
            foreach (Contact oContact in source)
            {
                if (predicate(oContact))
                    result.Add(oContact);
            }
            return result;
        }
    }
 
Note :
La classe que j’utilise ici
public class stringPredicate
    {
        private stringCondition _Condition;
 
        ///<summary>
        ///
        ///</summary>
        public stringCondition Condition
        {
            get { return _Condition; }
            set { _Condition = value; }
        }
        private string _Property;
 
        ///<summary>
        ///
        ///</summary>
        public string Property
        {
            get { return _Property; }
            set { _Property = value; }
        }
        private string _Value;
 
        ///<summary>
        ///
        ///</summary>
        public string Value
        {
            get { return _Value; }
            set { _Value = value; }
        }
 
        ///<summary>
        ///
        ///</summary>
        public stringPredicate()
        { }
 
        ///<summary>
        ///
        ///</summary>
        ///<param name="Condition"></param>
        ///<param name="Property"></param>
        ///<param name="Value"></param>
        public stringPredicate(stringCondition Condition, string Property, string Value)
        {
            this.Condition = Condition;
            this.Property = Property;
            this.Value = Value;
        }
 
        ///<summary>
        ///
        ///</summary>
        ///<param name="Condition"></param>
        ///<param name="Property"></param>
        ///<param name="Value"></param>
        public void SetProperties(stringCondition Condition, string Property, string Value)
        {
            this.Condition = Condition;
            this.Property = Property;
            this.Value = Value;
        }
 
        ///<summary>
        ///
        ///</summary>
        ///<param name="obj"></param>
        ///<returns></returns>
        public bool Predicate(object obj)
        {
            bool result = false;
            string CurrentPropertyValue = Convert.ToString(PredicateUtils.GetValue(obj, Property));
 
            switch (Condition)
            {
                case stringCondition.Contains:
                    result = CurrentPropertyValue.Contains(Value);
                    break;
                case stringCondition.EndWith:
                    result = CurrentPropertyValue.ToLower().EndsWith(Value.ToLower());
                    break;
                case stringCondition.EndWithMatchCase:
                    result = CurrentPropertyValue.EndsWith(Value);
                    break;
                case stringCondition.Equals:
                    result = CurrentPropertyValue.Equals(Value);
                    break;
                case stringCondition.Length:
                    if (CurrentPropertyValue.Length > Convert.ToInt32(Value))
                        result = true;
                   else
                        result = false;
                    break;
                case stringCondition.StartsWith:
                    result = CurrentPropertyValue.ToLower().StartsWith(Value.ToLower());
                    break;
                case stringCondition.StartsWithMatchCase:
                    result = CurrentPropertyValue.StartsWith(Value);
                    break;
                case stringCondition.IsMatch:
                    Regex oRegex = new Regex(Value);
                    result = oRegex.IsMatch(CurrentPropertyValue);
                    break;
            }
 
            return result;
        }
    }
    public class PredicateUtils
    {
        public static object GetValue(object obj, string Property)
        {
            return obj.GetType().GetProperty(Property).GetValue(obj, null);
        }
    }
    public enum stringCondition
    {
        Contains,
        EndWith,
        EndWithMatchCase,
        Equals,
        Length,
        StartsWith,
        StartsWithMatchCase,
        IsMatch
    }
 
 

Partager cet article

Repost 0

commentaires