MVC approach with C++ - c++

I have been learning PHP MVC pattern and it is pretty cool. have almost finished app and I can see how mess you can make a code without good design.
Now can MCV be applied to C++ apps? Where does Plugin manager/Plugins go if that is even possible?In model or controller?
Thanks!
EDIT:
I mean C++ with GUI toolkit like QT/Wxwidgets/GTK+
Also Please help me on how to implement in C++. I have learned how to do it in PHP but as you know the two languages are somehow different!
EDIT2
http://forums.wxwidgets.org/viewtopic.php?f=1&t=30983

how do you actually implement it in C++
make classes in charge of rendering know nothing about application details. Call them SomethingView classes to make this point clear
make your domain objects not know anything about visualization or user interaction. You don't need to call them Model, but you could
create a set of classes in charge of running the role of Controllers: wire somehow dependencies to view and model classes via dependency injection if possible. example: CppInject. In any case, controller classes can know both about model and view classes, so the important part is this: all the coupling between view and model objects is isolated to the controllers.
Also, this implies, that all imperative-style programming should be confined to the controller classes as well: view and model should be declarative-style. That means, they should offer services related to its role, but avoid direct interaction with other objects as side-effects
It is not true you need to implement communication between controllers and the other components with event-style system, although such system is definitely helpful, but certainly not required
surprise! the above applies to any language or framework, except of course languages that somehow already force MVC down your throat from the start, i.e: ruby on rails

MVC is a design pattern not a language specific construct, So yes you can apply it to C++ app as well.
MVC can and should be applied in any language so your User Interface is loosely coupled with the backend & either can be changed with minimum impact on each other.
The MVC pattern provides a clean separation of objects into:
Models for maintaining data,
Views for displaying all or a portion of the data, and
Controllers for handling events that affect the model or view(s).

Yes, MVC can be applied in C++. For example, the MFC framework uses Document/View architecture which is essentially an MVC.
A design pattern isn't a library or class. It's a pattern. So you don't have a generic MVC library for C++.

Use Tree frogs Framework. TreeFrog Framework is a high-speed and full-stack C++ framework for developing Web applications.

MVC is an architectural design pattern (i.e. a way of building software) commonly associated with web applications, but it is applicable in general to any software project in any language. You have to make a little abstraction effort on your project, and identify which piece of software belongs to each part (i.e. a GUI is probably part of View, etc.).
Note that this type of pattern is mainly aimed to separate developement, so that any part of the project can be developed regardless of the others. This can be annoying for a small standalone application, but useful and rewarding on bigger projects.

Personally, I use boost state machines for the logical and boost signals to connect things together.
I wrote a little example that you can analyze here:
https://github.com/edubois/mvp-player

Related

Create a separate app for my REST API or place it inside my working app?

I'm building simple gis system on geodjango.
The app displays a set of maps and I'm also attempting to provide a RESTFUL API for these maps.
I'm facing a decision whether to create a separate app for the API or to work inside my existing app.
The two apps are logically separate but they share the same models.
So what is considered better?
Although a case can be made for either of the approaches, I think keeping the APIs inside their associated apps would be a better one. Since the code in APIs is going to depend on the models, or other utility methods anyway, keeping APIs in the same app would lead to more cohesive code. Besides the very ideology behind Django apps is that they can be isolated and reused.
There used to be a similar case with storing the templates. In the initial days of Django, people used to prefer to store all the templates altogether in the same global folder (with subdirectories by the names of the app), however, in recent times even Django has started discouraging the said approach in the favour of storing templates in the respective app itself.
#hspandher's answer is very solid and will allow for most of your needs to be implemented.
There is though another approach which may be a bit more complicated to achieve but gives you all the space you may need for experimentation and reusability potential:
Separate everything:
Backend:
Isolate your API from its visualization (see frontend below) and make it completely autonomous and self-contained.
That can be achieved by separating your apps inside your Django project and expose the corresponding APIs which must be the only way for an external factor (ex. client, another app etc.) to "talk" with any one of your apps.
Frontend:
Assuming that you have your APIs exposed, you effectively separated the visualization from the logic and therefore you have many options on how to visualize your maps.
For example, you can now build a React app which can make requests to your API and visualize the responses by using any of those tools: leaflet.js, D3.js, or anything that you like really.
Summary:
The benefits of this separation are:
Separation of logic and implementation.
Better maintainability.
Many tool and technology options to use.
Reusability.
As a side note, you can read about 12 factor method and think about using it in your implementation.

