HI,
I made some test on object modification and deletion. I didn't find a way to modify an object outside the Service<> method scope.
For example I would like to create an object inside a service<> and return it. Some class use the object and modify it. After the modification a method update is call on the service<> and the object is stored in its state.
Is there a way to do this?
I didn't want to have all modifications method inside a service<> class. When an object is modified anywhere in my code I want the modifications to be commited in the database.
Regards
Philippe Delrieu
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
using the unit test TransactionBoundaryTests I develop this code :
The service<> that manage db access
[Transaction(TransactionOption.Required)]
public sealed class OBJNETdbaccess : Service<OBJNETdbaccess> {
public Demande createDemande() {
//create a demande
Demande newDemande = new Demande(9).Manage<Demande>();
return (Demande)ProxyFactory.CreateTransparentProxy(newDemande);
}
public void updateDemandeInDB(Demande demande) {
PersistenceFacade.Persist(demande);
}
public void deleteDemande(Demande demande) {
demande.Delete();
}
}
The test method that create/modify and delete object outside the service<>
public void testDemandeMapping() {
//create a demande
Demande newDemande = dbaccess.createDemande();
newDemande.Patientid = 12;
newDemande.Sampleid = 10;
dbaccess.updateDemandeInDB(newDemande);
dbaccess.deleteDemande(newDemande);
}
It works.
I would like to know if it's the right way to do because from this example I will develop a pattern for all my persistent object. I didn't made extensible test and I would like to avoid surprise when everything is developped.
regards
Philippe Delrieu
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
public Demande createDemande()
{
//create a demande
Demande newDemande = new Demande(9).Manage<Demande>();
return (Demande)ProxyFactory.CreateTransparentProxy(newDemande);
}
to simply:
public Demande createDemande()
{
//create a demande
return new Demande(9).Manage<Demande>();
}
The call to CreateTransparentProxy() is deprecated and not necessary (I just updated the unit test also...). Manage does all that you need.
You should change:
public void updateDemandeInDB(Demande demande)
{
PersistenceFacade.Persist(demande);
}
to:
public void updateDemandeInDB(Demande demande)
{
BindUtil.BindToTransaction(demande);
}
The call to PersistenceFacade.Persist() is redundant as you are already within a transactional service that has initiated a transaction. All you need to do is bind the object to the transaction. PersistenceFacade is just a helper class in case you don't want to create your own transactional service class.
deleteDemande() looks good.
You might want to change the name of your service class to 'DomainObjectsDbAccess' because OJB.NET is a defunct project. :-)
Also, from you parenthesis and your method casing, looks like you come from the Java world. True? ;-)
Regards,
Richard
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I finalize a base class that encapsulate ObjectDomaine main call. I will use it to facilitate ObjectDomain intagration in my projet.
Perhaps It can be use full for somebody.
[Transaction(TransactionOption.Required)]
public sealed class DomainObjectsDbAccess : Service<DomainObjectsDbAccess> {
public EditableObject CreateObjectInDB<EditableObjectType>(EditableObject obj) where EditableObjectType : EditableObject {
PersistenceFacade.Persist(obj); //call to persist because the object is created outside the Service method scope.
return obj.Manage<EditableObjectType>();
}
public void UpdateObjectInDB(EditableObject obj) {
BindUtil.BindToTransaction(obj);
}
public void DeleteObjectInDB(EditableObject obj) {
obj.Delete();
}
public EditableObject FindObjectInDBWithPrimaryKey<EditableObjectType>(object primaryKey) where EditableObjectType : EditableObject {
return (EditableObject)QueryFacade.FindObjectByPrimaryKey<EditableObjectType>(primaryKey);
}
public List<EditableObjectType> FindAllDBObjects<EditableObjectType>() where EditableObjectType : EditableObject {
Criteria criteria = new Criteria();
// criteria.AddOrderBy(Types.Demande.Field._sampleid);
return QueryFacade.FindObjectSet<EditableObjectType>(criteria);
}
}
Exemple of use :
Demande newDemande = new Demande(9);
newDemande = (Demande) dbaccess.CreateObjectInDB<Demande>(newDemande);
newDemande.Patientid = 10;
newDemande.Sampleid = 11;
dbaccess.UpdateObjectInDB(newDemande);
List<Demande> list = dbaccess.FindAllDBObjects<Demande>();
foreach (Demande dem in list) {
dem.Sampleid = 36;
dem.Patientid = 35;
dbaccess.UpdateObjectInDB(dem);
}
List<Demande> list = dbaccess.FindAllDBObjects<Demande>();
foreach (Demande dem in list) {
dbaccess.DeleteObjectInDB(dem);
}
Effectivly a come from the java world. I am new to C# that why I still have some specific habit.
Thank you for your help.
Philippe Delrieu
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
HI,
I made some test on object modification and deletion. I didn't find a way to modify an object outside the Service<> method scope.
For example I would like to create an object inside a service<> and return it. Some class use the object and modify it. After the modification a method update is call on the service<> and the object is stored in its state.
Is there a way to do this?
I didn't want to have all modifications method inside a service<> class. When an object is modified anywhere in my code I want the modifications to be commited in the database.
Regards
Philippe Delrieu
Hi,
using the unit test TransactionBoundaryTests I develop this code :
The service<> that manage db access
[Transaction(TransactionOption.Required)]
public sealed class OBJNETdbaccess : Service<OBJNETdbaccess> {
public Demande createDemande() {
//create a demande
Demande newDemande = new Demande(9).Manage<Demande>();
return (Demande)ProxyFactory.CreateTransparentProxy(newDemande);
}
public void updateDemandeInDB(Demande demande) {
PersistenceFacade.Persist(demande);
}
public void deleteDemande(Demande demande) {
demande.Delete();
}
}
The test method that create/modify and delete object outside the service<>
public void testDemandeMapping() {
//create a demande
Demande newDemande = dbaccess.createDemande();
newDemande.Patientid = 12;
newDemande.Sampleid = 10;
dbaccess.updateDemandeInDB(newDemande);
dbaccess.deleteDemande(newDemande);
}
It works.
I would like to know if it's the right way to do because from this example I will develop a pattern for all my persistent object. I didn't made extensible test and I would like to avoid surprise when everything is developped.
regards
Philippe Delrieu
Hi Philippe,
You should change:
public Demande createDemande()
{
//create a demande
Demande newDemande = new Demande(9).Manage<Demande>();
return (Demande)ProxyFactory.CreateTransparentProxy(newDemande);
}
to simply:
public Demande createDemande()
{
//create a demande
return new Demande(9).Manage<Demande>();
}
The call to CreateTransparentProxy() is deprecated and not necessary (I just updated the unit test also...). Manage does all that you need.
You should change:
public void updateDemandeInDB(Demande demande)
{
PersistenceFacade.Persist(demande);
}
to:
public void updateDemandeInDB(Demande demande)
{
BindUtil.BindToTransaction(demande);
}
The call to PersistenceFacade.Persist() is redundant as you are already within a transactional service that has initiated a transaction. All you need to do is bind the object to the transaction. PersistenceFacade is just a helper class in case you don't want to create your own transactional service class.
deleteDemande() looks good.
You might want to change the name of your service class to 'DomainObjectsDbAccess' because OJB.NET is a defunct project. :-)
Also, from you parenthesis and your method casing, looks like you come from the Java world. True? ;-)
Regards,
Richard
I finalize a base class that encapsulate ObjectDomaine main call. I will use it to facilitate ObjectDomain intagration in my projet.
Perhaps It can be use full for somebody.
[Transaction(TransactionOption.Required)]
public sealed class DomainObjectsDbAccess : Service<DomainObjectsDbAccess> {
public EditableObject CreateObjectInDB<EditableObjectType>(EditableObject obj) where EditableObjectType : EditableObject {
PersistenceFacade.Persist(obj); //call to persist because the object is created outside the Service method scope.
return obj.Manage<EditableObjectType>();
}
public void UpdateObjectInDB(EditableObject obj) {
BindUtil.BindToTransaction(obj);
}
public void DeleteObjectInDB(EditableObject obj) {
obj.Delete();
}
public EditableObject FindObjectInDBWithPrimaryKey<EditableObjectType>(object primaryKey) where EditableObjectType : EditableObject {
return (EditableObject)QueryFacade.FindObjectByPrimaryKey<EditableObjectType>(primaryKey);
}
public List<EditableObjectType> FindAllDBObjects<EditableObjectType>() where EditableObjectType : EditableObject {
Criteria criteria = new Criteria();
// criteria.AddOrderBy(Types.Demande.Field._sampleid);
return QueryFacade.FindObjectSet<EditableObjectType>(criteria);
}
}
Exemple of use :
Demande newDemande = new Demande(9);
newDemande = (Demande) dbaccess.CreateObjectInDB<Demande>(newDemande);
newDemande.Patientid = 10;
newDemande.Sampleid = 11;
dbaccess.UpdateObjectInDB(newDemande);
newDemande = (Demande) dbaccess.FindObjectInDBWithPrimaryKey<Demande>(newDemande.PrimaryKey);
newDemande.Patientid = 23;
newDemande.Sampleid = 24;
dbaccess.UpdateObjectInDB(newDemande);
List<Demande> list = dbaccess.FindAllDBObjects<Demande>();
foreach (Demande dem in list) {
dem.Sampleid = 36;
dem.Patientid = 35;
dbaccess.UpdateObjectInDB(dem);
}
List<Demande> list = dbaccess.FindAllDBObjects<Demande>();
foreach (Demande dem in list) {
dbaccess.DeleteObjectInDB(dem);
}
Effectivly a come from the java world. I am new to C# that why I still have some specific habit.
Thank you for your help.
Philippe Delrieu
Thanks Philippe for the sample code.