我读过这个关于类似问题的问题,但它并没有完全解决我的问题。
我有一个应用程序,需要使用 API 中的数据。问题是这样做存在性能和技术限制。性能限制是显而易见的。技术限制在于 API 不支持我需要进行的一些更精细的查询。
我决定使用 MySQL 作为可查询缓存。
由于我需要从 API 检索的数据不会经常更改,因此我决定每天刷新一次缓存,因此我不需要任何复杂的映射器来检查缓存中是否有数据,如果没有则回退到 API。这是我的第一个设计,但我意识到,当 API 无法支持我需要进行的大多数查询时,这并不是很实用。
现在我为每个聚合都有一组两个映射器。一种用于 MySQL,一种用于 API。
我现在的问题是如何从域中隐藏持久性的复杂性,以及我似乎需要多个存储库的事实。
理想情况下,我会有一个两个映射器都遵守的界面,但正如之前所披露的那样,这是不可能的。
是否可以拥有多个存储库,每个映射器一个?
persistence domain-driven-design datamapper repository ddd-repositories
有人可以从以下内容中得出一个具体的例子:
http://www.urdalen.com/blog/?p=210
..这表明如何处理one-to-many和many-to-many关系?
我不久前给作者发了电子邮件,但没有收到回复.我喜欢他的想法,但无法弄清楚如何在简单的单表关系之外实现它.
注意:我不想使用完整的ORM.我喜欢手工做我的SQL.我想改进我的应用程序代码的设计.现在,每个域对象都有自己的类,其中包含用静态方法包装的查询.它们只是返回标量,1d数组(记录)或2d数组(记录集),具体取决于查询.
我在我正在开发的PHP应用程序中使用数据映射器模式并且有一个问题.目前,您请求具有特定ID的Site对象,映射器将查找该行,创建对象并将其返回.但是,如果对同一站点再次执行此操作,则最终会得到两个具有相同数据的不同对象.例如.:
$mapper = new Site_Mapper();
$a = $mapper->get(1);
$b = $mapper->get(1);
$a == $b // true
$a === $b // false
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是,我应该:
我无法找到在特定上下文中仅获取必要的模型字段的方法.让我们说我有一个巨大的模型,有大约30个领域(属性).但是出于搜索目的,我只需要几个.
例:
class Foo
include DataMapper::Resource
property :id, Serial
property :title, String, :length => 256
property :description, Text
property :url, String, :length => 4096
property :city, String, :length => 64
property :country, String, :length => 64
property :state, String, :length => 64
property :created_at, Time
property :updated_at, Time
property :expires_at, Time
... etc fields ...
end
Run Code Online (Sandbox Code Playgroud)
因此,对于前端(Web应用程序),大多数字段都被使用.但对于搜索,我只需要说:标题,:城市,:州.它很容易查询数据
items = Foo.all(:title.like => 'Some stuff')
Run Code Online (Sandbox Code Playgroud)
然后我需要将获取的数据打包到JSON中.据我所知,有一个名为dm-serialize的DataMapper模块可以处理所有操作.
最后,我做输出包:
response = {:error => 0, :count => items.length, :data => items}
response.to_json
Run Code Online (Sandbox Code Playgroud)
但输出项目包含所有字段,而我只需要获取其中的一些字段.由于某些原因,延迟加载不起作用.
问题是:如何指定要选择的模型字段?
有四个类:
StudentBase,CourseBase和 StudentDataMapper,CourseDataMapper
Student对象可以与Course对象建立关系.一个学生可以有很多课程.许多学生可以参观一门课程.
在ER图中,学生实体具有称为"课程"的属性,但课程对他的学生一无所知.一门课程没有属于"学生"的属性作为回报.
哪些类应该执行关系的创建?
编辑:这是系统层!在业务逻辑层中,开发人员将StudentBase和CourseBase子类化为创建Student类和Course类.除了自己的业务逻辑代码之外,创建这些类的开发人员将看不到任何代码.
我有ruby程序运行到堆栈级别太深(SystemStackError)错误,结束于datamapper:
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/collection.rb:510:in `each'
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/query/conditions/comparison.rb:616:in `map'
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/query/conditions/comparison.rb:616:in `expected'
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/query/conditions/comparison.rb:461:in `matches?'
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/query/conditions/operation.rb:498:in `matches?'
from /usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.15/lib/extlib/inflection.rb:103:in `any?'
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/query/conditions/operation.rb:159:in `each'
from /usr/local/lib/ruby/1.8/set.rb:195:in `each'
... 5188 levels...
Run Code Online (Sandbox Code Playgroud)
有没有办法调试它?喜欢调查隐藏的5188级别?Ruby-debug无法帮助我,内置的ruby探测器因"[FATAL]无法分配内存而死"
这很简单,我想通过调用DataMapper来处理正常的[show]请求,就像我在Merb中所做的那样.
使用ActiveRecord我可以这样做:
class PostsController
def show
@post = Post.get(params[:id])
@comments = @post.comments unless @post.nil?
end
end
Run Code Online (Sandbox Code Playgroud)
它通过捕获资源的异常来处理404.
相反,DataMapper不会自动执行此操作,因此我现在正在使用此解决方案解决此问题:[在答案中移动]
有可能告诉控制器在not_found函数内停止吗?
我正在ubuntu上安装data-mapper但是在发出以下命令时出错:sudo gem install dm-sqlite-adapter
luc @ nf310:〜$ sudo gem install dm-sqlite-adapter
Building native extensions. This could take a while...
ERROR: Error installing dm-sqlite-adapter:
ERROR: Failed to build gem native extension.
/usr/bin/ruby1.8 extconf.rb
checking for sqlite3.h... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir …Run Code Online (Sandbox Code Playgroud) class User
include DataMapper::Resource
property :id, Serial
property :name, String
property :email, String
has n, :records
end
class Project
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :records ?????
end
#
class Record
# SPEND_REGEX = /^[0-9]{1}:[0-5]{1}[0-9]{1}$/
include DataMapper::Resource
property :id, Serial
property :reporting_type, String
property :spend_time, String
belongs_to :user
belongs_to :project ????
end
DataMapper.auto_upgrade!
Run Code Online (Sandbox Code Playgroud)
用??? 我标记了抛出错误的关系"`execute_non_query':无法添加带有默认值NULL的NOT NULL列(DataObjects :: SyntaxError)"如何定义2与datamapper中的一个模型有很多关系?
我希望能够在DataMapper中定义一个回调,并在事务中发生这种情况以及随附的更新.例如:
class Comment
include DataMapper::Resource
has n, :updates
property :id, Serial
property :text, String
after :update do
self.updates.create(:text => self.text)
end
end
Run Code Online (Sandbox Code Playgroud)
我认为上面的代码试图做的很清楚:任何时候Comment更新,也会创建相应的Update记录.现在,可能的情况是您可以更新帖子,创建更新将失败 - 无论出于何种原因 - 因此某些历史记录将丢失.所以我真的希望在事务中发生这种操作.
这可能吗?我可以想到一些解决方法(例如,定义自定义update方法); 但我很想知道是否有"正确"的方式,或者其他人是否可以想到优雅的方法.
datamapper ×10
ruby ×5
orm ×2
php ×2
activerecord ×1
architecture ×1
callback ×1
persistence ×1
profiler ×1
repository ×1
sinatra ×1
sqlite ×1
transactions ×1
ubuntu ×1