This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I have a Generic DAO (Data Access Object), something like DAO<T extends Entity<Integer>>. I was wondering if there is any consistent way of handling these common field for all type T. As a JPA/Hibernate user I know that this can be achieved throught a Mapped Supperclass of Attributes and settping up listeners for pre-update and pre-create. However, I am not quite sure if there is a way to achieve something similar with Lightspeed. This would be a nice feature to have.
|
|
|
You can handle these via a common base class e.g.: public abstract class DbObject : Entity<int> LightSpeed includes base class fields when it loads and saves entities (except transient fields of course). There is no direct equivalent of pre-create in LightSpeed but you can override OnSaving (or hook the Saving event) and make last-minute updates at that point. You can inspect the EntityState to determine whether the entity is being created (New) or updated (Modified). If setting this up in the designer, make sure to set the inheritance type to concrete table inheritance, or use an external base class (but note external base classes do not participate in database synchronisation). Also note that LightSpeed can automatically track creation and update times for you if you set the Track Create Time and Track Update Time options (or, for hand-coded entities, implement CreatedOn and UpdatedOn fields), so you don't need to implement these yourself. |
|
|
So I guess you can do something like this: public abstract class SuperEntity : Entity<int> } public class Person : SuperEntity } public class DAO<T extends SuperEntity> public void create(T item) } public void update(T item) } } This would work without the need to have listeners, since you are explicitly setting the values of the SuperEntity.
|
|
|
Yes, exactly, though of course in this case you need to make sure your application or repository code remembers to call the create or update method at the appropriate time, and that create is not called more than once, etc. The override / listener-based approach may be safer in that regard, though it introduces other problems e.g. how to get at the user name from the OnSaving code / Save event handler, and denies you fine control over when create and update get called. |
|