views:

199

answers:

1

In a new MVC application, I am building out all of the models, controllers, views etc. without my backend DB setup yet. I have an idea of what the backed will look like, but for now I'm focusing on the application.

I know that I can mock up a dummy model within the controller like:

public ActionResult Pictures()
{
    MyMVCApp.Models.Pictures pics = null;
    MyMVCApp.Classes.Picture pic1 = new MyMVCApp.Classes.Picture
    {
        AlbumID=1,
        Description="John Doh",
        ThumbnailLocation = "Photos/Thumbnails/John.jpg"
    };
    MyMVCApp.Classes.Picture pic2 = new MyMVCApp.Classes.Picture
    {
        AlbumID = 2,
        Description = "Jane Doh",
        ThumbnailLocation = "Photos/Thumbnails/Jane.jpg"
    };
    pics = new Pictures
    {
        PageTitle="PHOTO ALBUMS",
        PhotoAlbums = new List<MyMVCApp.Classes.PhotoAlbum>()
    };
    pics.PhotoAlbums.Add(new MyMVCApp.Classes.PhotoAlbum
    {
        AlbumID = 1,
        AlbumName = "Test1",
        AlbumCover = pic1,
        Created = DateTime.Now.AddDays(-15)
    });

    pics.PhotoAlbums.Add(new MyMVCApp.Classes.PhotoAlbum
    {
        AlbumID = 2,
        AlbumName = "Test2",
        AlbumCover = pic2,
        Created = DateTime.Now.AddDays(-11).AddHours(12)
    });
    return View(pics);
}

Doing this at least gives me something to look at on the view. My concern is when I'm ready to actually use the DB for my model, I don't want to lose my test Model.

How should I separate this out so that I don't have to change the View each time between the real controller and the test controller?

+2  A: 

You might consider not setting up those data classes within your controller. Instead request them from a InMemoryPictureRepository that will return the instances to you as you need them for testing.

In other words, put the responsiblity of data persistence behind an IRepository interface. That way you can have versions that are used for test that basically deliver hard-coded instances.

Ultimately, I think you really want to use dependency injection with an IoC container and not have the Repository referenced directly in your controller, but a drastically oversimplified look might be like this:

public class PictureController : Controller
{
    IPictureRepository _pictureRepository;

    public PictureController()
     {
        //Assume you change this for test/prod. Again you'd probably 
         //want to inject this if you really want testable controllers
        IPictureRepository _pictureRepository = new InMemoryPictureRepository();
     }

     public ActionResult Pictures()
     {
         List<Picture> pics  = _pictureService.GetAllPictures();
         return View(pics);
     }
}

Now you can have this InMemoryPictureRepository

public class InMemoryPictureRepository : IPictureRepository
{
    public List<Picture> GetAllPictures()
    {
        //All your hard-coded stuff to return dummy data;
    }
}

And this for your live stuff:

public class PictureRepository : IPictureRepository
{
    public List<Picture> GetAllPictures()
    {
       //Code to get data from L2S or wherever. This returns real stuff
    }
}
jlembke