MEF: Importing on Fields - mef

Is it recommended that we place an Import on a property instead of a field? I tried it on a field and it is working but Resharper is telling me a warning that the field was never initialized.

ReSharper doesn't recognize that MEF will be setting the variable and since there is no guarntee that MEF will be setting the variable (example if it isn't put into a container for example), so it is reasonable for ReSharper to warn about this. You can either ignore it or simply initialize the field to null (or default(T)).
As for whether or not you should use a property or field I think using a field is fine (assuming it is not public). I generally reserve properties for things I want to expose publicly. One special case to consider here is that there are some issues having Imports on private members in low trust scenarios like SL or paritial trust because MEF uses reflection and you cannot use private reflection in some of those scenarios.

Related

Globally Tag EF Core Queries with ".TagWithCallSite()"

With the release of the .TagWithCallSite() method in EF Core 6.0 I was wondering if there is a way to apply this globally on every query run via a DbContext in some way?
It would be much better to apply this across the whole project without having to put it on each query individually.
TagWithCallSite accepts parameters marked with CallerFilePathAttribute and CallerLineNumberAttribute which are filled in by compiler (or manually if needed) during build so it is impossible to set up globally.
No, you cant't do that.
When you explicitly define TagWithCallSite(), complier automatically fills default parameters filePath and lineNumber. It is not possible to define that for all queries because compiler do not store such information in Expression Tree.

What does it mean , when we say private instance variable for its own library?

I am beginner to flutter/dart and has read that we don't have public/private/protected access specifier for dart but if we want to make private instance variable, we can make the use of underscore(_) operator but it will not make the variable private to the class but to its own library, So what does it actually mean ?
Dart privacy is indeed only on a per library basis.
A name which begins with _ is a library private name. A private identifier, like _tmp, is considered a different name than a similarly spelled identifier, also _tmp, which occurs in a different library.
That means that code in a different library cannot access the private name _tmp because it can't even express it. If it tries to write _tmp, it can only refer to a private name of its own library instead.
The choice of embedding access control in the name makes sense when you remember that Dart has dynamic invocations. If you write dynamic x = ...; x.foo();, then this should call the foo method of x if there is one. To do this efficiently, it would be too much of an overhead if each dynamic invocation should also figure out where the name originally comes from and whether it's accessible to the caller. Dart avoids this overhead by making all public names visible, and all private names inexpressible.
The goal of privacy is to separate public interface API from internal implementation API, and to avoid naming conflicts.
You can write your private names without fear that they conflict with someone else's names, and without risking someone thinking they are meant for public use.
Dart does not try to protect code from other code in the same library. It's supposed to be the same author anyway, so they can be trusted to use the API responsibly (and if not, it's on themselves).
What it means for you as a user is: The library is the unit of code. You can make libraries which contains only a single class. The library privacy is class privacy for that class. Or you can create libraries with many classes and top-level functions which can all see each other's private names.
That means you should base your modularity on the need of classes to share implementation, and not really anything else. You can always build a larger API by exporting other libraries.
When creating a Pub package, I would create your own internal libraries, inside the lib/src/ directory, giving them whatever size is convenient, and then export your public API from the package main file in lib/.
a library/module/model refferes to the class itself, so any variables/methods are only accessable to the class itself. any class from the outside world trying to access this property, will not be able to. basically its own library means the class itself

Colliding aspects in postsharp

