New Posts New Posts RSS Feed: Fake Composition Context Troubles
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Fake Composition Context Troubles

 Post Reply Post Reply
Author
tersiusk View Drop Down
Newbie
Newbie
Avatar

Joined: 03-Nov-2010
Posts: 20
Post Options Post Options   Quote tersiusk Quote  Post ReplyReply Direct Link To This Post Topic: Fake Composition Context Troubles
    Posted: 22-Nov-2010 at 2:22am
Hi

I'm setting up a fake composition context to do some testing. Now I have a method that adds dummy entities to this entitymanager on the server side. This all works fine. The problem comes in on the client side. While all my entities are in the client entitymanager the relationships between them seem to disappear.  Say for example I have a User entity and a Application entity linked with a many-to-many relationship. On the serverside user.Applications returns results but on the client side the user.Applications has no results. Keep in mind that all the entities are there, they are just not linked.

Any idea what is going wrong? Must I add the mock entities on the client side?

Edit1:
I have come across some more issues. For now I'm adding the mock entities on the client side so that the relationships stick. Now I'm having another issue to do with the AsScalarAsync().Count() method. I have a query that definitely has results but when I use AsScalarAsync().Count the result is zero. If I do a normal ExecuteAsync the results are returned as expected. This only happens when I'm using your provided fake composition context. When I pull data from the db it all works again.
query.AsScalarAsync().Count(r =>
{
  var count = r.Result; // Value is zero if one is using a fake comp context.
 });

query.ExecuteAsync(r =>
{
var count = r.Results.Count(); // Returns the correct count with the fake comp context.
 });


Edited by tersiusk - 23-Nov-2010 at 12:05am
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 24-Nov-2010 at 4:31pm
tersiusk;

A few questions:

1. How are you populating the EntityManager on the server side? Are you populating it by using a remote server method and returning an EntityCacheState? Or are you using EntityServerFakeBackingStore.Remote?

2. What version of DF2010 are you using? 

We have made a few improvements to the CompositionContext since the release of 6.0.6 and might have fixed the bug that you're experiencing. I am currently using the latest internal release of DF and could not duplicate the bug you're seeing. The AsScalarAsync operation was fine as well. To populate my fake EM, I called a remote server method, create and populate a fake EM, return its EntityCacheState and restore it on the client. Perhaps, our setup is different. If you can answer my questions above, I can better attempt to duplicate your problem.
Back to Top
tersiusk View Drop Down
Newbie
Newbie
Avatar

Joined: 03-Nov-2010
Posts: 20
Post Options Post Options   Quote tersiusk Quote  Post ReplyReply Direct Link To This Post Posted: 24-Nov-2010 at 11:50pm
Hi DenisK

To answer your questions.

1. Currently I'm not populating the EM from the server since that did not seem to work. At the moment I populate it on the client side after a Em.Login(). When I tried the server side, I simply populated the EM right after authentication so the returning em already had the entities. Unfortunately this did not work properly as I mentioned the client EM did have all the entities but they were not linked at all. I can have a look at fetching the cache and loading that into the client EM but it still does not make sense why the client EM would have the entities but they are not linked as they were on the server side.

2. I'm using 6.0.6(The latest download). I have included a sample application and below is the code for it. It seems that adding a where clause with an Any causes the async call to return zero results. The normal executeasync works. This almost feels as if the user and application loses it's link and then there are no applications to satisfy the condition.(Back to problem 1).

var entitymanager = new DemoDBEntities(compositionContextName: CompositionContext.Fake.Name);

Application app = new Application();
app.Name = "App1";
app.Description = "description";
app.ApplicationID = Guid.NewGuid();

User user = new User();
user.FirstName = "User 1";
user.UserID = Guid.NewGuid();
user.Applications.Add(app);

entitymanager.AddEntity(user);
entitymanager.SaveChanges();

//Adding a where clause causes the as scalar to return the wrong results. 
var userQuery = entitymanager.Users.Where(u => u.Applications.Any(a => a.Name == "App1"));

userQuery.AsScalarAsync().Count(r =>
{
    var count = r.Result; // Value is zero if one is using a fake comp context.
    Console.Write("AsScalarAsync count : " + count);
    Console.WriteLine();
});

userQuery.ExecuteAsync(r =>
{
    var count = r.Results.Count(); // Returns the correct count with the fake comp context.
    Console.Write("ExecuteAsync count : " + count);
    Console.WriteLine();
});

Console.ReadLine();

Sample application with db sql script: uploads/988/SampleAsScalarAsync.rar
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 29-Nov-2010 at 3:24pm
Hi tersiusk;

The AsScalarAsync operation seems to be a bug. I have filed a bug report so the senior developer could take a look at it.

Regarding #1, if you can post your sample here showing how you populate your fake entity manager on the server, I can analyze it as well. 
Back to Top
tersiusk View Drop Down
Newbie
Newbie
Avatar

Joined: 03-Nov-2010
Posts: 20
Post Options Post Options   Quote tersiusk Quote  Post ReplyReply Direct Link To This Post Posted: 30-Nov-2010 at 3:13am
Hi DenisK

Here is a sample app for the first problem. As you can see I'm populating the entities in the logon method. This might not be the correct approach so feel free to post some code that will get the desired results. But also the behavior seems odd since the client enititymanager does have the entities but they are just not linked.


Edited by tersiusk - 30-Nov-2010 at 3:14am
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 30-Nov-2010 at 3:06pm
Hi tersiusk;

After further investigation, it looks like this is a bug although not quite a high priority one. The reason being is that the Login method is not really the place to modify your entities (querying is fine) so we're thinking of blocking the SaveChanges call within the LoginManager.

One does not need to go to the server side to populate the fake EM. For example, it can be done the following way on the client.

var em = new DomainModelEntityManager(compositionContextName:
  CompositionContext.Fake.Name);
PopulateBackingStore(em);
em.Clear(); // clear cache; no memory of prior entities
var q = em.Customers.Include(c => c.OrderSummaries)
  .Where(c => c.OrderSummaries.Any(
    os => os.Employee.FirstName == "Fred"));

private void PopulateFakeBackingStore(EntityManager em) {
  var customer = new Customer("TestCompany");
  em.AddEntity(customer);
  var employee = new Employee("Fred", "Smith");
  em.AddEntity(employee);
for (int i = 0; i < 5; i++) {
    var salesOrder = new OrderSummary(customer, employee);
    em.AddEntity(salesOrder);
  }
  em.SaveChanges();
}

I hope this helps.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 30-Nov-2010 at 3:07pm
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 30-Nov-2010 at 3:19pm
The AsScalarAsync bug has been fixed as well. It will be available in our next release of 6.0.7 or if you wish to have it earlier, I can send you an EAP.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down