是否有任何工具或实用程序(映射器程序集)从实体(使用linq - > sql,实体框架或其他...从DB获取)构建业务对象
如果没有一个,任何人都可以建议可以完成的最佳方式,而不是从实体类中复制粘贴属性(我现在正在做什么).
我正在使用已经存在并由其他应用程序使用的数据库.
设计数据库的人没有使用多个表名,因此DataMapper在跟随关联时选择了错误的表名.
例如:
class Foo
has n :components # => the table name here should be COMPONENT, but datamapper uses COMPONENTS
end
Run Code Online (Sandbox Code Playgroud)
我该如何改变这种行为?
由于检查table_exists,此代码在最后一行失败了吗?如何在Datamapper中正确执行此操作?
require 'sinatra'
require 'DataMapper'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/blog.db")
class Post
include DataMapper::Resource
property :id, Serial
property :title, String
property :body, Text
property :created_at, DateTime
end
DataMapper.finalize
# automatically create the post table
DataMapper.auto_migrate! unless Post.table_exists?
Run Code Online (Sandbox Code Playgroud) 我正在使用DataMapper和Sinatra构建一个小型Ruby应用程序,我正在尝试定义一个基本的博客模型:
由于每个评论belongs_to帖子的事实,我很难在评论之间找到自我参照关系.我的课程现在看起来像这样:
class User
include DataMapper::Resource
property :id, Serial
property :username, String
property :password, String
has n, :post
endRun Code Online (Sandbox Code Playgroud)
class Post
include DataMapper::Resource
property :id, Serial
property :content, Text
belongs_to :user
has n, :comment
endRun Code Online (Sandbox Code Playgroud)
class Comment
include DataMapper::Resource
property :id, Serial
property :content, Text
belongs_to :user
belongs_to :post
endRun Code Online (Sandbox Code Playgroud)
我正在关注协会的指南并构建一个新对象(CommentConnection)以将两个注释链接在一起,但我的问题是每个子注释不应属于Comment类隐含的Post.
我的第一直觉是为Comments提取一个超类,这样一个子类可以是"顶级"并属于一个帖子,而另一种类型的注释属于另一个注释.不幸的是,当我这样做时,我遇到的问题是注释ID变为空.
在DataMapper中建模这种递归注释关系的最佳方法是什么?
我有一个在Ubuntu/Apache2/Passenger上运行的Sinatra应用程序.
这是一个简单的URL缩短器,可以在我的登台服务器上运行,但在导入旧数据库(包含缩短的URL)时开始抛出以下错误:
undefined method `include?' for nil:NilClass
file: resource.rb location: block in attributes= line: 332
Run Code Online (Sandbox Code Playgroud)
完全回溯在这里:
/usr/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/resource.rb in block in attributes=
if model.allowed_writer_methods.include?(setter = "#{name}=")
/usr/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/resource.rb in each
attributes.each do |name, value|
/usr/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/resource.rb in attributes=
attributes.each do |name, value|
/websites/sinatra/shortener/application.rb in block in <top (required)>
ct.attributes = {
/usr/lib/ruby/gems/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb in call
proc { |a,p| unbound_method.bind(a).call } ]
/usr/lib/ruby/gems/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb in block in compile!
proc { |a,p| unbound_method.bind(a).call } ]
/usr/lib/ruby/gems/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb in []
route_eval { block[*args] }
/usr/lib/ruby/gems/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb in block (3 levels) …Run Code Online (Sandbox Code Playgroud) 如何编写activerecord语句,
Comments.where("attachment IS NOT NULL")
Run Code Online (Sandbox Code Playgroud)
在DataMapper中
即,我想"SELECT * FROM comments WHERE attachment IS NOT NULL;在rails中使用DataMapper 执行.
请帮忙; 我对DM不太熟悉!
我只是想知道是否有可能使用datamapper/dataweave发出一个格式字符串,在数字上有一个固定长度,并用填充零来完成长度?
就像是
String.format("%056",variable_number);
我在datamapper上尝试了这个,但它不起作用.我错过了什么?
我有一个控制器,可以获取要传递给视图的数据.向此注入(通过疙瘩容器)服务,该服务使用许多域模型+业务逻辑来创建数据.
服务本身有一个注入其中的"存储库"类,它有创建数据映射器和返回域模型实例的方法.
我知道我可能没有深入了解存储库概念,因为Martin Fowler将其设置为"在映射层上构建另一层抽象"和"存储库在域和数据映射层之间进行调解,就像在-memory域对象集合." 所以我可能会错误地使用这个术语.
服务:
class InputService
{
private $repos;
public function __construct($repo) {
$this->repos = $repo;
}
public function getInitialData()
{
$product = $this->repo->getProduct();
$country = $this->repo->getCountry();
$spinalPoint = $this->repo->getPoint();
/*business logic with model instances to produce data array*/
return //array of data
}
}
Run Code Online (Sandbox Code Playgroud)
库:
class InputRepository
{
private $db;
public function __construct($db) {
$this->db = $db;
}
public function getCountry()
{
$mapper = new CountryMapper($this->db);
$country = $mapper->fetch();
return $country; //returns country object
} …Run Code Online (Sandbox Code Playgroud) php model-view-controller datamapper decoupling repository-pattern
datamapper ×8
ruby ×5
sinatra ×2
asp.net ×1
associations ×1
data-binding ×1
dataweave ×1
decoupling ×1
linq ×1
mule ×1
padding ×1
php ×1
recursion ×1