-
Notifications
You must be signed in to change notification settings - Fork 1
BASE.data.DataContext
The DataContext helps developers with handling changes to models. Think of the DataContext as a transaction handler. If you have the need to query for data and modify some of the data and the relationships among that model, then the DataContext is your answer. It handles relationships for you, so you don't have to think about updating foreign keys, or primary keys from a service. The DataContext wants you to think about data as it should be, just data, not so much about the storage structure for persisting the data.
The DataContext uses many of the other technologies to handle complexity. It uses Queryables for querying your data store, Futures for asynchrony, and Observables for knowing whether or not properties have changed on a instance of a model.
Here is a list of the other technologies that you need to understand while using the DataContext.
The DataContext should be used when wanting to make changes to a specific model and its associated models. This usually means short lived instances of the DataContext. However there are exceptions. So that cannot be the rule all principle. A better question to ask yourself is, am I using the DataContext for managing 100's of objects, that are unrelated? If that is the case you should think about using many instances of the DataContext and saving smaller chunks of changes. A DataContext holds everything in memory that it brings from the service. If you keep querying off the DataContext you will eventually have the whole database in memory. That's usually not our intention. I like to think of Forms as a place for an instance of the DataContext. For example where you add, or edit any data is where you should use it. And it should look something like this.
//... var edm = new Edm(); var service = new Service(edm); //... saveButton.addEventListener("click", function(){ var firstName = firstNameInput.value; var lastName = lastNameInput.value; var id = idInput.value; var dataContext = new BASE.data.DataContext(service); dataContext.people.firstOrDefault(function(expBuilder){ return expBuilder.property("id").isEqualTo(id); }).then(function(person){ person.firstName = firstName; person.lastName = lastName; dataContext.saveChangesAsync().then(function(response){ console.log("Saved Data!"); }); }); }); //...