SubSonic 3.0 Repository Template Update
Got a fun email from Will Gant today:
Since everything else is going my way (sorta like Burger king), now comes the “can I have fries with that?” question. Would it be possible for the Add, Delete, and Update methods on the repository class to have overloads that take IEnumerable<T>?
And I sort of started to kick myself a bit – this should have seemed obvious, and I should have done it from the start, but I didn’t and I’m not too sure why. This is the goodness that is Open Source!
Will sent me a quick patch, and I added some spice to it and now we have a batch-updatable (read: one connection, one sql execution) Add/Update/Delete for the Repository which you can download here (I’ll include this in subsequent releases).
Hopefully this underscores a bit more just how much control you have in this!
To show you how this might work, here’s a quick spike using the Chinook database (our new test db):
[TestMethod]
public void Update_Should_Update_En_Masse() {
ChinookRepository<Customer> repo = new ChinookRepository<Customer>();
var customers = repo.GetAll().ToList();
int index=0;
foreach (var c in customers) {
c.Company = "Company" + index.ToString();
index++;
}
repo.Update(customers);
}
Just to re-iterate, the Update() method here takes in the List<Customer> that is created from the repo.GetAll().ToList() call. This will not work if you leave it as IQueryable. I’m not sure why – but the bottom line is that I tried and the changes weren’t honored as I think IQueryable doesn’t allow you to assign values to it – it’s just an Expression manager.
The Update method will create one single SQL query and execute it as a batch – one call! I was going to wrap the ExecuteBatch() method in a transaction but decided against it for now – however if you want to do that, you can edit the templates as you need
.







