Differences from Massive
- Porting Code from Massive to Mighty
- DataTable support
- Table meta-data
- Validation
- CRUD
- Paging
- Dynamic Methods
- Runtime Exceptions
Porting Code from Massive to Mighty
A lot of code written against Massive will just work with Mighty, despite all the new features. Just change
using Massive
tousing Mighty
andDynamicModel
toMightyOrm
.
Here is a list of those items which won’t compile directly against former Massive code. In all cases, the same (sort of) thing is still supported, with some minor code changes on your part; and whilst this might look like quite a large list, many of these are actually ‘edge cases’ - they’re not the core features, and you quite possibly aren’t even using them.
DataTable support
- DataTable is no longer directly supported (not even on .NET Framework). However if you need it, the (short) Massive source code for .ToDataTable() is here; it is open source, under the same license as this project
Table meta-data
Schema
is now calledTableMetaData
(the word ‘schema’ for table meta data was potentially misleading on those databases where a table’s schema already means something like ‘owner’, or ‘namespace’)DefaultValue(columnName)
is now calledGetColumnDefault(columnName)
Prototype
is replaced byNew()
CreateFrom(collection)
is replaced byNewFrom(obj)
(whereobj
can be aNameValueCollection
as before, but now can also be any other sensible way of specifying a name-value set)
Validation
- Automatic pre-validation before CRUD actions (save, insert, update, delete) is off by default in Mighty, set
PrevalidationType = Prevalidation.Full
(orPrevalidation.Lazy
) on your validator class to enable it - All validation is now done via overridable methods in the
Mighty.Validation.Validator
class, not in theMightyOrm
class itself. If you need validation you should create your ownValidator
subclass, overriding the methods which you need, and pass an instance of it to the constructor ofMightyOrm
IsValid(item)
now returns aList<object>
of errors (this is intentionally aList
, not anIList
). Each object is one error; the error objects are typically strings, but you get to decide this in your validation class. Sodb.IsValid(item).Errors.Count == 0
checks for no errors (andIsValid
can return the reported errors immediately, without storing them in a shared variable which might potentially be overwritten by other calls to the same instance of Mighty)- As in Massive, you may well not be calling
IsValid(item)
directly anyway, since validation is called automatically during CRUD operations
- As in Massive, you may well not be calling
CRUD
SaveAsNew(items)
is no longer required, useInsert(items)
instead; other uses ofInsert(item)
still work as beforeUpdate(item, key)
is replaced byUpdate(item)
where item contains the key; your instance ofMightyOrm
already knows which field the key is inUpdate(item, where, args)
is replaced byUpdateUsing(partialItem, where, args)
- Amongst other things the above two changes allow the new
Update(item1, item2, ...)
to update multiple items, each containing its own key
- Amongst other things the above two changes allow the new
Delete(null, where, args)
is replaced byDelete(where, args)
; note thatDelete(pk)
still works as before andDelete(item)
(where item must contain the PK value) is newly available
Paging
- The version of
Paged(string sql, ...)
for use with arbitrary SQL has been replaced byPagedFromSelect(columns, tablesAndJoins, where, orderBy, ...)
which is similar but allows correct handling of paged queries with arbitrary joins and qualified column names if you need them; note that the version ofPaged(...)
for use with the default table still works as before - Paged result sets on SQL Server now include an additional
RowNumber
column even if explicit columns are specified (this extra column already appeared if all columns were selected with"*"
, though it was calledRow
before) - Paged result sets on Oracle now have
RowNumber
instead ofr___
as the additional row number column name (the additional column always appears, as it did before)
Dynamic Methods
Massive had various useful, but effectively rather well-hidden methods, which were supported only dynamically, i.e. the named methods and arguments didn’t ‘really’ exist, but were all processed by a dynamic method handler.
These methods were useful, but having dynamic methods adds a slight overhead to every call to Massive (even if it’s not stored in a dynamic variable), and more importantly there is no Intellisense at all (so the fact that the methods exist is invisible, and there is no information about what the methods and their arguments do). So, although early versions of Mighty did support these methods, they have now all been turned into normal instance methods.
Perhaps the most useful were the Find
/Get
/Single
synonyms. In Massive you could do:
var film = films.Find(film_id: 42);
var film = films.Single(film_id: 42);
var employee = employees.Find(surname: "Smith", departmentid: 1234);
In Mighty, to get an item by primary key, you can just do:
var film = films.Single(42);
And to get an item by a named column, or columns, you can do:
var film = films.Single(new { film_id = 42});
var employee = employees.Single(new { surname = "Smith", departmentid = 1234 });
As you can see, the syntax is pretty close and the same features are available. And yes, the same method Single
works for both of the above; if passed an object with values only (including a primitive type, or list of primitive types), it uses these as the primary key value (or values). If passed an object which has names and values (including, but not limited to, anonymous objects as used above), then it uses these as a WHERE
specification.
All of the aggregate functions (Max
, Min
, Sum
, etc.) which were previously available as dynamic methods in Massive are also now full, intellisense-able methods in Mighty.
Runtime Exceptions
The only one of the above changes which will compile against MightyOrm
when stored in a non-dynamic variable, even if you don’t make the mentioned changes, is Update(item, key)
. This will be misinterpreted initially as an attempt to update two different items, but running it will give you a helpful runtime exception: System.InvalidOperationException : Value-only collections not supported for action Update; use Update(item), not Update(item, pk)
.
You will also get a runtime exception (RuntimeBinderException
) rather than compile-time errors if you call any no-longer-supported Massive methods against MightyOrm
stored in a dynamic variable (dynamic db = new MightyOrm(...)
). To avoid this, and since there’s no other reason to store MightyOrm
itself (as opposed to its results!) in a dynamic variable any more, we recommend changing your dynamically stored instances of DynamicModel
(e.g. dynamic db = new DynamicModel(...)
) if any, to non-dynamically stored instances of MightyOrm
(e.g. either var db = new MightyOrm(...)
or MightyOrm db = new MightyOrm(...)
).