Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 587915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:15:46+00:00 2026-05-13T15:15:46+00:00

I’m trying to wrap my head around implementing MVC with as much useful abstraction

  • 0

I’m trying to wrap my head around implementing MVC with as much useful abstraction as I can for purposes of automated unit testing. In the process, I came up against an interesting conundrum: How and where do I declare the mock object for my database?

Here’s what I have.

  • ContactView is a form that implements IContactView. It has knowledge of IContactModel so that it can update itself in response to event notifications from the model object.
  • Contact is a class that implments IContactModel. It encapsulates the business rules for manipulating the object, as well as the code for fetching from/updating data in the data access layer.
  • ContactController is a class that implements IContactController and has knowledge of both IContactModel and IContactView.
  • Database is a class that implements IDatabase and contains methods for selecting, inserting, updating, and deleting data in the database.

This, to me, is the tricky part. The model object needs to know how to interact with the IDatabase so that it can operate on either a real database or a mock object. The problem I am faced with is how to provide a reference to the database without violating Separation of Concerns.

I’d rather that the views and controllers had no knowledge of how the data was stored. That way, if I chose to do so later, I could swap out IDatabase for something like IJsonStore or IXmlStore and only have to touch the model classes. I don’t want my views & controllers making any assumptions about where and how the data is stored.

I see a couple of probable solutions, but I am not certain what the best one is.

  • I can declare a singleton that exposes a public property, Database (of type IDatabase). This way, the unit tests could set the database to a mock object, and production code would set it to a production database. But that means that production code, at some point, would have to know about IDatabase. I suppose that’s unavoidable; but I am hoping there’s some other solution that’s preferable.
  • I can modify the model classes to maintain a reference to the database, taking it in the constructor. This seems undesirable simply because it results in a lot of additional code. And, I’m brought right back to square one: Whomever declares the model instance has to know which IDatabase object I want to use.

I’m sure there are alternatives out there, and that I’m just unaware of them. So I’m throwing this out there: How would you guys do this, and what have you seen that worked well?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:15:47+00:00Added an answer on May 13, 2026 at 3:15 pm

    It is often better to keep Model objects as POCOs/POJOs, and have the Controllers populate Model (and View) using injected dependencies.

    For lots of different reasons, Constructor Injection is your best default choice for DI. Here’s a C#-based example of a Controller with an injected IDatabase:

    public class ContactController : IContactController
    {
        private readonly IDatabase db;
    
        public ContactController(IDatabase db)
        {
            if (db == null)
            {
                throw new ArgumentNullException("db");
            }
    
            this.db = db;
        }
    
        public IContactView CreateView(int id)
        {
            var model = this.db.Select(id);
            return new ContactView(model);
        }
    }
    

    I don’t know whether this looks like your existing interfaces, but it should be enough to give you an idea. Notice how the readonly keyword and the Guard Clause collaborate to make the injected dependency an invariant of the ContactController so that the rest of code is guaranteed that it’s always present.

    You can use either Poor Man’s DI or a proper DI Container to wire your application up in the application’s entry point. This would be where you map IDatabase to a concrete implementation, allowing you to adhere to the Liskov Substitution Principle in the rest of the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.