Django-like Framework Pattern

I have been using Django for many years (since Django 1.2). and in the past, I used different type of web frameworks (such as CakePHP, Rails, ASP.NET MVC, and some other full-stack framework). Django wasn't my first framework.
Different frameworks have differences in their approaches and benefits. There are certain part of those framework I like and I don't. In this context, I'd like to look at the design of Django Framework in more specific.
After transition to Django, I like how it design its framework. When learning a new language (such as Go, Scala, Ruby, Haskell), I try to looks for a framework that has some similarity in its design especially those I mentioned later.
The following are the 2 Django framework design decision that is very different:
it encourage pluggable apps or apps reusability.
Hence:
moving away from monolithic design by Jacob Kaplan-Moss
using multiple apps to create a full feature site, without any model or logic
writing a Project-less Django mentioned in Practical Django Projects by James Bennett
it uses Model View Template instead of classical MVC:
mentioned in FAQ.
view describes which data is presented
template describes how the data is presented
a view normally delegates to a template
controller is probably the framework itself: the machinery that sends a request to the appropriate view, according to the Django URL configuration.
I would not believe that Django pioneer such features. I believed this pattern is very common in Framework Design. Just that I have no idea, what is this (design) pattern called? This concept is very useful to be applied in other framework. I believed knowing the name of the pattern could help me understand or even build a new framework on different language with the same concept.
Currently there are tons of web framework, most of them are following the classic MVC pattern. Some use the concept of plugin to add certain capability. Plugin however solve the reusability in different approach depending on the context.
So I did tried to learn as many framework I could in order to find an alternative framework in different languages. Hoping that I could find out the pattern that Django use. However, it is very difficult for me to learn all of them. In fact, I haven't found one so far.
I have been searching for:
Django like framework in 'ruby'
Django like framework in 'Java'
Django like framework in 'Haskell'
Django like framework in 'Go-Lang'
Django like framework in 'Scala'
Unfortunately, none of them really, highlight the concept that I'm interested in.
In this Q&A, I would like to know what do people call such framework? (Or What pattern is Django use?) Would be good if you could give a references in this design which other framework might have been using it too?
Looking at Django design philosophies
I think they use/combine a lot of different design patterns trying to fulfill the philosophies. It's difficult to map to a single concept.
Tip is to look at Software Design Pattern and it should be possible to identify many patterns are used in django. Many patterns are of course common in other frameworks as well.
For example (patterns more or less used):
modelform_factory maps to "Builder"
QuerySet can be mapped to "Lazy initialization"
The ORM/database mapped to "Adapter"
What is it about django that you cannot do in other languages?
is it the access to the database, or the models? - no, python also has SQLAlchemy; ruby has Active Record...
is it the views, or web framework? - no, you can provide views with Flask, Pyramid, rails, php ...
it is the templating system? - no, you also have Jinja; mustache, Liquid...
is it the admin contrib packages? - no, you have phpmyadmin, workbench ...
is it a set of libraries to make your development easy? a toolkit?
django has great tooling, many packages you can use; it is a platform, meaning it has enough of a core to be the starting point of many projects, and enough community behind it to have many packages to make integration into a turn-key solution a breeze.
django uses the DRY (Don't Repeat Yourself) principle as a design philosophy. From the point of reusability, keeping clear responsibilities for each piece / app makes it easy to reuse components. But that doesn't take a well-designed platform; the emphasis should be on the components that are written in a way to be reused. E.g. generic tagging, configuration / data-driven components...
Django is MVC.
MVC is a pattern not a strict rule frameworks have to apply. The pattern tries to give a commonly accepted solution to a frequent problem. The problem is "How to properly organize a web framework" and the solution is "by separating data, logic and UI" in meaningful modules.
Therefore Django is MVC. Django logically separates those concepts (and many others) in modules. That's what matters IMO. Django view is not the same as MVC view, but... poteto potato...
About app reusability:
Django is The web framework for perfectionists(...) and perfectionists (or simply good developers) write reusable code. This is expressed by Django DRY philosophy and materializes in Django apps.
I'm pretty sure you can design app-like components with other web frameworks. Of course, since apps are in Django's nature since many years, it has much better support!

When i proceed to develop a software, ui design or database design, which should be first?

I tried to design the ui with some ui mocking software, but i found it's hard for me to settle down all the detail of the design, since the database didn't design yet.
But if i first design software, then the same problem occur, I didn't have the UI, how can I create a prominent UI ?
UI first.
Mock up an elegant and easy-to-use user interface (and workflow) thinking from the point of view of the user, and only then think about the underlying database / data structures you'll need to bring that UI to life.
If you can't design your UI because you haven't yet designed your database, you're doing it wrong IMHO. How many annoying pieces of software have you used that suffered from letting the database design drive the UI design?
Edit: As others have pointed out, you need to start with use cases / user stories. The UI design and database design, whichever order you do them, should only happen after you know what your software is trying to do, and for whom.
Edit by Bryan Oakley:
(source: gapingvoid.com)
Put the user at the place he deserves. Design UI first.
Database is only a consequence of user needs.
use cases first, neither ui nor database.
If you're trying to solve a problem in an object-oriented language, it's recommended that you start thinking about the objects involved. Don't worry about the database or the UI until you've got a solid domain model nailed down that addresses all the use cases.
You don't worry about the database or the UI at first. You can serialize objects to the file system if you need persistence and don't have a database. Being able to drive your app with a command line UI is a good exercise for guaranteeing that you have a good MVC separation.
Start with the objects.
UPDATE:
The one advantage that this approach has is that it doesn't prejudice the UI with a particular database design or vice versa. The object are agnostic about the other two layers. You aren't required to have a UI or relational database at all. You're just getting the objects right. Once you have that, you can create any UI or persistence scheme you like, confident that the domain model can handle the problem you've been asked to solve.
All the above answers address your issue in a right direction. That said, I would follow the SDLC thoroughly. It helps you understand the need for the solution for the problem at hand. Then comes the requirement gathering followed by the design either UI / underlying structure that supports the UI. It's a procedure but you would benefit in the end.
Your question is very subjective.
My opinion (and it is just that) is that database and underlying structure should come first. It can often help to put down the keyboard and mouse and compile some notes on paper.
Lay out goals like what you want your application to do, list the features you require and then start thinking about how you'll build it.
This method works for me in application design.
usually you need to manipulate some data in the solutions you develop. So you should consider how this data is organised first, stabilizing this layer is fundamental at the beginning. I agree with duffymo's comment about designing the business objects first if you are in a OO world. Mapping these objects to the DB will be a part of your work. Then you add business functionality and work on the presentation layer. Of course you will have to do some refactoring from time to time, but usually the refactoring impacts the business and presentation layers more than the database.
read this, it is a good technique.
DDD - Domain Driver Design
Would you build a house without a foundation? Database design isn't the fun part but it is the foundation of most business apps and if you get it wrong it becomes the most costly to fix and the most costly to maintain.
That said, I note that there is no reason you can't work on both together as they intertwine. But before you can do either, you need to understand the requirements and the business you are writing the app for.

Customizable Web Applications

At my company we develop prefabricated web applications. While our applications work as-is in many cases, often we receive complex customization requests. We are having a problem in trying to perform this in a structured way. Generic functionality should not be influenced by customizations. At the moment we are looking into Spring Web Flow and it looks like it can handle a part of what we need.
For example, we have an Online Shopping and we have a request from a client that in a moment of checking out the Shopping Basket order has to be written to a proprietary logging system.
With SWF, it is possible to inherit our Generic Checkout Flow with ClientX Checkout Flow and to extend it with states necessary to perform a custom log write. This scenario seems to be handled well. This means we can keep our Generic Checkout Flow as is and extend it with custom functionality, according to Open/Closed principle. Our team in time can add functionality to the Generic Checkout Flow and this can be distributed to a client without modifying the extension.
However, sometimes clients request our pages to be customized. For example, in our Online Shopping app a client requests a multiple currencies feature. In this case, you need to modify the View as well as the Flow (Controller). Is there a technology that would let me extend the Generic View and not modify it? So far, only two solutions with majority of template-based views (JSP, Struts, Velocity etc.) seems to be
to have a specific version of view for each client. This obviously leads to implementation explosion
to make application configurable depending on parameter (if multipleCurrency then) that leads to code explosion - a number of configuration conditions that have to be checked in each page
What would be the best solution in this case? There are probably some other customization cases I am not able to recall. Is there maybe a component based view technology that would let me extend certain base view and does that makes sense.
What are typical solutions to a problem of configurable web applications?
each customization point implies some level of conditionality.
Where possible folks tend to use style sheets to control some aspects. For example display of a currency selector perhaps could be done like that.
Another thought for that currency example: 1 is the limiting case of many. So the model provides the list of currencies. The view displays a selector if there are many, and a fixed field if only one. Quite well-defined behaviour - easy to test reusable for other scenarios.

TDD and ADO.NET Entity Framework

I've been playing with ADO.NET Entity Framework lately, and I find that it suits my needs for a project I'm developing. I also find cool its non-invasive nature.
After generating a data model from an existing database you are faced with the task of integrating the generated model and your business logic. More specifically, I'm used to integration-test my classes that interact with the data store via mocks/stubs of the DAL interfaces. The problem is that you cannot do this using the ADO.NET Entity Framework because the entities it generates are simple classes with no interface.
The question is: how do I apply a TDD approach to the development of an application that uses ADO.NET Entity Framework? Is this even possible or should I migrate to another DAL-generation toolset?
One of the big critiques against the Entity Framework has been that it is inherently hard to test, for example in the ALT.Net Vote of No Confidence that gef quoted.
Here is a blog post discussing how to get around this, and be able to test your code without hitting the database, when using Entity Framework.
If testability is a big concern, you might want to look at another ORM framework, such as NHibernate, at least until Entity Framework 2.0 is released.
Although, the original question has been answered, I feel like I might add something:
I am currently using the Entity Framework 4.0 on an intranet site I'm building. I am able to test everything in my business logic and controllers without a database connection using the POCO support that has been added.
Although, the POCO's can be generated from the new t4 template included in VS 2010, something that I haven't been able to find in VS 2010 is a t4 template for generating your object context (the object context basically works as a built in unit of work for EF and is essential for mapping your EF objects to POCOs). Luckily Joachim Lykke Andersen in his blog post Entity Framework 4.0 Beta 1 – POCO, ObjectSet, Repository and UnitOfWork wrote a t4 template for generating it and it has been very helpful. If you pursue a solution using the EF4 that is testable without a database connection I highly recommend implementing something similar to his solution which includes a generic repository, unit of work wrapper, and a unit of work factory. It has been very helpful.
Best of luck.
I agree that version 1 of the Entity Framework is a crime against design and it definitely got my vote of no confidence. I credit the EF product team though for acknowledging the failure and responding by opening up their design process to the community. The next release isn't going to be perfect, it might not even be ready for use in a production level application, but I think they're finally starting to understand what's important to those of use who know that bad design is bad business. That being said... I'm still suspicious. Continuous design-time feedback is new to these guys and I've read quite a few statements on the ADO.NET blog that raise bright, red flags. We'll see how it goes with the release of .NET 4.0.
They appear to be trying though:
Test-Driven Development Walkthrough with the Entity Framework 4.0
"The tight coupling of the persistence
infrastructure to the entity classes
largely eliminates the ability to
efficiently use very tight feedback
cycles on the business logic with
automated testing. In its current
state, EF entity classes cannot be
effectively unit tested independently
of the database.
The efficiency of automated unit
testing of behavioral objects is
largely a matter of how easy the
mechanics of test data setup are and
how quickly the tests can be executed.
Using the actual database will make
test data setup more laborious,
introduce data to satisfy relational
constraints that are not germane to
the test, and make test execution an
order of magnitude slower.
A team’s ability to do evolutionary
design and incremental delivery is
damaged by the Entity Framework’s
inattention to fundamental software
design principles like Separation of
Concerns."
Blatantly stolen from here:
http://efvote.wufoo.com/forms/ado-net-entity-framework-vote-of-no-confidence/
If you're looking specifically at DAL-generation tools you'll have a hard time integrating this with TDD. Most dal generation tools I know also generate your business objects and tightly couple them to the DAL making testing difficult.
You can look at OR-mapping tools like nHibernate and maybe Linq to sql that enable "persistance ignorance", you can define your business objects yourself and they have no links to the DAL or any other infrastructure code. This makes testing your business logic seperately from your database much easier. I found it also enables other scenario's like occasionally connected clients far better.
This answer has changed to "Yes, you can".
You can generate POCO and interfaces using customized T4 templates such as https://entityinterfacegenerator.codeplex.com/, then create mocking objects to test EF in and out without hitting the database.