Generically add the DoNotNotify attribute
Closed this issue · 3 comments
Hello,
Is there any way to customize the compilation process of Fody outside of the FilterType attribute ?
The FilterType can be assigned to an assembly, so it seems generic enough to fit our needs, but in order to filter the properties, we can only use the DoNotNotify attribute
My current project was using PostSharp, and using the Aspect.CompileTimeValidate method, we were
able to filter out some properties generically (only virtual properties, or only properties whose name match a given pattern, and so on)
example :
public override bool CompileTimeValidate(MethodBase method) { return IsPropertySetter(method) && !method.IsAbstract && IsVirtualProperty(method); }
Using Fody mean we would either have to
- decorate all properties where we dont want to have the PropertyChanged event raised with the DoNotNotify attribute
- alter the source of Fody to add our code, then use the modified assembly...
Is there any other way to achieve the desired result, or do you plan to later add such a feature ?
Regards
@FredericFait unfortunately it is not possible to execute code inside at build/weaving time. and no there is no intention to add this feature
but what is the negative side effect of adding INPC being added to a few extra properties?
Well, in our case, some generic methods are fired before and after the propertyChanged event is actually raised
Using PostSharp, we can restrict this code to some specified properties (those which are virtual and non abstract), and it will be applied to all required classes (using the filter, like in Fody)
So when developer create a new class, he doesn't have to write a single line of code, or to differentiate beetween some properties
And because this discrimination is done during compilation, there is no impact on performance.
To reproduce this behavior easily using Fody, we have 2 choices :
- Ask to all developers to manually add the DoNotNotify attribute to every abstract or non virtual properties (to all existing and future classes of our project, something like 400+ files)
- Intercept the call during execution, then test if the property is abstract or non virtual and, if so, prevent the raise of the event.
The first choice imply a lot of reworking of code, we may miss some classes/properties, or add it where it shouldn't be, and it is prone to errors in the future
The second choice imply reflection during execution, so it will degrade performances
The second choice is not acceptable as it is a production software, and the first choice doesn't seems robust enough, and imply a lot of rework
So I'm currently thinking about :
- creating a new PropertyFilterAttribute attribute (like the FilterTypeAttribute), allowing us to add some filtering to the properties name
- Add a new method ProcessPropertyFilterTypeAttributes(); after the ProcessFilterTypeAttributes(); method, inside which we would filter out the properties based on their name just like how you filter out the classes name
- Either directly alter the CleanDoNotNotifyTypes() method, in order to remove the properties we want to excluse (abstract or non virtual), or find a way to provide an overridable method to let us handle it in the project code (like in Postsharp) but this seems more tricky
All this would let us achieve the desired outcome without any need for code rework or performance impact, but it also imply modifying Fody sources. So it also means that we either won't be able to work with the future releases of Fody, or we will have to reapply those changes to each future release
so i can better understand your scenario. can u submit a PR with a failing test that illustrates exactly what you cannot currently achieve.