Den söndagen den 22:e december 2013 kl. 10:54:40 UTC+1 skrev egon:
mgo (http://labix.org/mgo) has a really good API for doing CRUD things, I
think you can steal a lot from them. Maybe, something like
https://gist.github.com/egonelbre/8080345... i.e. you still keep your
structures in the Controller.
My DB logic abstraction is like mgo, and fits pretty well so that's why I
want to do the same for the controller part
Regarding the Controller... I think you are putting too much pressure on
the Controller and it doesn't know yet what it wants to be... Read the
Controller posts by Carlo Pescio, (
http://www.carlopescio.com/2012/03/life-without-controller-case-1.html).Figure out how to name the parts and you'll probably get to a better
solution. Depending on your application CQRS/DDD may suit better... i.e.
CRUD approach usually couples together the querying and changing which can
cause bottlenecks. Search for Greg Young videos for that.... but obviously,
it will be a totally different architecture and it requires more to setup
than the CRUD approach.
Thanks for the links, very interesting. The RESTful API fits well with CRUD
though, so I don't think changing to CQRS will solves the problem. The
responsibilities of the controllers right now is to unmarshalling data from
JSON into structs and write to the response. I just want to abstract common
code so that it is still DRY as I add more endpoints for the API. The
methods that is not common will still stays in the individual controllers.
I don't how much and how your structures differ, i.e. if the structures
can be abstracted then the Controller and DB part will easily follow into a
nicer abstraction. Of course you'll get dynamic structures then which could
be problematic.
The data model differs a lot between different entities (of course), but
each controllers only differ in for example endpoint prefix (e.g. /foos or
/bars) and the type of structs to unmarshalling data into.
I'm trying to avoid the reflect package if I can, it seems like the Swiss
army knife for all kind of Go problems.
+ egon
On Sunday, December 22, 2013 11:14:43 AM UTC+2, Peter Nguyen wrote:Yes, I'm sorry, here is the full story. I'm working on a RESTful web
application using one of the many go microframework. First I created a
controller to one of the endpoints, let's call it /foos that has method to
create, read, update, delete the entities. In the beginning, the DB logic
was inside the methods of the controller, but when I have to create a
second endpoint, let's call it /bars, it seemed necessary to abstract the
DB logic to its own struct and methods. This worked quite well using object
interface{} as parameter and pass on a pointer to the struct or passing the
struct itself and do type assertion when you get it back.
My next step now is trying to abstract the common methods of the
controller, such as create, read, update, delete to a common struct, just
as I did with the DB logic. Because most of the code for it is the same,
except for the type of the struct that you're working with and its fields
(such as DB table name, endpoint prefix etc.). As I'm new to Go, I wanted
to try different approaches such as embedding to see if it was applicable
to this problem.
Den söndagen den 22:e december 2013 kl. 09:51:41 UTC+1 skrev egon:
On Sunday, December 22, 2013 10:10:00 AM UTC+2, Peter Nguyen wrote:
Yes, that's what I thought, a bad idea. I'm trying to create an
abstract struct with common methods to be reused by other structs. How is
the idiomatic way to do this?
Please tell the full story of what you are trying to accomplish.
Currently you are just explaining methods, which help you do something..
but not what that something is.... and I just don't mean the concept of
inheritance... are you working on a game, tcp protocol, website.... etc.
The actual problem you are having! Otherwise you are going to get vague
answers and a solution that may not actually solve your problem properly.
Basically, the idiomatic way is not to do that... and to do something
else... unless the other options even worse... which in case that would be
the "best" way to do it.
+ egon
Den söndagen den 22:e december 2013 kl. 08:54:10 UTC+1 skrev Jesse
McNelis:
On Sun, Dec 22, 2013 at 5:58 PM, Peter Nguyen wrote:
Is there a way to access the outer struct and its field from the
embedded struct? Should you pass the outer struct as a parameter?
Yes, you'll have to pass a pointer to the embedding struct to the
embedded struct.
But when people ask about this they are generally trying to fight the
language and emulate inheritance.
What problem are you trying to solve?
--
=====================
http://jessta.id.au --
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+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/groups/opt_out.