简体   繁体   English

针对每个存储库方法的Android Clean Architecture UseCase?

[英]Android Clean Architecture UseCase for each method of repository?

Does we need to create UseCases for each method from Repository interface in domain layer? 我们是否需要为域层中的Repository接口为每个方法创建UseCases

For example assume that I have such Repository interface 例如,假设我有这样的Repository接口

interface ThingRepository {
    void create(Thing thing);
    void delete(Thing thing);
    List<Thing> readAll();
    int size();
}

As you can see there is size() method that returns number of records in database or in file, whatever. 正如您所看到的那样, size()方法返回数据库或文件中的记录数,无论如何。 And this method is pretty fast. 这种方法非常快。 I guess that there is no need for UseCase for this method because it wouldn't block UI thread and can be executed synchronously. 我想这个方法不需要UseCase ,因为它不会阻止UI线程并且可以同步执行。

So could you explain me when you create UseCase s and when you don't. 所以,当你创建UseCase时,你能解释我吗? Basically is there any rules for UseCase creation? 基本上是否有任何UseCase创建规则?

Sorry if there is some misunderstanding in this question. 对不起,如果这个问题存在一些误解。

Thanks in advance ;) 提前致谢 ;)

Also I opened the same issue on Android-CleanArchitecture repo on github but nobody answered it yet that's why I'm asking here. 我也在github上的Android-CleanArchitecture repo上打开了同样的问题 ,但没有人回答它,这就是我在这里问的原因。

Fun question! 有趣的问题!

The easy answer. 简单的答案。 You as a programmer do not write the use-cases. 作为程序员,您不会编写用例。 Your specifications document is where you derive your use-cases from. 您的specifications document是从中派生用例的地方。 Once you have your use-cases all lined up nice in your kanban board, then you start solving those problems. 一旦你的用例在你的看板上排成一行,那么你就开始解决这些问题了。 One at a time. 一次一个。

Notice I said programmer ? 注意我说程序员 That's only if you go to work, sit down, boss hands you a specification, and you code for 8 hours and then go home. 只有当你去上班,坐下来,老板递给你一个规格,你编码8小时然后回家。 However, that's usually not the case and some of us are architects as well. 然而,通常情况并非如此,我们中的一些人也是建筑师 (This is an oversimplification for my following point.) (这是我对以下几点的过度简化。)

From the context of your original post, and why everyone in the comments is jumping on you is because of this... 从你原来的帖子的背景,以及为什么评论中的每个人都跳上你是因为这...

Does we need to create UseCases for each method from Repository interface in domain layer? 我们是否需要为域层中的Repository接口为每个方法创建UseCases?

Simply put: In test driven development, you don't write a single character of code until you write a failing test first. 简单地说:在测试驱动开发中,在首先编写失败测试之前,不要编写单个代码字符。 The same goes for use-case driven development; 用例驱动的开发也是如此; you don't write a single character of code until you have a use-case you are trying to solve. 在你有一个试图解决的用例之前,你不要写一个代码字符。

and

As you can see there is size() method that returns number of records in >database or in file, whatever. 正如您所看到的那样,size()方法返回>数据库或文件中的记录数,无论如何。 And this method is pretty fast. 这种方法非常快。 I guess that >there is no need for UseCase for this method because it wouldn't block UI >thread and can be executed synchronously. 我想那>这个方法不需要UseCase,因为它不会阻止UI>线程并且可以同步执行。

What that sounds like is. 这听起来像是。 "I didn't have a use-case to display the number of records in a database so I added a size() function to the repository interface because I thought it should have one, and I'm trying to write a use-case for it." “我没有用例来显示数据库中的记录数,所以我在存储库界面添加了一个size()函数,因为我认为它应该有一个,我正在尝试编写一个用例为了它。” That simply not the goal of use-case driven development. 这根本不是用例驱动开发的目标。

So all that being said, let's come back to the size() function in your ThingRepository . 所以说的是,让我们回到你的ThingRepositorysize()函数。 The only reason you should have added the size() function is to solve a use-case. 应该添加size()函数的唯一原因是解决用例。

Example: "The application should display the total number of all Things in the database. 示例:“应用程序应显示数据库中所有Things的总数。

The problem with that use-case is, if I'm displaying Things to a user, then more than likely my Presenter has a _ThingRepository injected into it. 这个用例的问题是,如果我向用户显示Things ,那么我的Presenter很可能_ThingRepository注入其中。 I would much rather run a _ThingRepository.readAll().Count() because it's either already in memory, or needs to be at some point for other Presenter functions, which is much faster than making another trip to the database (which could possibly be in another country) for a simple record count. 我宁愿运行一个_ThingRepository.readAll().Count()因为它已经在内存中,或者需要在某些时候用于其他Presenter函数,这比再次访问数据库要快得多(这可能是在另一个国家)进行简单的记录计数。

If you were trying to solve the size() use-case first, your kanban board is probably out of order, as "Displaying things to user" is a Presenter function and an implementation detail that should be put off until the last responsible moment . 如果您首先尝试解决size()用例,那么您的看板可能会出现故障,因为“向用户显示内容”是一个Presenter函数和一个应该推迟到最后一个负责时刻实现细节

So, does the size() function even really need to be there? 那么, size()函数是否真的需要存在? Not unless you have a really good reason to put it there, and not until you need it. 除非你有充分的理由把它放在那里,直到你需要它。

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

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