srcddd
目录
application
assembler
UserReq.go
UserRsp.go
dto
MessageResult.go
UserDTO.go
services
UserService.go
domain
aggregates
Member.go
models
IModel.go
UserAttrs.go
UserLogAttrs.go
UserLogModel.go
UserModel.go
repos
IUserRepo.go
services
UserLoginService.go
valueobjs
UserExtra.go
infrastructure
dao
UserLogRepo.go
UserRepo.go
utils
SysUtil.go
interfaces
configs
UserServiceConfig.go
controllers
UserController.go
main.go
main.go
package main
import (
"dbTest/srcddd/interfaces/configs"
"dbTest/srcddd/interfaces/controllers"
"github.com/shenyisyn/goft-gin/goft"
)
func main(){
goft.Ignite().
Config(configs.NewUserServiceConfig()).
Mount("v1",controllers.NewUserController()).
Launch()
}
application\services\UserService.go
package services
import (
"dbTest/srcddd/application/assembler"
"dbTest/srcddd/application/dto"
"dbTest/srcddd/domain/repos"
"dbTest/srcddd/domain/valueobjs"
)
type UserService struct {
AssUserReq *assembler.UserReq
AssUserRsp *assembler.UserRsp
UserRepo repos.IUserRepo `inject:"-"`
UserLogRepo repos.IUserLogRepo `inject:"-"`
}
func(this *UserService) GetSimpleUserInfo(req *dto.SimpleUserReq) *dto.SimpleUserInfo {
userModel:=this.AssUserReq.D2M_UserModel(req) //DTO对象转为实体
userModel.UserId = 101
userModel.UserName = "test"
userModel.UserPwd = "123"
extra := &valueobjs.UserExtra{UserCity: "test"}
userModel.Extra = extra
//this.UserRepo.FindById(userModel)
return this.AssUserRsp.M2D_SimpleUserInfo(userModel)
//member:=aggregates.NewMember(userModel,this.UserRepo,this.UserLogRepo)
//return this.AssUserRsp.M2D_SimpleUserInfo(member.QueryUser().User)
}
application\assembler\UserReq.go
package assembler
import (
"dbTest/srcddd/application/dto"
"dbTest/srcddd/domain/models"
"github.com/go-playground/validator/v10"
)
type UserReq struct {
//v *validator.Validate
}
func(this *UserReq) D2M_UserModel(dto *dto.SimpleUserReq) *models.UserModel {
validate := validator.New()
err := validate.Struct(dto)
if err != nil {
panic(err.Error())
}
return models.NewUserModel(
models.WithUserID(dto.Id),
)
}
application\assembler\UserRsp.go
package assembler
import (
"dbTest/srcddd/application/dto"
"dbTest/srcddd/domain/aggregates"
"dbTest/srcddd/domain/models"
)
type UserRsp struct {
}
func(this *UserRsp) M2D_SimpleUserInfo(user *models.UserModel) *dto.SimpleUserInfo {
simpleUser := &dto.SimpleUserInfo{}
simpleUser.Id = user.UserId
simpleUser.Name = user.UserName
simpleUser.City = user.Extra.UserCity
return simpleUser
}
func(this *UserRsp) M2D_UserLogs(logs []*models.UserLogModel) (ret []dto.UserLog){
return
}
func(this *UserRsp) M2D_UserInfo(mem *aggregates.Member) *dto.UserInfo {
userInfo := &dto.UserInfo{}
userInfo.Id = mem.User.UserId
userInfo.Logs = this.M2D_UserLogs(mem.GetLogs())
return userInfo
}
application\dto\MessageResult.go
package dto
type MessageResult struct {
Result interface{} `json:"result"`
Message string `json:"message"`
Code int `json:"code"`
}
application\dto\UserDTO.go
package dto
import "time"
// mark 输入
type(
SimpleUserReq struct {
Id int `uri:"id" binding:"required,min=100"`
}
)
// 输出
type (
SimpleUserInfo struct {
Id int `json:"id"`
Name string `json:"name"`
City string `json:"city"`
}
UserLog struct {
Id int `json:"id"`
Log string `json:"log"`
Date time.Time `json:"name"`
}
UserInfo struct {
Id int `json:"id"`
Name string `json:"name"`
City string `json:"city"`
Phone string `json:"phone"`
Logs []UserLog `json:"logs"`
})
interfaces\configs\UserServiceConfig.go
package configs
import (
"dbTest/srcddd/application/assembler"
"dbTest/srcddd/application/services"
)
type UserServiceConfig struct {
}
func NewUserServiceConfig() *UserServiceConfig {
return &UserServiceConfig{}
}
func(*UserServiceConfig) UserService() *services.UserService {
return &services.UserService{
AssUserReq: &assembler.UserReq{},
AssUserRsp: &assembler.UserRsp{},
}
}
interfaces\controllers\UserController.go
package controllers
import (
"dbTest/srcddd/application/dto"
"dbTest/srcddd/application/services"
"github.com/gin-gonic/gin"
"github.com/shenyisyn/goft-gin/goft"
)
type UserController struct {
UserSvr *services.UserService `inject:"-"`
}
func NewUserController() *UserController {
return &UserController{}
}
//GET /users/123
func(this *UserController) UserDetail(ctx *gin.Context) goft.Json {
simpleUserReq := &dto.SimpleUserReq{}
ctx.ShouldBindUri(simpleUserReq)
return this.UserSvr.GetSimpleUserInfo(simpleUserReq)
}
func(this *UserController) Build(goft *goft.Goft) {
goft.Handle("GET","/users/:id",this.UserDetail)
}
func(*UserController) Name() string {
return "UserController"
}
domain\aggregates\Member.go
package aggregates
import (
"dbTest/srcddd/domain/models"
"dbTest/srcddd/domain/repos"
)
//会员聚合 ----会员: 会员+日志
type Member struct {
User *models.UserModel // 聚合根
Log *models.UserLogModel
userRepo repos.IUserRepo
userLogRepo repos.IUserLogRepo
}
func NewMember(user *models.UserModel, userRepo repos.IUserRepo, userLogRepo repos.IUserLogRepo) *Member {
return &Member{User: user, userRepo: userRepo, userLogRepo: userLogRepo}
}
func NewMemberByName(name string, userRepo repos.IUserRepo, userLogRepo repos.IUserLogRepo) *Member {
user := userRepo.FindByName(name)
return &Member{User: user, userRepo: userRepo, userLogRepo: userLogRepo}
}
func(this *Member)Create() error{
err := this.userRepo.SaveUser(this.User)
if err != nil {
return err
}
this.Log = models.NewUserLogModel(
this.User.UserName,
models.WithUserLogType(models.UserLog_Create),
models.WithUserLogComment("add new user" + this.User.UserName),
)
return this.userLogRepo.SaveLog(this.Log)
}
func(this *Member) GetLogs() (ret []*models.UserLogModel) {
return
}
domain\models\IModel.go
package models
import "fmt"
type IModel interface {
ToString() string
}
type Model struct {
Id int
Name string //实体名称
}
func(this *Model) SetName(name string) {
this.Name = name
}
func(this *Model) SetId(id int) {
this.Id = id
}
func(this *Model) ToString() string {
return fmt.Sprintf("Entity is:%s,id is %d", this.Name, this.Id)
}
domain\models\UserAttrs.go
package models
import "dbTest/srcddd/domain/valueobjs"
type UserAttrFunc func(model *UserModel)
type UserAttrFuncs []UserAttrFunc
func WithUserID(id int) UserAttrFunc {
return func(u *UserModel) {
u.UserId = id
}
}
func WithUserName(name string) UserAttrFunc {
return func(u *UserModel) {
u.UserName = name
}
}
func WithUserPass(pass string) UserAttrFunc {
return func(u *UserModel) {
u.UserPwd = pass
}
}
func WithUserExtra(extra *valueobjs.UserExtra) UserAttrFunc {
return func(u *UserModel) {
u.Extra = extra
}
}
func(this UserAttrFuncs) apply(u *UserModel) {
for _,f := range this {
f(u)
}
}
domain\models\UserLogAttrs.go
package models
import "dbTest/srcddd/domain/valueobjs"
type UserAttrFunc func(model *UserModel)
type UserAttrFuncs []UserAttrFunc
func WithUserID(id int) UserAttrFunc {
return func(u *UserModel) {
u.UserId = id
}
}
func WithUserName(name string) UserAttrFunc {
return func(u *UserModel) {
u.UserName = name
}
}
func WithUserPass(pass string) UserAttrFunc {
return func(u *UserModel) {
u.UserPwd = pass
}
}
func WithUserExtra(extra *valueobjs.UserExtra) UserAttrFunc {
return func(u *UserModel) {
u.Extra = extra
}
}
func(this UserAttrFuncs) apply(u *UserModel) {
for _,f := range this {
f(u)
}
}
domain\models\UserLogAttrs.go
package models
type UserLogAttrFunc func(model *UserLogModel)
type UserLogAttrFuncs []UserLogAttrFunc
func WithUserLogType(logType uint8) UserLogAttrFunc {
return func(u *UserLogModel) {
u.LogType = logType
}
}
func WithUserLogComment(comment string) UserLogAttrFunc {
return func(u *UserLogModel) {
u.LogComment = comment
}
}
func(this UserLogAttrFuncs) apply(u *UserLogModel) {
for _,f := range this {
f(u)
}
}
domain\models\UserLogModel.go
package models
import "time"
const (
UserLog_Create = 5
UserLog_Update = 6
)
type UserLogModel struct {
*Model
Id int `gorm:"column(id); primary_key;auto_increment" json:"id"`
UserName string `gorm:"column(user_name);" json:"user_name" xorm:"'user_name'"`
LogType uint8 `gorm:"column(log_type);" json:"log_type" xorm:"'log_type'"`
LogComment string `gorm:"column(log_comment);" json:"log_comment" xorm:"'log_comment'"`
UpdateTime time.Time `gorm:"column(update_time);" json:"update_time" xorm:"'update_time'"`
}
func NewUserLogModel(userName string, attrs ...UserLogAttrFunc) *UserLogModel {
logModel := &UserLogModel{UserName: userName}
UserLogAttrFuncs(attrs).apply(logModel)
logModel.Model = &Model{}
logModel.SetId(logModel.Id)
logModel.SetName("user log entity")
return logModel
}
domain\models\UserModel.go
package models
import (
"dbTest/srcddd/domain/valueobjs"
"dbTest/srcddd/infrastructure/utils"
)
type UserModel struct {
*Model
UserId int `gorm:"column(user_id); primary_key;auto_increment" json:"user_id"`
UserName string `gorm:"column(user_name);" json:"user_name" xorm:"'user_name'"`
UserPwd string `gorm:"column(user_pwd);" json:"user_pwd" xorm:"'user_pwd'"`
Extra *valueobjs.UserExtra `gorm:"embedded"`//值对象
}
func NewUserModel(attrs ...UserAttrFunc) *UserModel {
user := &UserModel{}
UserAttrFuncs(attrs).apply(user)
user.Model = &Model{}
user.SetName("user Entity")
user.SetId(user.UserId)
return user
}
func(user *UserModel) BeforeSave() {
user.UserPwd = utils.Md5(user.UserPwd)
}
domain\repos\IUserRepo.go
package repos
import "dbTest/srcddd/domain/models"
type IUserRepo interface {
FindById(*models.UserModel) error
FindByName(string) *models.UserModel
SaveUser(*models.UserModel) error
UpdateUser(*models.UserModel) error
DeleteUser(*models.UserModel) error
}
type IUserLogRepo interface {
FindByName(model *models.UserLogModel) error
SaveLog(model *models.UserLogModel) error
}
domain\services\UserLoginService.go
package services
import (
"dbTest/srcddd/domain/repos"
"dbTest/srcddd/infrastructure/utils"
"fmt"
)
type UserLoginService struct {
userRepo repos.IUserRepo
}
func(this *UserLoginService) UserLogin(userName string,userPwd string ) (string,error) {
user:=this.userRepo.FindByName(userName)
if user.UserId>0{ //有这个用户
if user.UserPwd==utils.Md5(userPwd){
//记录登录日志
return "1000200",nil
}else{
return "1000400",fmt.Errorf("密码不正确")
}
}else{
return "1000404",fmt.Errorf("用户不存在")
}
}
domain\valueobjs\UserExtra.go
package valueobjs
type UserExtra struct {
UserPhone string `gorm:"column(user_phone);" json:"user_phone" xorm:"'user_phone'"`
UserQQ string `gorm:"column(user_qq);" json:"user_qq" xorm:"'user_qq'"`
UserCity string `gorm:"column(user_city);" json:"user_city" xorm:"'user_city'"`
}
type UserExtraAttrFunc func(extra *UserExtra)
type UserExtraAttrFuncs []UserExtraAttrFunc
func NewUserExtra(attrs ...UserExtraAttrFunc) *UserExtra {
userExtra := &UserExtra{}
UserExtraAttrFuncs(attrs).apply(userExtra)
return userExtra
}
func WithUserPhone(phone string) UserExtraAttrFunc {
return func(u *UserExtra) {
u.UserPhone = phone
}
}
func WithUserQQ(qq string) UserExtraAttrFunc {
return func(u *UserExtra) {
u.UserQQ = qq
}
}
func WithUserCity(city string) UserExtraAttrFunc {
return func(u *UserExtra) {
u.UserCity = city
}
}
func(this UserExtraAttrFuncs) apply(u *UserExtra) {
for _,f := range this {
f(u)
}
}
infrastructure\dao\UserLogRepo.go
package dao
import (
"dbTest/srcddd/domain/models"
"dbTest/srcddd/domain/repos"
"github.com/jinzhu/gorm"
)
type UserLogRepo struct {
db *gorm.DB
}
func NewUserLogRepo() *UserLogRepo {
return &UserLogRepo{}
}
var _ repos.IUserLogRepo = &UserLogRepo{}
func (u UserLogRepo) FindByName(model *models.UserLogModel) error {
panic("implement me")
}
func (u UserLogRepo) SaveLog(model *models.UserLogModel) error {
panic("implement me")
}
infrastructure\dao\UserRepo.go
package dao
import (
"dbTest/srcddd/domain/models"
"dbTest/srcddd/domain/repos"
"github.com/jinzhu/gorm"
)
type UserRepo struct {
DB *gorm.DB
}
func NewUserRepo() *UserRepo {
return &UserRepo{}
}
var _ repos.IUserRepo = &UserRepo{}
func (this *UserRepo) FindById(model *models.UserModel) error {
return this.DB.Where("user_id=?",model.Id).Error
}
func (this *UserRepo) FindByName(name string) *models.UserModel{
panic("implement me")
}
func (this *UserRepo) SaveUser(model *models.UserModel) error {
return nil
}
func (this *UserRepo) UpdateUser(model *models.UserModel) error {
return nil
}
func (this *UserRepo) DeleteUser(model *models.UserModel) error {
return nil
}
infrastructure\utils\SysUtil.go
package utils
import (
"crypto/md5"
"fmt"
)
func Md5(str string) string {
return fmt.Sprintf("%X",md5.Sum([]byte(str)))
}
问题解决:
1.
//error 输入
type (
SimpleUserReq struct {
Id int `json:"id" binding:"required,min=100"`
}
)
// mark 输入
type(
SimpleUserReq struct {
Id int `uri:"id" binding:"required,min=100"`
}
)
error
//GET /users/123
func(this UserController) UserDetail(ctx *gin.Context) goft.Json {
simpleUserReq := &dto.SimpleUserReq{}
ctx.ShouldBindUri(simpleUserReq)
return this.UserSvr.GetSimpleUserInfo(simpleUserReq)
}
mark
//GET /users/123
func(this *UserController) UserDetail(ctx *gin.Context) goft.Json {
simpleUserReq := &dto.SimpleUserReq{}
ctx.ShouldBindUri(simpleUserReq)
return this.UserSvr.GetSimpleUserInfo(simpleUserReq)
}
实现1
func NewUserService() *UserService {
return &UserService{
UserRepo: dao.NewUserRepo(),
}
}
usr := NewUserService()
usr.UserRepo.FindById(userModel)
//this.UserRepo.FindById(userModel)
实现2
func main(){
goft.Ignite().
Config(configs.NewUserServiceConfig(), configs.NewDBConfig(), configs.NewRepoConfig()).
Mount("v1",controllers.NewUserController()).
Launch()
}
type RepoConfig struct {
}
func NewRepoConfig() *RepoConfig {
return &RepoConfig{}
}
func(*RepoConfig) UserRepo() repos.IUserRepo{
return &dao.UserRepo{}
}
type UserService struct {
AssUserReq *assembler.UserReq
AssUserRsp *assembler.UserRsp
UserRepo repos.IUserRepo `inject:"-"`
UserLogRepo repos.IUserLogRepo `inject:"-"`
}
this.UserRepo.FindById(userModel)
总结:
1.大小写的问题
type UserService struct {
AssUserReq *assembler.UserReq
AssUserResp *assembler.UserResp
UserRepos repos.IUserRepos `inject:"-"`
//userRepos repos.IUserRepos `inject:"-"` error 无法被注入
UserLogRepo repos.IUserLogRepos `inject:"-"`
}
2.注入问题
`inject:"-"`
3.库的引入
"gorm.io/gorm"
or 不同
"github.com/jinzhu/gorm"
srcddd的更多相关文章
随机推荐
- django路由匹配、反向解析、无名有名反向解析、路由分发、名称空间
目录 django请求生命周期流程图 1.Django请求的生命周期的含义 2.Django请求的生命周期图解及流程 3.Django的请求生命周期(分布解析) 路由层 1.路由匹配 2.path转换 ...
- Linux虚拟机启动报错operating system not found解决步骤
此报错为硬盘上的启动代码丢失 实验准备步骤 1) 准备: dd if=/dev/zero of=/dev/nvme0n1 bs=446 count=1 2) 系统启动报错截图 修复步骤如下 第一步:选 ...
- 服务端挂了,客户端的 TCP 连接还在吗?
作者:小林coding 计算机八股文网站:https://xiaolincoding.com 大家好,我是小林. 如果「服务端挂掉」指的是「服务端进程崩溃」,服务端的进程在发生崩溃的时候,内核会发送 ...
- Linux_ps总结
ps命令用于监测进程的工作情况.进程是一直处于动态变化中,而ps命令所显示的进程工作状态时瞬间的 使用方式: ps [options] 常用参数 -A 显示所有进程 -a 显示现行终端机下的所有进程, ...
- springMVC配置时,静态资源和jsp文件路径没错但是访问时controller的请求报404错误。
springMVC配置时,静态资源和jsp文件路径没错但是访问时controller的请求报404错误. 1.场景 如果在web.xml中servlet-mapping的url-pattern设置的是 ...
- Python数据科学手册-机器学习: k-means聚类/高斯混合模型
前面学习的无监督学习模型:降维 另一种无监督学习模型:聚类算法. 聚类算法直接冲数据的内在性质中学习最优的划分结果或者确定离散标签类型. 最简单最容易理解的聚类算法可能是 k-means聚类算法了. ...
- 01 uniapp/微信小程序 项目day01
一.起步 1.1 配置uni-app开发环境 什么是uni-app,就是基于vue的一个开发框架,可以将我们写的一套代码,同时发布到ios.安卓.小程序等多个平台 官方推荐使用Hbuilderx来写u ...
- crictl 命令 - Kubernetes 管理命令详解
描述:crictl 是 CRI 兼容的容器运行时命令行对接客户端, 你可以使用它来检查和调试 Kubernetes 节点上的容器运行时和应用程序.由于该命令是为k8s通过CRI使用containerd ...
- frps服务端与nginx可共用443端口
转载自: https://www.ioiox.com/archives/78.html frps服务器上的nginx frps.ini配置 由于nginx占用80/443端口,frps.ini中的 v ...
- GC plan_phase二叉树挂接的一个算法
楔子 在看GC垃圾回收plan_phase的时候,发现了一段特殊的代码,仔细研究下得知,获取当前数字bit位里面为1的个数. 通过这个bit位为1的个数(count),来确定挂接当前二叉树子节点的一个 ...