我正在编写一些代码,这些代码将用于网站,我对我的"架构"有一些疑问。
显然,我想对这种方法和我的以下想法提出一些反馈。
1)我认为服务类应该只有将域对象转换为模型对象的代码。我正在考虑使用Dozer,只需添加一些Dozer无法做到的更复杂的代码(基本上是将来的位)。从我看到的,因为我的模型类基本上是域类,没有繁重的东西,而且成员具有相同的名称,我甚至不需要注释或xml。 / p>
2)在 Access 中,我使用所有域类的基类作为参数,所以我可以有一个抽象类,并在那里实现所有常用方法所以
public abstract class DomainAccess<T extends Domain> {
protected abstract Logger getLogger();
protected DatabaseFacade db;
protected Class<T> domainClass;
@Inject
public DomainAccess(DatabaseFacade databaseFacade, Class<T> domainClass) {
this.db = databaseFacade;
this.domainClass = domainClass;
}
@SuppressWarnings("unchecked")
public T fetchByPrimaryKey(Object primaryKey) {
return (T) db.find(domainClass, primaryKey);
}
// TODO This might be better to be used for complete comparison if expanded
public boolean exists(T object) {
return fetchByPrimaryKey(object.getPrimaryKey()) == null ? false : true;
}
public void save(T object) {
db.save(object);
}
public void merge(T object) {
db.merge(object);
}
public void delete(T object) {
db.remove(object);
}
public void saveOrUpdate(T object) {
if (exists(object)) {
merge(object);
} else {
save(object);
}
}
public void deleteByPrimaryKey(T object) throws EntityNotFoundException {
Object primaryKey = object.getPrimaryKey();
T objectToDelete = fetchByPrimaryKey(primaryKey);
if (objectToDelete == null) {
getLogger().debug("There was no entry found with primary key: " + primaryKey);
throw new EntityNotFoundException("No entry was found with specified primary key [" + primaryKey + "]");
} else {
getLogger().debug("Deleting entry with id: " + primaryKey);
delete(objectToDelete);
}
}
@SuppressWarnings("unchecked")
public List<T> getResultList(String hql, String... parameters) {
TypedQuery<T> query = db.createTypedQuery(hql, domainClass);
for (int i = 0; i < parameters.length; i++) {
query.setParameter(i + 1, parameters[i]);
}
return query.getResultList();
}
@SuppressWarnings("unchecked")
public T getSingleResult(String hql, String... parameters) {
TypedQuery<T> query = db.createTypedQuery(hql, domainClass);
for (int i = 1; i <= parameters.length; i++) {
query.setParameter(i, parameters[i - 1]);
}
return query.getSingleResult();
}
}
3)同样地,我认为在服务中我认为我应该使用模型作为参数 公共抽象类DomainService {
protected abstract Logger getLogger();
protected final Validator validator;
protected DomainService() {
// TODO this might be needed only for insertion so instead of a class member, maybe it's better to have it as
// a method variable?
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
this.validator = factory.getValidator();
}
/**
* Inserts an entry in the database for the object passes an an argument.
*
* @param object The object representing the entry to be inserted
* @throws ValidationException When the object doesn't pass the validation. i.e. a member value is not valid based
*/
//TODO handle validation exception with message
abstract public void insert(T object) throws ValidationException;
/**
* Deletes an entry from the database. A whole object of the appropriate type is passed as an argument which
* will be used as a storage/collection of the attributes of the entry by which the deletion can occur. Different
* implementations can use these attributes to performs filtering and collection of entries to be deleted.
*
* @param object An object representing the entry to be deleted.
*
* @throws EntityNotFoundException when no entry to be deleted is found
*/
// TODO remove TransactionRequiredException, IllegalArgumentException
abstract public void delete(T object) throws EntityNotFoundException;
/**
* Returns all the entries of the table.
*
* @return a list containing objects representing all the entries in the table.
*/
abstract public List<T> fetchAll();
}
因此,在我将拥有对象的所有值的servlet中,我将构建一个成员实例,然后将其传递给 service ,这将转换它到域或使用它来更新现有的域对象(例如,它是从数据库中检索的)等等