CodeSmith Community
Your Code. Your Way. Faster!

Find(), GetPaged() Overload: Search by Entity

Latest post 01-30-2007 4:14 PM by Tyrven. 2 replies.
  • 01-30-2007 7:35 AM

    • Tyrven
    • Not Ranked
    • Joined on 01-30-2007
    • Redmond, WA
    • Posts 8
    • Points 160

    Find(), GetPaged() Overload: Search by Entity

    A feature I've seen in a lot of DALs and have used in the past is the ability to pass an entity instance to a search method (e.g., Find(), GetPaged()) in order to pass search parameters.  I find this approach particularly elegant, personally, and was surprised not to find it in .netTiers. I would see this being an alternative to using SqlFilterParameterCollection.

    For example, if my data entity object is Product: 

        Product oProduct = new Product();
        oProduct.Name = "A%";
        oProduct.DateAdded = "2007-01-01";   

        TList<Customers> list1 = DataRepository.CustomersProvider.Find(oProduct);

    A similar option, with slightly more flexibility, would be to build an [Entity]Search class that inherits from the [Entity] class but implements additional search metadata, such as:

        oProduct.Name = "A";
        oProduct.NameSearchMethod = ">";
        oProduct.DateAdded = "2007-01-01";
        oProduct.DateAddedSearchMethod = "<=";

    I can see arguments for and against this.  Obviously, even with the Search class option this is a bit limited in that it can't support multiple conditions per field.  Nonetheless, I don't see it as being terribly difficult to add and it wouldn't break existing code so it would be a definite "nice to have".

    Jeremy
     

     


    • Post Points: 35
  • 01-30-2007 11:55 AM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 764
    • Points 22,530

    Re: Find(), GetPaged() Overload: Search by Entity

    Hi Jeremy,

     Have you taken a look at the [Entity]ParameterBuilder classes?  I think they provide the functionality you are looking for and are more complete than what you have listed above.

    ProductsParameterBuilder pb = new ProductsParameterBuilder();
    pb.AppendGreaterThan(ProductsColumn.ProductName, "A");
    pb.AppendLessThanOrEqual(ProductsColumn.LastUpdate, "1/1/07");
    
    TList<Products> list = DataRepository.ProductsProvider.Find(pb.GetParameters());
    

    There are a whole slew of different append options and you can also control the junction operator (and, or, in).  Bobby also had a post that showed how you can effectively group parameters together with parenthesis here:

    http://community.codesmithtools.com/forums/thread/21225.aspx

    As an exampe, take the following code:

    ProductsParameterBuilder pb = new ProductsParameterBuilder();
    pb.AppendGreaterThan(ProductsColumn.ProductName, "A");
    pb.AppendLessThanOrEqual(ProductsColumn.LastUpdate, "1/1/07");
    pb.Append("AND (", ProductsColumn.ProductName, "%st%", true);
    pb.Append("OR", ProductsColumn.ProductName, "%nd%", true);
    pb.Append(") AND", "1", "1", false);
    
    TList<Products> list = DataRepository.ProductsProvider.Find(pb.GetParameters());
    

    This results in the following filter expression being built:

     (ProductName > @Param0)
     AND (LastUpdate <= @Param1)
     AND ( (UPPER(ProductName) LIKE UPPER(@Param2))
     OR (UPPER(ProductName) LIKE UPPER(@Param3))
     ) AND (1 = @Param4)
    

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 55
  • 01-30-2007 4:14 PM In reply to

    • Tyrven
    • Not Ranked
    • Joined on 01-30-2007
    • Redmond, WA
    • Posts 8
    • Points 160

    Re: Find(), GetPaged() Overload: Search by Entity

    Yes - I didn't mean for this to replace or compete with ParameterBuilder, because I think that's a lot more fully featured (and a pretty slick feature).  This was intended more as a simple and intuitive shortcut for quick search/filter requirements using the existing entity.  It wouldn't add functionality, per se, but it'd add an overload consistent with a lot of other DALs without adding much overhead.
    • Post Points: 5
Page 1 of 1 (3 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems