patternjavaModerate
DataLayer Interfaces Genericized
Viewed 0 times
interfacesdatalayergenericized
Problem
I have some concerns on my refactoring for the data layer in my current Project. Just for a small info, I am currently building a CRM as "training JEE application". But enough of talk, let's talk code!
IService.java: (7 lines, 160 bytes)
IListService.java: (8 lines, 169 bytes)
IDependentListService.java: (8 lines, 225 bytes)
IAddressService.java: (7 lines, 199 bytes)
IContactPersonService.java: (7 lines, 167 bytes)
IContractService.java: (9 lines, 234 bytes)
ICustomerService.java: (9 lines, 220 bytes)
IProductService.java: (10 lines, 262 bytes)
IProjectService.java: (9 lines, 228 bytes)
IUserService.java: (7 lines, 129 bytes)
Update (Code):
The code for the IUserService changed a bit, after reviewing the implementation of the CustomerService:
IUserService.java:
```
public interface IUserService extends IListServi
IService.java: (7 lines, 160 bytes)
public interface IService {
public void update(T update);
public void delete(long id);
public T getById(long id);
}IListService.java: (8 lines, 169 bytes)
public interface IListService extends IService {
public void add(T add);
public List getAll();
}IDependentListService.java: (8 lines, 225 bytes)
public interface IDependentListService extends IService {
void add(T entity, long masterRecordId);
List getAllByMasterRecordId(long masterRecordId);
}IAddressService.java: (7 lines, 199 bytes)
public interface IAddressService extends IDependentListService{
public void promoteToMainAddress(long addressId);
}IContactPersonService.java: (7 lines, 167 bytes)
public interface IContactPersonService extends IDependentListService{
}IContractService.java: (9 lines, 234 bytes)
public interface IContractService extends IDependentListService {
public List getAllContractsByLoggedInUser();
}ICustomerService.java: (9 lines, 220 bytes)
public interface ICustomerService extends IListService {
}IProductService.java: (10 lines, 262 bytes)
public interface IProductService extends IListService {
public List getAllCurrentProducts();
public List getAllArchivedProducts();
}IProjectService.java: (9 lines, 228 bytes)
public interface IProjectService extends IDependentListService{
public List getAllProjectsByLoggedInUser();
}IUserService.java: (7 lines, 129 bytes)
public interface IUserService extends IListService{
}Update (Code):
The code for the IUserService changed a bit, after reviewing the implementation of the CustomerService:
IUserService.java:
```
public interface IUserService extends IListServi
Solution
This answer will reflect the naming convention at my work.
Naming Convention
Prefixing
Prefixing
If you only use
Naming of methods
Those methods are very good! If you can keep clear names like those, using your services will be easy. I like the way you named all you method!
Delete
You could return the object that has been deleted. It could be useful if you want to show the information of the item that has been deleted. The client could decide if he wants to use the object or not.
Edit: In fact, you should not return an object since you're just working with
Naming Convention
Prefixing
I to all your interface seems superfluous, since we use a simple name like ContractService. The implementation class would have Impl suffixed to the name of the service like : ContractServiceImpl. Normally you will use DI to inject the implementation where you need it, so you will only see the interface name.Prefixing
I seems odd to me if I'm looking at the Java collections API where you have : List, Map, etc. If you only use
ContractService as the name of the implementation, it could not be clear at first read that you are using an implementation directly and not the interface. Using Impl as a suffix make it clear that it's the implementation, so if you see it in your code, it can show that something smelly is going on.Naming of methods
public interface IProductService extends IListService {
public List getAllCurrentProducts();
public List getAllArchivedProducts();
}Those methods are very good! If you can keep clear names like those, using your services will be easy. I like the way you named all you method!
Delete
public void delete(long id);You could return the object that has been deleted. It could be useful if you want to show the information of the item that has been deleted. The client could decide if he wants to use the object or not.
Edit: In fact, you should not return an object since you're just working with
long id and not the object directly. If the signature was public void delete(T delete) this would have been a good advice, but with the current signature, you would have a to retrieve the object, delete it and then return the deleted object.Code Snippets
public interface IProductService extends IListService<Product> {
public List<Product> getAllCurrentProducts();
public List<Product> getAllArchivedProducts();
}public void delete(long id);Context
StackExchange Code Review Q#46194, answer score: 10
Revisions (0)
No revisions yet.