I've got a basic MVC application - generated from the MVC application template. I've created a basic controller which contains the usual Index, Details, Edit and Create methods; I've got a couple of classes one to hold my object called Person:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Description { get; set; }
}
And another to mock up a fake DAL. It doesn't persist the data to file or a database, it's just a quick mock-up for the purpose of this prototype:
public class PersonData
{
/* Fake out our initial repository */
static List<Person> People = new List<Person>()
{
new Person() { Id=1, FirstName="John", LastName="Doe", Description="King of the world" },
new Person() { Id=2, FirstName="Jane", LastName="Doe", Description="Chief whip cracker" }
};
/* Set up the indexing system */
private static int GetNextId()
{
return People.OrderByDescending(i => i.Id).First().Id + 1;
}
public static IEnumerable<Person> GetPeople()
{
return People;
}
public static Person GetPerson(int id)
{
if (id > People.OrderByDescending(i => i.Id).First().Id)
throw new ArgumentOutOfRangeException();
return People.Where(i => i.Id.Equals(id)).SingleOrDefault();
}
public static void Save开发者_如何学Go(Person data)
{
/* Either remove the old one or assign a new ID */
if (data.Id != 0)
People.Remove(People.Where(i => i.Id.Equals(data.Id)).SingleOrDefault());
else
data.Id = GetNextId();
People.Add(data);
}
}
There's nothing particularly interesting about the controller either, here's the excerpt of the Edit functionality:
// GET: /Person/Edit/5
public ActionResult Edit(int id)
{
Person person = PersonData.GetPerson(id);
return View(person);
}
//
// POST: /Person/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Person person)
{
try
{
PersonData.Save(person);
return RedirectToAction("Details", person);
}
catch
{
return View(person);
}
}
Everything functions, but I've noticed a couple of curious anomalies that I can't explain. First up, if I make a change to a person in my edit form and save it, everything is reflected in the details page just fine. Then I cue up another browser instance - Opera/Chrome/Whatever and enter the details page for that person. I was expecting to see that in the second browser, I would see the data previous to my changes because the PersonData is only loaded in memory and it's not persisted - however, I see the changes that were made in browser 1 in the second browser.
I chalked this up to being because I'm running in Visual Studio's Development Server and assumed that I was just accessing the same session. To prove the hypothesis I added the Session ID to the details page expecting to see the same session ID, but I was wrong. Each session has a different ID.
Further to this - the session ID changes with every page refresh in both browsers, and they're always different from each other.
I don't understand why this is and wonder if someone could explain:
- Why is the data from my DAL mock persisting across both browsers when they're both on different sessions?
- Why is the session id not being maintained across page refreshes?
Thanks in advance
Your fake repository is declared as a static:
/* Fake out our initial repository */
static List<Person> People = new List<Person>()
{
new Person() { Id=1, FirstName="John", LastName="Doe", Description="King of the world" },
new Person() { Id=2, FirstName="Jane", LastName="Doe", Description="Chief whip cracker" }
};
Statics exist for the life of the process, it is not tied to an ASP.NET session. Therefore all your sessions will see the data until the web server process is restarted (in your case the VS development web server process).
Are you using static objects? If so, they are shared across each instance of the run-time.
Maybe you could share the implementation of your "DAL mock"? One cant be sure, but it seems a bit like your mock is a bit flawed, did you check that first?
精彩评论