uch.NZ
uch.NZ

Reputation: 23

Programming to Interfaces and using loose coupling - studing TDD, unit testing and DI

Greetins, Lets suppose I have a class called Formater:IFormater, and a method LoadData(), like this:

public interface IFormater { void LoadData(string strSomeData); }
public class Formater : IFormater { public void LoadData(string strSomeData) {/*do some magic*/} }

Suppose also I have a class called View:IView and a method LoadRawData(), like this:

public interface IView { void LoadRawData(string strSomeData); }
public class View : IView {
    private IFormater _formater;
    public View(IFormater formater) { _formater = formater; }
    public void LoadRawData(string strSomeData) { _formater.LoadData(strSomeData); }
}

Now, in my prog I create an object of the View class and call LoadRawData(), something like this:

static void Main(string[] args) {
    kernel = new StandardKernel(new Modules()); //ninject
    formater = kernel.Get<IFormater>(); //ninject
    IView view = new View(formater);
    view.LoadRawData(args[0]);
}

I use ninject as DI mechanism and actually everything works fine now.

The questions:

  1. Is it a correct to have the IView interface to call Formater::LoadDtata() internally? (For myself I don't see any other way).

  2. Should I have Different names for Formater::LoadData() and View::LoadRawData(), or they should have the same name, or maybe there is a practice how to name them?

I'm sorry. It looks like I made several serius inaccuracies. I have corrected them above. Actually I don't use MVC pattern. I only thought so.

The point is that I want to isolate display logic from parsing and formating it.

My console app should open a file, read text contents and pass it to formater class, that will do some formating and bring it back (but before that formater will pass it to parser to extract necessary contents from a raw text), like this view <-> formater <-> parser

This is simple prog and I use it to better understand some best methodologies and practicies like TDD, unit testing and DI.

I'm sorry for previous inconsistiencis.

Upvotes: 1

Views: 240

Answers (1)

Ben Smith
Ben Smith

Reputation: 20230

To answer question 1), as you are using the MVC pattern, your concrete view (not IView which is an interface) should not call IController::LoadData(). The relationship between the Model, View, Controller looks like:

enter image description here

(Image taken from this article which gives a basic definition of MVC)

Instead the controller should place the LoadData data into a model, and then the controller should return a view which is associated with (i.e. can access) this model.

e.g in ASP.NET MVC you could do something like:

public class YourController : Controller
{
 IDataRepository db;

 public YourController(IDataRepository db)
 {
  this.db = db; 
 }

 public ViewResult Index()
 {
  YourModel model = db.LoadData();
  return View(model);
 }
}

What is not helping you is the fact that you are trying to use the MVC pattern from a console application. I'd suggest working through an MVC tutorial (e.g. this is pretty good) to get a better understanding.

Upvotes: 1

Related Questions