美文网首页
Lotus 与 Clean Architecture

Lotus 与 Clean Architecture

作者: chenge微博谈 | 来源:发表于2015-09-04 11:20 被阅读131次

    最近开始学习Lotus框架,文档中提到Lotus的设计思路来自Clean Architecture,如下图所示:

    可以看到Entity(相当于Rails model)居于核心,DB在外围,Repository作为数据中介。采用的是松耦合结构。Rails中model、db、view是强耦合的。

    架构:The Clean Architecture(整洁的架构)(转载)

    引入Lotus的两种方式

    • 独立模式:完全采用Lotus
    • 混合模式:Rails + Lotus model

    独立模式

    我学习了入门课,因为了解有限,留待以后再探讨了。

    混合模式

    由于Lotus是模块化的,所以我想到了在Rails中引入Lotus model,这样学习成本比较低。
    可以有效利用现有的Rails资源。

    试验还比较顺利:

    • 只是form_for不能用了,form_tag可以用。
    • helper的部分参数不支持,例如:posts_path(@post)需要改为posts_path(@post.id)

    示例代码如下:

    # Entity
    
    class Post
        include Lotus::Entity
        attributes :title, :body, :created_at, :updated_at
    end
    
    # Controller
    
    def create
        @post = Post.new(params)
        @post.created_at = @post.updated_at = Time.now
        PostRepository.create(@post)
        redirect_to posts_path, notice: 'Post was successfully created.'
    end
    
    

    遇到的问题

    每次修改了Ruby代码会导致报错,重启服务器就好了。

    Lotus::Model::Adapters::NoAdapterError in PostsController#edit
    Cannot invoke `find' without selecting an adapter. Please check your framework configuration.
    

    不知道可有解决办法?请教各位。

    混合模式的优点

    • Entity更符合SRP单一职责原则。很容易切换数据源。
    • Entity测试完全简化了,可作为每日例行测试,数据库测试可以作为集成测试。

    最后

    有兴趣用Lotus么?欢迎加入探讨。

    相关文章

      网友评论

          本文标题:Lotus 与 Clean Architecture

          本文链接:https://www.haomeiwen.com/subject/arblcttx.html