它是被认为不好的做法注入DAO到构造?如果是这样,为什么呢? [英] Is it deemed bad practice to inject the DAO into the constructor? and if so, why?

查看:114
本文介绍了它是被认为不好的做法注入DAO到构造?如果是这样,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(DAL)数据访问层(不过这个问题是相关的DAO也),这是一个RESTful Web服务在android系统通信(比的事实,我不希望包括重相关的其他不宁静的图书馆的互动没那么复杂)。

I have a (DAL) Data Access Layer (but this question is relevant for DAOs as well) which is communicating with a restful web service in android (which is less relevant other than the fact that I am not wanting to include heavy restful libraries as the interaction isn't so complex).

我有它包装是由从该数据存取层信息填充的列表,当用户将扫描下来并到达该列表的底部对象,这个对象检索另一组从DAL信息。

I have a object which wraps a list which is populated by information from this data access layer, as the user scans down and reaches the bottom of this list, this object retrieves another set of information from the DAL.

我想调用的类此列表包装对象来只做出的名单包装对象,而不是调用DAL(或DAO)。然后,我可以构造一个DAL并将它传递给这些名单包装对象的构造函数,然后调用的类可以只不断向此列表包装对象调用该对象可以处理新信息的retreival。

I would like the calling class of this list wrapping object to only have to make calls to the the list wrapping object and not the DAL (or a DAO). I could then construct a single DAL and pass it to the constructors of these list wrapping objects, then the calling class can just keep making calls to this list wrapping object and that object can handle the retreival of new information.

那么,这听起来像不好的做法,或只是一个非常糟糕的解释吗?

So, does this sound like bad practice or just a really bad explanation?

这是个糟糕的主意注入DALS和DAO,中域对象的构造?

Is it a bad idea to inject DALs and DAOs in the constructor of the domain object?

推荐答案

答案取决于你是否感觉到强烈的贫血的领域模型和混合的面向对象的函数式编程。

The answer depends on whether you feel strongly about "anemic domain models" and mixing object-oriented with functional programming.

一个问题是,您将创建一个循环依赖这种方式:模型和持久性包必须了解对方。如果您使用更实用的风格,并且不给DAO引用一个模型对象,那么它是一个单向的关系。

One problem is that you'll create a cyclic dependency that way: model and persistence packages have to know about each other. If you use a more functional style, and don't give a DAO reference to a model object, then it's a one-way relationship.

我不喜欢你的设计了。我担心它过于耦合的。我不是在混合多功能风格困扰。

I wouldn't like your design much. I fear that it's too coupled. I'm not bothered by mixing a functional style in.

这篇关于它是被认为不好的做法注入DAO到构造?如果是这样,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