model view controller - MVC, DI and Mock Objects -
How much more can I apply MVC as an abbreviate useful to wrap my head In the process for the automated unit testing purposes, I came up against an interesting puzzle: how and how can I declare a fake object for my database?
What I have here
- Contact View is a form that implements IContactView IContactModel so that she can update the model object in response to event notifications.
- Contacts is a class that IContactModel . It captures the code in business rules to manipulate the object, as well as to update / update the data in the Data Access Layer.
- ContactController is a class that has both knowledge of IContactController and IContactModel and IContactView .
- Database is a class that has IDBS and methods to select, insert, update, and delete data in the database.
This is a difficult part for me. Model object needs to know how to interact with IDBS so that it can work on the actual database or the counterfeit object.
Do not know the administrators That was how the data was stored. In this way, if I decide to do this later, then I can swap the IDbase for something like ijasonstore or IXmlStore and only touch the model classes. I think your thoughts & amp;
I get some potential solutions, but I'm not sure what's best.
- I can declare a singleton that exposes the public property, the database (type IDbase ). In this way, the unit can set the test database to a counterfeit object, and the output code will set it in the production database, but this means that the production code, at some point, will have to know about IDBase. I think that is indispensable; But I'm hoping that some other solution would be better.
- I can modify the model class to maintain the context of the database, it seems undesirable because it is a result of too many extra codes. And, I've been brought back to a class directly: The model whose example has been declared should know which ID base I want to use.
I'm sure there are options out there, and I'm just ignorant of them. So I'm throwing it there: how would you do this, and what have you seen that works well?
Thanks in advance.
It is better to keep model objects in the form of POCO / POJO, and by using the controller's inject dependency Model (see more).
For various reasons, your best choice is for constructor injection DI. The C # based example of a controller with the injection IDbase is:
Public class ContactController: IContactController {Private Readonly IDBS DB; Public Contact Controller (IDBS DB) {if (DB == blank) {New argument (NullException) ("DB"); } This.db = db; } Public IContactView CreateView (int id) {var model = this.db.Select (id); Return a new contact view (model); }}
I do not know that it looks like your existing interface, but you must be enough to give an idea. Note that how to support read
keywords and guard clauses, how to make injection dependencies an indefinable ContactController so that the remaining code can be guaranteed that it is always present.
You can use either poor man's de or appropriate container to wire your application in the entry point of the application. This will be where you map the IDbase in a solid implementation, allowing you to follow the code in the rest of the code.
Comments
Post a Comment