11-14-2013 02:09 AM
Hi there, I'm curious if it's possible to overwrite or extend the 'Delete' method. Or maybe to define own 'global' methods like Save() and Delete().
The reason why I'm curious is the following:
I'm supposed to rather flag something as 'deleted' instead of really deleting something. Since it needs a lot of patience to implement a property called 'Deleted' and a method called 'DeleteRestore' on every single entity and it's also hard to maintain if something somewhere changes, I'd thought of implementing the property only on the entities necessary and then check for it's existence in the 'DeleteRestore' method. If it exists, it gets flipped; if not, it calls Delete() on the entity.
Currently I'm using an external method like:
ExternalLibrary.GenericMethods.DeleteRestore(object entity)
{
Type entityType = entity.GetType();
bool? deleted;
try
{
PropertyInfo pi = entityType.GetProperty("Deleted");
deleted = ((bool?)pi.GetValue(entity, null)) ?? false);
pi.SetValue(entity, !deleted, null);
MethodInfo mi = entityType.GetMethod("Save");
mi.Invoke(entity, new object[0]);
}
catch { }
}
to achieve this. But I don't like it that much. I'd rather like to use a 'global' method, so I can call something like
anyEntity.DeleteRestore();
// or
anyEntity.FlagDeleted(); //and
anyEntity.FlagRestored();
instead of
ExternalAssembly.GenericMethods.DeleteRestore(anyEntity)
Is there a way to achieve this?
Solved! Go to Solution.
11-14-2013 02:50 AM
The best way to do this would be to evaluate modifying the Code Templates that generate the entity classes.
Check the code templates node in the Application Architect.
John Perona
Director, Infor CRM DevelopmentInfor CRM
11-14-2013 08:37 AM
Okay. I took a look at the template 'Default-Class-SalesLogix'. Looks hard. I've never worked with code templates, yet, but I'm willing to bite through it.
If I understand correctly, these are so called 'T4' templates (these ones: http://msdn.microsoft.com/en-us/library/vstudio/bb126445.aspx). Is there any slx-specific documentation on these templates?
Anyway I'm just going to start and play around a little bit and post more questions when I'm facing any problems.
11-14-2013 11:13 AM - edited 11-15-2013 04:02 AM
I made a little change to the template:
<# OrmEntityProperty deleted = entity.GetPropertyByPropertyName("Deleted"); #> <# if (deleted != null) #> <# { #> public override void Delete() { // Put the magic here } public override void Restore() { // Put some other magic here } <# } #> <# else #> <# { #> public override void Delete() { // The current magic happens here
} <# } #>
This looks really nice and works like a charm. But now I'm starting to realize how big this task is:
Not only can't I imagine right now, how to change the value of the property and then save the entity 'from within the template'. You'll also have to change every single OnBeforeDelete() and OnAfterDelete() BusinessRules (especially those directly deleting rows from the database, I mean wth?) and/or at least put the property 'Deleted' on every single entity (which would be easier, if you could tell the 'New Entity Wizard' to do this automatically).
I think I'm putting this off until my C# skills and understanding of SLX are better and the task is getting a higher priority
But thank you for the little excursion to code templates. I preciate it and think I'll dig deeper in the future. Too bad there're more important things to do at the moment, but I might get back to implementing that next year.