0

Ok, so I have a question regarding the EventArgs that can be passed when an event is triggered. I am designing a small, basic search engine and have a class called Query that contains a method Search. When this method is called, I want to trigger an event which will pass the results to be storred in a variety of cache class instances (SizeBoundedCache and TimeBoundedCache). So I thought the best way to do this would be to use an event.

The delegate is declared like this ->

public delegate void CacheStoreDelegate(object sender, EventArgs e);

The rest of the code within the Query class relevant to this question is here (uses Linq) ->

public event CacheStoreDelegate AddToCache;

public virtual void OnQuery (EventArgs e)
{
    if(AddToCache != null)
        AddToCache(this, e);
}

public Query()
{
}

public Query(string queryString, OOP5.Provided.QueryOperator op)
{
    //Access and set the terms array
    this.Terms = OOP5.Provided.QueryUtils.GetTermsFromString(queryString);
    this.Operator = op; 
}

public static IEnumerable<string> Search (this SearchCore s, IQuery q)
{
    // Accept a query and return IEnumerable<string> of
    // all document IDs matching that query
    if (q.Operator == QueryOperator.Any)
    {
        var GetAnyMatch = from single_query in q.Terms
                          group s.Search(single_query)
                          by s.documents.Keys
                          into results
                          where results.Count >= 1
                          select results[0];

        this.OnQuery(GetAnyMatch);
        return GetAnyMatch;
   }

   if (q.Operator == QueryOperator.All)
   {
       var GetAllMatch = from single_query in q.Terms
                         group s.Search(single_query)
                         by s.documents.Keys
                         into results
                         where results.Count >= q.Terms.Lengthselect results[0];

        this.OnQuery(GetAllMatch);
        return GetAllMatch;
   }
}

All the cache classes will be notified whenever a search is called and I also need thme to receive the results.

Thanks so much in advance for the help. Also, if there is a more elegant way to do this that I am not thinking of, please chime in. Cheers!

4

2 に答える 2

1

eventargs から派生するタイプ CacheStoreEventArgs のクラスを作成します

public class CacheStoreEventArgs:eventargs
{
    private IEnumerable<string> Data;//List<string> better

    public IEnumerable<string> data
    {
        get { return Data; }
        set { this.Data = value; }
    }

    public CacheStoreEventArgs(IEnumerable<string> NewData)
    {
        this.data = NewData;
    }
}

次に、イベントを宣言します(事前定義された汎用のものを使用するため、宣言する必要はありません)

public event EventHandler<CacheStoreEventArgs> AddToCache;

メソッド検索内で、メソッドを「On....」と呼びます

public static IEnumerable<string> Search (this SearchCore s, IQuery q)
{
    //after you get query result
    CacheStoreEventArgs cs = new CacheStoreEventArgs(queryresultvariablehere);
    //and call your method now with the instance of your derived eventargs class
    OnQuery(cs);
}

public virtual void OnQuery (CacheStoreEventArgs e)
{
     try
     {
        EventHandler<CacheStoreEventArgs> temp = AddToCache
        if( temp != null)
              temp(this,e);
     }
     catch(Exception ex) 
     {
        //exception handling
     }
}
于 2013-06-22T23:19:50.430 に答える