当我阅读实施域驱动设计中的聚合章节时,我了解到通常我不应该在一个事务中更改多个聚合实例。
后面在关于实现Repository的章节中,我发现在Repository界面中定义了一个 removeAll 方法然后我感到困惑:isn不调用 removeAll ,换句话说,从存储库中删除多个聚合,违反了在一个事务中只更改一个聚合实例的规则?
答案 0 :(得分:0)
取决于。
就个人而言,如果用户一次性删除多个聚合,我认为这是一种设计气味,完全是出于您声明的原因。
另一方面,我经常看到后台工作可以清理的东西。在您在此类代码中提出的意义上,removeAll
具有activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<!-- Editext for Search -->
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search Choultry."
android:inputType="text"/>
<ListView
android:id="@+id/listViewshowall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/inputSearch">
</ListView>
</RelativeLayout>
MainActivity.java
package com.naman.namrathasrinivas.choultriesbangalore;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private DatabaseHandler databaseHandler;
private SQLiteDatabase db;
private ListView listview;
EditText inputSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
databaseHandler = new DatabaseHandler(MainActivity.this);
databaseHandler.createdatabase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
databaseHandler.opendatabase();
} catch (SQLException sqle) {
throw sqle;
}
listview=(ListView)findViewById(R.id.listViewshowall);
// TODO Auto-generated method stub
ArrayList<StudentEntity> list=databaseHandler.Getvalue(this.toString());
ListViewAdapter adapter=new ListViewAdapter(MainActivity.this, list);
listview.setAdapter(adapter);
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
// MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
ArrayList<StudentEntity> listentity = new ArrayList<StudentEntity>();
String selectQuery = "SELECT * FROM hall where name like 'b%' ";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
StudentEntity sentity = new StudentEntity();
sentity.setName(cursor.getString(1));
sentity.setLocality(cursor.getString(12));
listentity.add(sentity);
} while (cursor.moveToNext());
}
// return contact list
// return listentity;
}
});
}
}
功能并不罕见。
&#34;每次交易仅修改一个聚合的原因&#34;规则存在很简单:聚合是定义一致性边界的概念,因此每次中断一个聚合规则时,您都会违反自己的设计。这就是为什么你应该有充分的理由违反规则,否则不要这样做。