This looks like a simple question, but actually it takes a bit of thought 
and experimentation to arrive at a working structure.

I'm also working on an MVC solution.  I've set up the source code structure 
so that it's ready to commit to github, but the first version is not 
complete, so it's not yet committed.  If you plan to publish your solution 
via github you should do the same.

My github account is goblimey.

My website contains information about films.  I have several resources 
(which are database tables) and a separate model and controller for each.  
One of my resources is a Person, represented by a table called people.

I'm designing through interfaces, so most of my objects are concrete 
structures that implement interfaces.  This is useful because I can test a 
lot of the modules using mock objects. 

So, my base source code structure is src/github.com/goblimey/films.  Within 
that I have directories controllers, models and views amongst others.

I have an interface Person.  This is in 
src/github.com/goblimey/films/person/Person.go.  In the same directory I 
have a simple structure ConcretePerson (in ConcretePerson.go) that 
satisfies the interface and a unit test ConcretePerson_test.go.  All of 
these are in the package person, so they all start with

package person

I have another structure called GorpMysqlPerson that satisfies the Person 
interface and implements it using Gorp and MYSQL.  This means that is has 
the same methods as ConcretePerson.  You cannot define two structures with 
the same method names in the same directory, so it's in a subdirectory 
src/github.com/goblimey/films/person/gorpmysql.  This is the package 
gorpmysql.  So the first few lines of GorpMysqlPerson.go are:

package gorpmysql

import (
    personModel "github.com/goblimey/films/models/person"
)


I rename the person package personModel within this module because I have 
lots of packages called person and I need to avoid clashes.  So in the rest 
of this module, I refer to the Person interface as personModel.Person.

My people controller is 
github.com/goblimey/films/controllers/people/Controller.go, so it's in the 
package people.  (Initially I called the module PeopleController.go, but 
the golint tool advised me not to, so I changed it.  I agonised about 
whether the package should be called people or person.)  the controller 
imports the Person interface and the GorpMysql model:

package people

import (
    personModel "github.com/goblimey/films/models/person"
    gorpPersonModel "github.com/goblimey/films/models/person/gorpmysql"

As I said earlier, I'm designing through interfaces, so although I use the 
GorpMysql version of the Person model, throughout the code I refer to the 
objects via the Person interface wherever possible.  In other words, only 
the modules that have to know exactly what version of a Person it's dealing 
with, know exactly what it is.  If a module doesn't need to know exactly 
what the object is, it defines it as a Person.  For example, I have various 
forms with methods like this:

type PersonForm interface {
    // Person gets the Person embedded in the form.
    Person() personModel.Person
    // SetPerson sets the Person in the form.
    SetPerson(person personModel.Person)

So my form object contains an object that satisfies the Person interface, 
but that's all it knows about that object.

The controller creates a GorpMysqlPerson and puts it into the form using 
SetPerson().  Once it's in the form, it can be passed around the rest of 
the system and all most of the rest of the code knows about it is that it 
is some structure that satisfies the Person interface.  I can then test 
those bits of code in isolation using mocking.  (I'm currently using 
gomock.)  However, it turns out that the whole structure of my solution is 
affected by that decision.  In particular, my controller works in two 
stages.  There is one method that receives the request, creates concrete 
objects and passes them as interfaces to subsidiary methods that do most of 
the work.  The worker methods don't know and don't need to know precisely 
what kind of object they are working on. 

Once I have all this stuff working I plan to publish it via my github 
account, but it's not ready yet.  Meanwhile, I think the stuff above 
answers your question (which, as I said, is not as simple as it looks).



On Tuesday, August 2, 2016 at 1:25:21 PM UTC+1, kritika...@indiamart.com 
wrote:

> i have created two folders model and controller . 
>> now according to MVS ,logic should be in controller and when i am trying 
>> to access the funtion written in model folder in controller , i am getting 
>> error .
>>
>> please help me with import statements that i should include with example 
>> if possible 
>>
>
>
> *Watch our latest TV Commercial #IndiaKiKhoj 
> <https://youtu.be/SoBcg2z0pZE%20%20>*
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to