2011-11-16 2 views
0

Utilisation de Subsonic 2.1 Je veux que l'appel de ma méthode aux résultats ressemble à ceci: results(searchCriteria) maintenant je dois passer le CollectionType ainsi que le type.Dans Subsonic 2.1, comment faire pour que cet appel générique prenne un paramètre générique?

Animal searchCriteria = GetSearchCritera(); 
AnimalCollection results = results<Animal, AnimalCollection>(searchCriteria); 
// I want the call to be results(searchCriteria); 

Voici la méthode des résultats que je veux juste prendre Y

public static T results<Y, T>(Y searchCriteria) 
    where Y: ReadOnlyRecord<Y>, new() 
    where T: ReadOnlyList<Y, T>, new() 
{ 
    using (IDataReader results = ReadOnlyRecord<Y>.Find(searchCriteria)) 
    { 
     T a = new T(); 
     a.Load(results); 
     return a; 
    } 
} 
+0

Comment voulez-vous * * attendre pour savoir quel type de collection pour créer si vous avez seulement dit-il 'Animal' ? Il pourrait y avoir plusieurs collections appropriées. –

+1

On suppose que le type 'Collection' est toujours' blahCollection', où 'blah' est le type d'enregistrement sous-jacent. – mellamokb

+0

@David: Vous pouvez regarder la version la plus récente, [SubSonic 3.0] (http://subsonicproject.com/) – mellamokb

Répondre

1

J'ai fait cette classe:

public class ConcreteList<T> : ReadOnlyList<T, ConcreteList<T>> where T: ReadOnlyRecord<T>, new() 
    { 
     public ConcreteList() { } 
    } 

changé ce code:

public static ConcreteList<T> results2<T>(T searchCriteria) 
     where T : ReadOnlyRecord<T>, new() 
    { 
     using (IDataReader results = ReadOnlyRecord<T>.Find(searchCriteria)) 
     { 
      ConcreteList<T> a = new ConcreteList<T>(); 
      a.Load(results); 
      return a; 
     } 
    } 

et Je suis capable de l'appeler comme ceci:

Animal searchCriteria = GetSearchCritera(); 
    ConcreteList<Animal> results = results2(searchCriteria); 

Oh oui, je voulais que ce soit une méthode d'extension:

public static class ReadOnlyRecordExtensions 
{ 
    public static ConcreteList<T> ExecuteFind<T>(this T searchCriteria) 
      where T : ReadOnlyRecord<T>, new() 
    { 
     using (IDataReader results = ReadOnlyRecord<T>.Find(searchCriteria)) 
     { 
      ConcreteList<T> list = new ConcreteList<T>(); 
      list.Load(results); 
      return list; 
     } 
    } 
} 
Questions connexes