This project has moved and is read-only. For the latest updates, please go here.


All objects managed by AgFx must have an associated "data loader" that implmements IDataLoader<T>, where T is a LoadContext type. A data loader has two main jobs:

  1. Building a LoadRequest to be used for fetching a new model value
  2. Deserializing the data fetched by the LoadRequest.

Specifying a DataLoader

Data loaders can be associated with an object in one of two ways.

First, the default way is to make the loader a nested public type of the model object itself. AgFx will take the first such type it finds that matches this pattern:


  public class TestContextObject : ModelItemBase<TestLoadContext> {

            public TestContextObject() {


            public class TestContextDataLoader : IDataLoader<TestLoadContext> {
                public LoadRequest GetLoadRequest(TestLoadContext loadContext){

                public object Deserialize(TestLoadContext loadContext, Type objectType, Stream stream) {                
                    var tco = new TestContextObject();
                    tco.LoadContext = loadContext;
                    return tco;




Second, the model item can have a DataLoaderAttribute on it to specify a loader implemented in another object.


public class MyModel : ModelItemBase {
   public string Value { get; set; }

Which should you choose? The first way should be the 90% case, if not more. It allows keeping your loading and request logic in the same place as the model. If, for some reason, you need to separate the two into different assemblies or code areas, then you can use the DataLoaderAttribute tie the two together.



IDataOptimizer allows you to customize the data that gets written into the cache store. The common case here is for performance. Many web service requests return a large amount of JSON or XML, when your app only needs a small amount of it. To use IDataOptimizer, implement the IDataOptimizer interface on your data loader implementation.


public class UserVmLoader : IDataLoader<MyLoadContext>, IDataOptimizer {


Here's a sample implementation that just uses a simple StreamReader to read/write the cached data. Not only is this simple, it's much faster because it avoids creating the serializer, doing reflection, etc.



  public object DeserializeOptimizedData(LoadContext context, Type objectType, System.IO.Stream stream) {
            StreamReader sr = new StreamReader(stream);

            string id = sr.ReadLine();

            UserVm vm = new UserVm(id);

            vm.FullName = sr.ReadLine();
            vm.UserName = sr.ReadLine();
            vm.ProfileIconUrl = new Uri(sr.ReadLine());
            return vm;


        public bool SerializeOptimizedData(object value, System.IO.Stream outputStream) {
            StreamWriter sw = new StreamWriter(outputStream);
            UserVm vm = (UserVm)value;
            return true;



Last edited Mar 31, 2011 at 1:36 AM by shawnburke, version 5


No comments yet.