[Translate] Use a concise architecture in Go applications (2)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The original text here, the continuation of the former ...

--– Translation Divider Line--–

Using a concise architecture in the Go app (2)

Architecture implementation

The first step is to implement the domain layer. As has been said before, the application and its use case will be fully available, but this is not a complete mall. Therefore, the code that defines the realm should be short enough to fit in a file:

package domainimport ("errors") type Customerrepository interface {Store (Customer customer) Errorfindbyid (id int) Customer}type itemrepository Interface {Store (item item) Errorfindbyid (id int) Item}type orderrepository Interface {Store (Order order) Errorfindbyid (id int) Order}type Customer s truct {ID intname string}type Item struct {id intname stringvalue float64available bool}type Order struc t {Id Intcustomer Customeritems []item}func (Order *order) ADD (item Item) error {if!item. Available {return errors. New ("Cannot add unavailable items to order")}if Order.value () + item. Value > 250.00 {return errors. New (' an order could not be exceed a total value of $250.00 ')}order. Items = append (order. Items, item) return Nil}func (Order *order) value () float64 {sum: = 0.0for I: = range order. Items {sum = sum + order. Items[i]. Value}return sum} 

Obviously, this code does not rely heavily on anything, except for some methods that return an error and introduce the "errors" package. Although the domain model described here will eventually be in the form of a row in the database, there is no database-related code.

We defined three so-called Go interfaces for storage as an alternative. Storage is a concept that comes from domain-driven design: It abstracts the preservation and loading of domain models from some kind of persistence mechanism. From a domain perspective, a store is simply a container for acquiring (FindByID) or the store domain model.

Customerrepository,itemrepository and Orderrepository are just interfaces. Because they are interfaces between databases and applications, they are implemented at the interface layer. Here's how the dependency principle applies to Go applications: The abstraction interface defined by the inner layer does not refer to anything outside the outer layer, and its implementation is defined in the outer layer. This implementation can be injected into the layer where you want to use it, and in a minute you can see that this example is applied to the use-case layer.

In this way, the use-case layer can use the expression of the domain layer to refer to the concept of the domain layer-the storage area. At the same time, the code actually executes is at the interface layer.

For each part of each layer, there are three interesting questions: where it is used, where its interface is, and where it is implemented.

For orderrepository example, the answer is as follows: It will be used in the use case layer, its interface belongs to the domain layer, its implementation belongs to the interface layer.

On the other side, the Order entity's Add method is used by the use-case layer, and the same interface belongs to the domain layer. But its implementation is also at the domain level, because it does not need anything outside the domain layer itself.

There are three structures that implement the definition of a store interface: Custormer, Order, and Item. They represent three domain models. The Order entity implements some additional functionality through two methods AddItem and value, which is just an internal use of the assist function. AddItem implements the specific domain functions required by use cases.

There is some extra detail in this code when it comes to discussing the overall architecture. As you already know, we've added some constraints to the AddItem method. It will soon be discovered that our application has several constraints in several places and it is interesting to discuss which constraints belong to where.

The first constraint is not to allow the addition of an unavailable item to the order--obviously this is a commercial constraint. The restriction that a user is not allowed to place an order on an unavailable item is the same for a Web store and a telephone hotline; This is not something that is unique to our software--defining this constraint is driven by business acumen.

The same is true for orders that don't exceed $ A--whether the store is a website or a desktop game, which is always an effective business rule.

Other constraints in some places, where the value of a commodity must be stored in a database, you must take extra care of the floating-point number Saved by the Value field in the database; However, this is a technical constraint, not a business constraint, and it does not belong to the domain bundle.

On the other hand, the database interface code and the database itself do not have to worry about this at all when the total number of persisted orders exceeds $ A, and they are well-compliant with this business rule. This example is a powerful explanation for Uncle Bob's idea, such as making a completely opposite assumption: for example, adding the $ $ order constraint to the database's stored procedure. As soon as your app starts to grow, wish you all the business rules to stay intact. I'd be more than happy at all times to keep them in the same place.

--– Translation Divider Line--–

To be Continued ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.