I am using the PostSharp solution for INotifyPropertyChanged by decorating my business classes with the [NotifyPropertyChanged] attribute.
All works fine.
Now I wrote a custom aspect that handles property changes so that I get some custom flags set when some special properties change. This aspects is named [HandlePropertyChanged] and works when used alone.
Now I try to use both aspects in combination. As I read on the PostSharp page I can manually order them to ensure a fixed order by using
[NotifyPropertyChanged(AspectPriority = 0)]
[HandlePropertyChanged(AspectPriority = 1)]
In this case, I can build my solution, but because "NotifyPropertyChanged" runs before "HandlePropertyChanged", the changes on my properties are already done and the custom logic does not run correctly.
If I try this
[HandlePropertyChanged(AspectPriority = 0)]
[NotifyPropertyChanged(AspectPriority = 1)]
my build fails with the error at the bottm of the text (see below).
Best would be to simply do what NotifyPropertyChanged does in my custom aspect and forget about the PostSharp aspect
Is this possible?
0: Error C:\Source\WAVE\WAVE.Data.Contracts\Entities\Base\EntityBase.cs (17,16) PS0115: Conflicting aspects on "TopMotive.WAVE.Data.Contracts.Entities.Base.EntityBase`1": according to aspect dependencies, transformation "Instantiation of aspect PostSharp.Patterns.Model.NotifyPropertyChangedAttribute" should be located both before and after transformation "Instantiates binding collection for field "PostSharp.Patterns.Model.NotifyPropertyChangedAttribute/LocationBindings".".
This bug is fixed in PostSharp 5.0.52 and PostSharp 6.0.16 RC.
Try superior and free alternative: Stepen Cleary's Calculated Properties.
https://github.com/StephenCleary/CalculatedProperties/blob/master/README.md
I used both in production and found it to be far better than PostSharp's aspect.
Also from PostSharp docs:
"If a property getter calls a virtual method from its class or a delegate, or references a property of another object (without using canonical form this.field.Property), PostSharp will generate an error because it cannot resolve such a dependency at build time. The same limitations apply when your property getter contains complex data flows, such as loops, or calls to methods (except property getters) of other classes.
When this happens, you can either refactor your code so that it can be automatically analyzed by PostSharp, or you can take over the responsibility for analyzing the code"
None of those limitations apply to Calculated Properties. It can do loops, virtual methods, LINQ to objects, basically any runtime dependencies that you can imagine doesn't matter how indirect. Dependency graph rewires itself at runtime and just works without any ceremony. They are also fast.

SugarCRM $beanFiles array modification best practices

In SugarCRM, you can create your custom modules (e.g. MyModule) and they are kept in /modules just like stock objects, with any default metadata, views, language files, etc. For a custom module MyModule, you might have something like:
/modules/MyModule/MyModule.class.php
/modules/MyModule/MyModule.php
/modules/MyModule/language/
/modules/MyModule/metadata
And so on, so that everything is nicely defined and all the modules are kept together. The module becomes registered with the system by a file such as /custom/Extension/application/Include/MyModule.php with contents something like:
<?php
$beanList['MyModule'] = 'MyModule';
$beanFiles['MyModule'] = 'modules/MyModule/MyModule.php';
$moduleList[] = 'MyModule';
Obviously, the $beanFiles array references where we can find the base module's class, usually an extension of the SugarBean object. Recently I was advised that we can adjust that file's location for the sake of customization, and it makes sense to a degree. Setting it like $beanFiles['MyModule'] = 'custom/modules/MyModule/MyModule.php'; would allow us to access the base class via Module Loader even if the security scan tool prevents core file changes, and this would also allow us to not exactly extend, but replace stock modules like Accounts or Calls, without modifying core files and having system upgrades to wipe out the changes.
So here's my question: what is the best practice here? I've been working with SugarCRM pretty intensely for several years and this is the first time I've ever been tempted to modify the $beanFiles array. My concern is that I'm deviating from best practice here, and also that somehow both files modules/MyModule/MyModule.php and custom/modules/MyModule/MyModule.php could be loaded which would cause a class name conflict in PHP (i.e. because both classes are named MyModule...). Obviously, any references to the class would need to be updated (e.g. an entryPoint that works with this module), but am I missing any potential ramifications?
Technically it should be fine, but I can see how it could be possible that both the core version and your version could conflict if both are referenced. It all depends on the scenario, but I prefer to extend the core bean and find somewhere in the stack where I can have my custom version used in place of the core bean. I wrote up an example a couple of years ago here: https://www.sugaroutfitters.com/blog/safely-customizing-a-core-bean-in-sugarcrm
For most use-cases, there's a way to hijack Sugar to use your bean at a given point.
If you can't get around it you can always grep to see where the core module is explicitly being included to ensure that there won't be conflict down the road.

Does GWT deferred binding return singletons of the same type within the scope of a single EntryPoint?

Let's say I have a number of GWT modules which are used as libraries, and one module with an entry point which inherits all of the library modules.
Each of the submodules needs to access a single instance of SomeClass.
If I call GWT.create(SomeClass.class) in modules A & B, do I get the same instance? If so, is this guaranteed?
No. GWT.create(SomeClass.class) compiles to new SomeClass(), unless there is a rebind rule of some kind - a replace-with or a generate-with rule will cause this to instead invoke the default constructor of whatever type is selected by those rules.
This means that GWT.create is not a suitable way to provide access to a singleton instance. Instead, consider some DI tool like Gin, or manual DI by always passing around the same instance. It is also possible to use the static keyword to keep a single instance where all code compiled into the same app can reference it.