简体   繁体   English

将DAO注入构造函数是否被视为不良做法? 如果是这样,为什么?

[英]Is it deemed bad practice to inject the DAO into the constructor? and if so, why?

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)数据访问层(但是这个问题也与DAO有关),它正在与android中的一个宁静的Web服务进行通信(除了我不想包含大量的宁静的库这一事实之外,它的相关性较小)互动并不是那么复杂)。

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中检索另一组信息。

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). 我希望此列表包装对象的调用类只需要调用列表包装对象,而不是DAL(或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. 然后,我可以构造一个DAL并将其传递给这些列表包装对象的构造函数,然后调用类可以继续对该列表包装对象进行调用,并且该对象可以处理新信息的检索。

So, does this sound like bad practice or just a really bad explanation? 那么,这听起来像是不好的做法还是只是一个很糟糕的解释?

Is it a bad idea to inject DALs and DAOs in the constructor of the domain object? 在域对象的构造函数中注入DAL和DAO是一个坏主意吗?

The answer depends on whether you feel strongly about "anemic domain models" and mixing object-oriented with functional programming. 答案取决于您是否对“贫血症领域模型”有强烈的兴趣,以及是否将面向对象与函数式编程混合在一起。

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. 如果您使用更具功能性的样式,并且没有给DAO引用模型对象,那么这是一种单向关系。

I wouldn't like your design much. 我不太喜欢您的设计。 I fear that it's too coupled. 我担心它太耦合了。 I'm not bothered by mixing a functional style in. 我不喜欢混用功能样式。

Domain Objects are typically data carriers without any real logic. Domain Objects通常是没有任何实际逻辑的数据载体。 I would hence consider it bad design to tightly couple it with your DAO logic. 因此,我认为将其与您的DAO逻辑紧密耦合是不好的设计。 The general logic might go something like: 一般逻辑可能类似于:

public class DataService {
    private DAO dao;
}

public class UserService {
    private DataService svc;

    public DomainObject createDomainObject() {
       return new DomainObject(dao.getData());
    } 
}

You are introducing a circular dependency there, so it's not the best design. 您将在此处引入循环依赖关系,因此它不是最佳设计。

If you are developing an android app, and you are scrolling a list, then SlowAdapter and EfficientAdapter are probably what you are looking for. 如果您正在开发一个android应用程序,并且正在滚动列表,那么SlowAdapterEfficientAdapter可能就是您想要的。

If I understood you correctly what you are implementing is pagination. 如果我对您的理解正确,那么您正在实现的就是分页。 And your solution for it is how I would (and have) implemented it myself. 您对此的解决方案是我自己(并将自己)实现它的方式。

Passing the DAL to the constructor is not bad per se. 将DAL传递给构造函数本身并不坏。 Best practise would be using a Dependency Injection framework (Spring is a prominent example) in-order to avoid "hard coded" dependencies between layers. 最佳实践是使用依赖性注入框架(Spring是一个突出的示例),以避免层之间的“硬编码”依赖性。

But since you mentioned Android I doubt that using such a framework is a good idea or even possible. 但是自从您提到Android以来,我怀疑使用这样的框架是个好主意,甚至可能。 (Maybe Android has some sort of DI build-in?) (也许Android内置了某种DI?)

To summarize you seem to have given some thought about your application architecture. 总而言之,您似乎已经考虑了您的应用程序体系结构。 I wouldn't worry about passing arguments to a constructor. 我不用担心将参数传递给构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM