Print Page | Close Window

CompositionContext.Fake and Login

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2586
Printed Date: 02-Jun-2025 at 12:04pm


Topic: CompositionContext.Fake and Login
Posted By: robert.hui
Subject: CompositionContext.Fake and Login
Date Posted: 25-Mar-2011 at 3:39pm
I have a question about how to use the CompositionContext.Fake. I read over the DRC documentation on faking: http://drc.ideablade.com/xwiki/bin/view/Documentation/discovery-compositioncontext-fake, but it only covered a simple case where no Login credentials were required.

I'm trying to set up a fake context for use in unit tests, by inflating an in-memory database to run some of our queries. I'm running into a chicken and egg problem with our login process, though.

By default, the first login to the BOS is a LoginCredential. This allows the user to read back a list of users (in our UI, this let's us show a dropdown). All other access is locked down until a user logs in with credentials. My question is, how do I inject fake user data into the system *before* log in, so that I can authenticate against credentials in the DB?

Is this even possible (via a RestoreCacheState, for example)?

Thanks,
Rob



Replies:
Posted By: DenisK
Date Posted: 28-Mar-2011 at 1:39pm
Hi robert.hui;

If I understand your question correctly, I believe you can accomplish what you want with IEntityServerFakeBackingStore.RestoreAsync operation.

The idea is to populate a real EntityManager's cache with some Users data and allow a fake EntityManager to import that cache to its own cache thus populating the fake EntityManager with some data.

Please see code snippets below.

    public void PopulateFakeEmWithUsersBeforeLogin() {
      var fakeEm = new NorthwindIBEntityManager(compositionContextName: CompositionContext.Fake.Name); 
      var coop = Coroutine.Start( () => PopulateWithUsersCore(fakeEm) );
      coop.Completed += (s, args) => {
        var isSuccessful = args.CompletedSuccessfully;
        var error = args.Error;
        fakeEm.Users.ExecuteAsync(op => {
          var isSuccessful2 = op.CompletedSuccessfully;
          var error2 = op.Error;
          var list = new List<User>(op.Results);
        });
      };
    }

    private IEnumerable<INotifyCompleted> PopulateWithUsersCore(NorthwindIBEntityManager fakeEm) {
      var realEm = new NorthwindIBEntityManager();
      var fakeBackingStore = fakeEm.CompositionContext.GetFakeBackingStore();

      //Populate realEm cache with Users
      var query = realEm.Users;
      var queryOp = query.ExecuteAsync();
      queryOp.Completed += (s, args) => {
        var isSuccessful = args.CompletedSuccessfully;
        var error = args.Error;
        var users = new List<User>(args.Results);
      };
      yield return queryOp;

      //Populate fakeEm with realEM cache
      var realEmCacheState = realEm.CacheStateManager.GetCacheState();
      var restoreOp = fakeBackingStore.RestoreAsync(realEmCacheState);
      restoreOp.Completed += (s, args) => {
        var isSuccessful = args.CompletedSuccessfully;
        var error = args.Error;
      };
      yield return restoreOp;
    }


Posted By: robert.hui
Date Posted: 06-Apr-2011 at 2:37pm
Dennis,

Thanks for the help on that. Something very similar to that appears to have worked on my end.



Print Page | Close Window