Building a Chat Web App With Signal R, Part 2: Listing 1

The PersonHub Add() method.

public bool Add(Person newPerson)
{
    bool result = false;

    try
    {
        using (var context = new PersonContext())
        {
            var person = context.People.Create();
            person.FirstName = newPerson.FirstName;
            person.LastName = newPerson.LastName;
            person.Email = newPerson.Email;
            context.People.Add(person);
            context.SaveChanges();
            Clients.All.personCreated(person);
            result = true;
        }
    }
    catch (Exception)
    {
        Clients.Caller.raiseError("Unable to create a new Person.");
    }

    return result;
}

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at vogelvision@gmail.com.