Create a Simple Shopping Cart Service with Go – I

I want to create a simple shopping cart service with go lang.First of all,I’ll create a very simple domain model for shopping cart except any bounded context like items,details etc.

I use constants for Status and TypeCode fields like

So, i created a very simple domain model for shopping cart.And now, I ll create a handler for create,update,delete or other implementations.

Before I began create handlers , I ll write a generic json response and error functions.A basic response function

And a basic error function

I created a simple error and response functions.and now, i will create a createCart function.

I ll try to explain the above code line by line.

First of all, I created a cart object like var cart Cart

And then I read request body and assing body to cart object like json.NewDecoder(r.Body).Decode(&cart)

And then check assigned object required properties like UserId etc.

if cart.UserId == “” { JSONError(w, r, start, “Incorrect body”, http.StatusBadRequest) return }

After that I created a bson object and save to mongodb.

If any exception occured i try to set http status as 500.

if err != nil { JSONError(w, r, start, “Failed to create cart”, http.StatusInternalServerError) return }

By the way, I use mongodb as db provider.And i used the following code block for the database connection

You can define connection yaml and store your connection string, collection name, db name etc. at configuration file but i did not want to worry about it.

At this point ,i created a sample domain model, handlers and db connection provider.And now, I ll define a routing for my handlers like

I used NotFound for undefined routings.

Finally, I created a main function like that

And the curl request


Ill write and implement update and delete handlers in the next article.


Happy coding!

Design a basic basket domain with EventSourcing and Pipeline Pattern

In this sample,I develop a basic basket with products and discounts using EventSourcing and DDD.I dont need of any heavy Aggregate base library but just using a functional approach.


First of all, I created a basic helper for object validation.

you can find different varieties of ensure at github,gist etc. And then, I created a basic basket aggregate like that.

So, we have to define a basic object for all events.

And I created all Basket Events

Basket Created Event

Discount Applied Event

Product Added Event

After I defined the events, I created commands which are related with Basket aggregate.

Add Product Command

Create basket command

Finally, I developed  a basic pipeline pattern implementation for discounts.

And then, I created a discount service



Happy Coding!

Create a simple command validator for annoying validation errors

Sometimes, unhandled validation errors occurs while processing commands. it will probably cause of latency under load.So,how can we prevent this?

Let’s try it. You can define a basic validation interface for all commands like that

And then , you can create a command validator for an every single command.

I used FluentValidator for Rule Engine.See AbstractValidator

Finally, You can register ICommandValidator to your container.

And you can validate an every single command at your publisher.


So, you do not have to worry about validation errors anymore.


Happy codings!



Applying simplified Domain Repository with MongoDB

I’m interested in cqrs,event sourcing and ddd concepts in the last few months.I have read many many blog post,books and presentations and I looked at several source code at github.After a few unsuccessful attempts,I developed a generic repository for our domain and aggregates.To be honest,I didn’t want to develop a domain repository for every single domain object.Let’s focus on the following interface.

I have an AggregateRootBase for all domain objects.So, what exactly does AggregateRootbase look like?

Do all domain objects have to purgeable? In fact, the answer is not certain.It  depents on your structure, your business requirements or your ubiquitous language.

What does purgeable mean? If you want to delete domain objects from repository in some cases, you can mark as purgeable.

Anyway, see below for implementation of IDomainRepository.

We can use any source for domain repository like sql,oracle,postgresql,couchbase etc.But I choose mongodb cause of Eventual Consistency.Anyway, lets try mongodb implementation.

Firstly, I created a base object for MongoDb settings.


And then ,I developed an implementation.

Well, how exactly did I use it? I used it in every command handler.


Happy codings!