开发者

WCF RIA Service Agent pattern with complex queries

I'm looking for best practices regarding abstracting my RIA domain context away from my view models to make them data source agnostic. It seems like the best solution I can find is the service agent pattern as described here. However, what if I have some relatively complex query logic to perform?

For example, currently I have my domain contexts in my view models. Let's say I have a ContactSearchViewModel, where there is a bit of logic involved in constructing the search query:

protected EntityQuery<Contact> CreateSearchQuery()
    {
        var query = Context.GetContactsQuery().Where(
                        e => e.First_Name.ToLower().StartsWith(FirstNameSearch.ToLower()) &&
                        e.Last_Name.ToLower().StartsWith(LastNameSearch.ToLower()));
        if (!string.IsNullOrEmpty(Phone))
        {
            query = query.Where(q => q.Phone == Phone ||
                                     q.Mobile == Phone ||
                                     q.Work == Phone);
        }
        if (SelectedContactType == "Prospect")
        {
            query = query.Where(q => q.Contact_Type_Id == 1);
        }
        else if (SelectedContactType == "Customer")
        {
            query = query.Where(q => q.Contact_Type_Id == 2);
        }
        return query;
    }

Now I could of course have a service agent method with a signature that looks something like public EntityList<Contact> SearchContacts(string firstName, string lastName, string phone, ContactType contactType) but imagine if my search queries were even more complex - this interface would become unwieldy. Is there a better alternative, some way I could construct the query in the VM like I do with the domain context? Or should I just suck it up and use some parameter objects?

I would very much prefer to be able to construct the queries on the VM, because I curre开发者_如何学JAVAntly have a small hierarchy of VMs which all perform various searches using a base search class and template method for generating queries, kind of like

WCF RIA Service Agent pattern with complex queries

Not being able to generate queries from the client would cause a lot of code duplication that I fixed using this inheritance structure.


I would suggest looking into the Repository pattern. It's part of DDD, but has application in any architecture, IMO. It's purpose is to house query logic.

It seems dirty to give your VM objects query logic, unless that is their single purpose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