Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Monday, 08 August 2011

image

EF Code first makes the DB.

Expose DB with a WCF Service which creates an OData feed.

image

| | # 

odata.codeplex.com

2 way data binding.

The Full Stack 8 – Client Search

The Full Stack 9 – Silverlight Unit Tests, MVVM Lite

10 – MVVM

13 – Sterling server db.. offline.

| | # 
# Sunday, 07 August 2011

problem – using my version of EFCodeFirst this isn’t compiling.

Code Snippet
        public void MarkAsModified(object entity) {
            base.ObjectContext.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
        }

Here is the new working CTP5 version of the code.

Code Snippet
public int Save(Person person) {
            //if (person.PersonID == 0) {
            //    db.People.Add(person);
            //}
            //else {
            //    //var personRetrived = db.People.Find(person.PersonID);
            //    db.People.Attach(person);
            //    //db.MarkAsModified(person); // chaged with EF Code First CTP5.
            //}

            db.Entry(person).State = person.PersonID == 0 ?
                EntityState.Added :
                EntityState.Modified;
            
            db.SaveChanges();
            return person.PersonID;
        }

image

Automatically creating unit tests from our controller.

Constructor Injection

Code Snippet
public class PersonController : Controller {
        // constructor injection
        IPersonRepository repo;
        public PersonController(IPersonRepository repository) {
            repo = repository;
        }

        public PersonController() {
            repo = new PersonRepository();
        }


        public ActionResult Index() {
            var people = repo.GetAll();
            return View(people);
        }

Installing ninject.mvc3

Code Snippet
private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IPersonRepository>().To<PersonRepository>(); // DM put this in
        }   

then in controller don’t need the parameterless contructor:

Code Snippet
public class PersonController : Controller {
        // constructor injection
        IPersonRepository repo;
        public PersonController(IPersonRepository repository) {
            repo = repository;
        }
| | # 

MS Test

Should library and ShouldFluent

install-package Should

uninstall-package Should

image

Fake – repository is a working implementation but takes a shortcut..and not used in production.

not a Mock, Stub or .. (see Martin Fowler). http://martinfowler.com/articles/mocksArentStubs.html

testm – snippet to make a test class.

http://should.codeplex.com/

 

First test of CRUD is Create

Using a dictionary in fake repository to keep an in memory ‘database’

Code Snippet
[TestClass]
    public class PersonRepositoryTest {
        FakePersonRepository repository;

        [TestInitialize]
        public void Setup() {
            repository = new FakePersonRepository();
        }
        
        [TestCleanup]
        public void CleanUp() {
            repository = null;
        }

        // Create
        [TestMethod]
        public void Can_Add_New_Contact_And_Get_ID() {
            int id = repository.Add(new Person { Name = "George Washington" });
            bool result = repository.Exists(id);
            result.ShouldBeTrue("PersonID did not exist");
            id.ShouldEqual(1, "PersonID not correct");
        }

        // Read
        [TestMethod]
        public void Can_Retrieve_Contact_By_ID() {
            Person person = CreateAndRetrievePerson("Bill Gates");
            person.Name.ShouldEqual("Bill Gates");
        }

        private Person CreateAndRetrievePerson(string name) {
            int id = repository.Add(new Person { Name = name});
            Person person = repository.Retrieve(id);
            return person;
        }

        // Update
        [TestMethod]
        public void Can_Update_Person() {
            var person = CreateAndRetrievePerson("John Adams");
            person.Name = "Bob Jones";
            int id = person.PersonID;

            repository.Update(id, person);

            var personRetrieved = repository.Retrieve(id);
            personRetrieved.Name.ShouldEqual("Bob Jones");
        }

        [TestMethod]
        public void Can_Update_Correct_Person() {
            var person = CreateAndRetrievePerson("George Washington");
            var person2 = CreateAndRetrievePerson("John Adams");

            int id = person.PersonID;
            int id2 = person2.PersonID;
            var updatedPerson = repository.Retrieve(id);

            person.Name = "Thomas Jefferson";

            repository.Update(id, person);

            var retrievedPerson = repository.Retrieve(id);
            retrievedPerson.Name.ShouldEqual("Thomas Jefferson", "Update Failed");

            var retrievedPerson2 = repository.Retrieve(id2);
            retrievedPerson2.Name.ShouldEqual("John Adams", "Updated Wrong Person");
        }

        [TestMethod]
        public void Can_Delete_Person() {
            var person = CreateAndRetrievePerson("George Washington");
            int id = person.PersonID;
            repository.Delete(id);
            repository.Exists(id).ShouldBeFalse();
        }

Code Snippet
class FakePersonRepository : IPersonRepository {

        Dictionary<int, Person> contacts = new Dictionary<int, Person>();

        public int Add(Models.Person person) {
            int id = contacts.Count + 1;
            person.PersonID = id;
            contacts.Add(id, person);
            return id;
        }

        public bool Exists(int id) {
            return contacts.ContainsKey(id);
        }

        public Person Retrieve(int id) {
            return contacts[id];
        }

        public void Update(int id, Person person) {
            contacts[id].Name = person.Name;
        }

        public void Delete(int id) {
            contacts.Remove(id);
        }
    }

See next blog post for refactorings

| | #