You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The "Find" method doesn't exist, but since Massive is dynamic it will try to infer what you mean by using DynamicObject's TryInvokeMember. See the source for more details. There's more on the dynamic query stuff down below.
53
62
54
63
You can also run ad-hoc queries as needed:
55
64
56
-
var result = tbl.Query("SELECT * FROM Categories");
65
+
```csharp
66
+
varresult=tbl.Query("SELECT * FROM Categories");
67
+
```
57
68
58
69
This will pull categories and enumerate the results - streaming them as opposed to bulk-fetching them (thanks to Jeroen Haegebaert for the code). If you need to run a Fetch - you can:
59
70
60
-
var result = tbl.Fetch("SELECT * FROM Categories");
71
+
```csharp
72
+
varresult=tbl.Fetch("SELECT * FROM Categories");
73
+
```
61
74
62
-
If you want to have a Paged result set - you can:
75
+
If you want to have a paged result set - you can:
63
76
64
-
var result = tbl.Paged(where: "UnitPrice > 20", currentPage:2, pageSize: 20);
In this example, ALL of the arguments are optional and default to reasonable values. CurrentPage defaults to 1, pageSize defaults to 20, where defaults to nothing.
67
82
68
-
What you get back is IEnumerable < ExpandoObject > - meaning that it's malleable and exciting. It will take the shape of whatever you return in your query, and it will have properties and so on. You can assign events to it, you can create delegates on the fly. You can give it chocolate, and it will kiss you.
83
+
What you get back is `IEnumerable<ExpandoObject>` - meaning that it's malleable and exciting. It will take the shape of whatever you return in your query, and it will have properties and so on. You can assign events to it, you can create delegates on the fly. You can give it chocolate, and it will kiss you.
69
84
70
85
That's pretty much it. One thing I really like is the groovy DSL that Massive uses - it looks just like SQL. If you want, you can use this DSL to query the database:
71
86
72
-
var table = new Products();
73
-
var productsThatILike = table.Query("SELECT ProductName, CategoryName FROM Products INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID WHERE CategoryID = @0",5);
74
-
//get down!
87
+
```csharp
88
+
vartable=newProducts();
89
+
varproductsThatILike=table.Query("SELECT ProductName, CategoryName FROM Products INNER JOIN Categories ON Categories.CategoryID = Products.CategoryID WHERE CategoryID = @0",5);
90
+
//get down!
91
+
```
75
92
76
93
Some of you might look at that and think it looks suspiciously like inline SQL. It *does* look sort of like it doesn't it! But I think it reads a bit better than Linq to SQL - it's a bit closer to the mark if you will.
77
94
@@ -81,76 +98,88 @@ Massive is built on top of dynamics - so if you send an object to a table, it wi
81
98
82
99
You can send just about anything into the MassiveTransmoQueryfier and it will magically get turned into SQL:
83
100
84
-
var table = new Products();
85
-
var poopy = new {ProductName = "Chicken Fingers"};
86
-
//update Product with ProductID = 12 to have a ProductName of "Chicken Fingers"
87
-
table.Update(poopy, 12);
101
+
```csharp
102
+
vartable=newProducts();
103
+
varpoopy=new {ProductName="Chicken Fingers"};
104
+
//update Product with ProductID = 12 to have a ProductName of "Chicken Fingers"
105
+
table.Update(poopy, 12);
106
+
```
88
107
89
108
This also works if you have a form on your web page with the name "ProductName" - then you submit it:
90
109
91
-
var table = new Products();
92
-
//update Product with ProductID = 12 to have a ProductName of whatever was submitted via the form
93
-
table.Update(poopy, Request.Form);
110
+
```csharp
111
+
vartable=newProducts();
112
+
//update Product with ProductID = 12 to have a ProductName of whatever was submitted via the form
113
+
table.Update(poopy, Request.Form);
114
+
```
94
115
95
116
Insert works the same way:
96
117
97
-
//pretend we have a class like Products but it's called Categories
98
-
var table = new Categories();
99
-
//do it up - the new ID will be returned from the query
100
-
var newID = table.Insert(new {CategoryName = "Buck Fify Stuff", Description = "Things I like"});
118
+
```csharp
119
+
//pretend we have a class like Products but it's called Categories
120
+
vartable=newCategories();
121
+
//do it up - the new ID will be returned from the query
122
+
varnewID=table.Insert(new {CategoryName="Buck Fify Stuff", Description="Things I like"});
123
+
```
101
124
102
125
Yippee Skippy! Now we get to the fun part - and one of the reasons I had to spend 150 more lines of code on something you probably won't care about. What happens when we send a whole bunch of goodies to the database at once!
103
126
104
-
var table = new Products();
105
-
//OH NO YOU DIDN't just pass in an integer inline without a parameter!
106
-
//I think I might have... yes
107
-
var drinks = table.All("WHERE CategoryID = 8");
108
-
//what we get back here is an IEnumerable < ExpandoObject > - we can go to town
109
-
foreach(var item in drinks){
110
-
//turn them into Haack Snacks
111
-
item.CategoryID = 12;
112
-
}
113
-
//Let's update these in bulk, in a transaction shall we?
114
-
table.Save(drinks);
127
+
```csharp
128
+
vartable=newProducts();
129
+
//OH NO YOU DIDN't just pass in an integer inline without a parameter!
130
+
//I think I might have... yes
131
+
vardrinks=table.All("WHERE CategoryID = 8");
132
+
//what we get back here is an IEnumerable < ExpandoObject > - we can go to town
133
+
foreach(varitemindrinks){
134
+
//turn them into Haack Snacks
135
+
item.CategoryID=12;
136
+
}
137
+
//Let's update these in bulk, in a transaction shall we?
138
+
table.Save(drinks);
139
+
```
115
140
116
141
Named Argument Query Syntax
117
142
-------------------
118
143
I recently added the ability to run more friendly queries using Named Arguments and C#4's Method-on-the-fly syntax. Originally this was trying to be like ActiveRecord, but I figured "C# is NOT Ruby, and Named Arguments can be a lot more clear". In addition, Mark Rendle's Simple.Data is already doing this so ... why duplicate things?
119
144
120
145
If your needs are more complicated - I would suggest just passing in your own SQL with Query().
121
146
122
-
//important - must be dynamic
123
-
dynamic table = new Products();
147
+
```csharp
148
+
//important - must be dynamic
149
+
dynamictable=newProducts();
124
150
125
-
var drinks = table.FindBy(CategoryID:8);
126
-
//what we get back here is an IEnumerable < ExpandoObject > - we can go to town
127
-
foreach(var item in drinks){
128
-
Console.WriteLine(item.ProductName);
129
-
}
130
-
//returns the first item in the DB for category 8
131
-
var first = table.First(CategoryID:8);
132
-
133
-
//you dig it - the last as sorted by PK
134
-
var last = table.Last(CategoryID:8);
135
-
136
-
//you can order by whatever you like
137
-
var firstButReallyLast = table.First(CategoryID:8,OrderBy:"PK DESC");
138
-
139
-
//only want one column?
140
-
var price = table.First(CategoryID:8,Columns:"UnitPrice").UnitPrice;
141
-
142
-
//Multiple Criteria?
143
-
var items = table.Find(CategoryID:5, UnitPrice:100, OrderBy:"UnitPrice DESC");
151
+
vardrinks=table.FindBy(CategoryID:8);
152
+
//what we get back here is an IEnumerable < ExpandoObject > - we can go to town
You can do the same thing as above for aggregates:
148
175
149
-
var sum = table.Sum(columns:Price, CategoryID:5);
150
-
var avg = table.Avg(columns:Price, CategoryID:3);
151
-
var min = table.Min(columns:ID);
152
-
var max = table.Max(columns:CreatedOn);
153
-
var count = table.Count();
176
+
```csharp
177
+
varsum=table.Sum(columns:Price, CategoryID:5);
178
+
varavg=table.Avg(columns:Price, CategoryID:3);
179
+
varmin=table.Min(columns:ID);
180
+
varmax=table.Max(columns:CreatedOn);
181
+
varcount=table.Count();
182
+
```
154
183
155
184
Metadata
156
185
--------
@@ -161,46 +190,55 @@ In addition, if you want to generate an empty instance of a column - you can now
161
190
Factory Constructor
162
191
-------------------
163
192
One thing that can be useful is to use Massive to just run a quick query. You can do that now by using "Open()" which is a static builder on DynamicModel:
164
-
var db = Massive.DynamicModel.Open("myConnectionStringName");
One thing that's always needed when working with data is the ability to stop execution if something isn't right. Massive now has Validations, which are built with the Rails approach in mind:
171
203
172
-
public class Productions:DynamicModel {
173
-
public Productions():base("MyConnectionString","Productions","ID") {}
The idea here is that Validate() is called prior to Insert/Update. If it fails, an Error collection is populated and an InvalidOperationException is thrown. That simple. With each of the validations above, a message can be passed in.
217
+
The idea here is that `Validate()` is called prior to Insert/Update. If it fails, an Error collection is populated and an InvalidOperationException is thrown. That simple. With each of the validations above, a message can be passed in.
184
218
185
219
CallBacks
186
220
---------
187
-
Need something to happen After Update/Insert/Delete? Need to halt BeforeSave? Massive has callbacks to let you do just that:
188
-
189
-
public class Customers:DynamicModel {
190
-
public Customers():base("MyConnectionString","Customers","ID") {}
191
-
192
-
//Add the person to Highrise CRM when they're added to the system...
193
-
public override void Inserted(dynamic item) {
194
-
//send them to Highrise
195
-
var svc = new HighRiseApi();
196
-
svc.AddPerson(...);
197
-
}
221
+
Need something to happen after Update/Insert/Delete? Need to halt before save? Massive has callbacks to let you do just that:
0 commit comments