In the first article, I explained the reasons for making smart queries and the basic solution design. Starting from this article, we will explain its implementation process.
Actually writing this seriesArticleAt the beginning, I wanted to explain it from bottom to bottom, But I sorted it out again.CodeIn fact, if you do not know the most superficial things, it is not very in-depth.
So in our second article, we will talk about the shortest but most frequently used smart query framework, that is, model.
First, our entity or database structure is as follows:
In addition, as shown in the following code, we have a model for passing the name = value pair and the query predicate.
1: PublicActionresult index (querymodel Model)
2:{
3:Using(Var db =NewDbentities ())
4:{
5:VaR list = dB. Users. Where (model). tolist ();
6:ReturnView (list );
7:}
8:}
I name it querymodel. It is passed in by the Action parameter, and then the where extension method of EF. It is a prototype for building lambda expressions. The above is a common code for querying actions.
The querymodel code is
The unique items attribute of querymodel is a set of conditionitems, which contains all query conditions.
The properties in conditionitem
- Field indicates the name of the target attribute to be queried. We set it to support subattribute queries, such as "profile. myuser. ID"
- Method is the currently used predicate, which is an enumerated value of querymethod.
- Orgroup is a string. Multiple Expressions of an orgroup are associated with the OR operator before and
- Prefix is the prefix of a category. We assume that an action may process multiple query condition groups and add attributes to separate these query conditions.
- Value is the value of this expression.
On the page
1: <Form Action= "" Method= "Post">
2:Name:<Input ID= "Name" Name= "[Like] Name" Type= "Text" Value= "" />
3:Email:<Input ID= "Email" Name= "[Equal] email" Type= "Text" Value= "" /> <BR />
4:ID:<Input ID= "ID" Name= "[Equal] ID" Type= "Text" Value= "" />
5:Birthday:<Input ID= "Birthday" Name= "[Equal] Birthday" Type= "Text" Value= "" /> <BR />
6: <Input Type= "Submit" Value= "Query" />
7: </Form>
This form is submitted to the server. We need to convert imodelbinder in ASP. net mvc to convert the keyvaluepair in querystring or postdata to our conditionitem.
Of course, in addition to the prefix method, we also have the prefix and orgroup to be submitted from the client, so we need to develop a rule.
We agree
- In [], it is the query predicate.
- Prefix in parentheses ()
- Orgroup in braces {}
We can use the following imodelbinder implementation to convert the value in request. querystring or request. Form to querymodel:
1: Public ClassSearchmodelbinder: imodelbinder
2:{
3:Public ObjectBindmodel (controllercontext, modelbindingcontext bindingcontext)
4:{
5:VaR model = (querymodel) (bindingcontext. Model ??NewQuerymodel ());
6:VaR dict = controllercontext. httpcontext. Request. Params;
7:VaR keys = dict. allkeys. Where (C => C. startswith ("["));// We think that only the beginning of [must be processed
8:If(Keys. Count ()! = 0)
9:{
10:Foreach(Var keyInKeys)
11:{
12:If(! Key. startswith ("["))Continue;
13:Var val = dict [Key];
14:// Process the case of no value
15:If(String. Isnullorempty (VAL ))Continue;
16:Addsearchitem (model, key, Val );
17:}
18:}
19:ReturnModel;
20:}
21:
22:/// <Summary>
23:/// Add a set of key = value to querymodel. Items
24:/// </Summary>
25:/// <Param name = "model"> querymodel </param>
26:/// <Param name = "key"> htmlname of the current item </param>
27:/// <Param name = "Val"> current item value </param>
28:Public Static VoidAddsearchitem (querymodel model,StringKey,StringVal)
29:{
30:StringField ="", Prefix ="", Orgroup ="", Method ="";
31:VaR KEYWORDS = key. Split (']',')','}');
32:// Divide the name in HTML into the desired parts
33:Foreach(VAR keywordInKeywords)
34:{
35:If(Char. isletterordigit (keyword [0]) field = keyword;
36:VaR last = keyword. substring (1 );
37:If(Keyword [0] ='(') Prefix = last;
38:If(Keyword [0] ='[') Method = last;
39:If(Keyword [0] ='{') Orgroup = last;
40:}
41:If(String. Isnullorempty (method ))Return;
42:If(!String. Isnullorempty (field ))
43:{
44:VaR item =NewConditionitem
45:{
46:Field = field,
47:Value = Val. Trim (),
48:Prefix = prefix,
49:Orgroup = orgroup,
50:Method = (querymethod) enum. parse (Typeof(Querymethod), method)
51:};
52:Model. Items. Add (item );
53:}
54:}
55:}
Of course, we also need to add
1:Modelbinders. binders. Add (Typeof(Querymodel ),NewSearchmodelbinder ());
In this way, the corresponding query conditions can be obtained in the action.
We can see that the data submitted from the form is correctly stored in querymodel.