Caliburn-Micro / Caliburn.Micro

A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
http://caliburnmicro.com/
MIT License
2.79k stars 775 forks source link

[WIP] Applying best practice #871

Open K4PS3 opened 10 months ago

K4PS3 commented 10 months ago

This pull request mainly focused on organizing the core project. You will find descriptive message in each commit.

Here is the main points:

In general the changes made here not critical for the code itself, most of the changes to benefit from the compiler capability, in Roslyn words most of the changes applied to the Trivias.

The missing part that found in <NoWarn>...</NoWarn> in .csproj of the core project, These errors needs to be fixed one-by-one with extensive testing and good consideration for the effects on others parts of the framework.

I tried to do small changes for each commit to make it easier to review. I also worked on the platform projects, but they need a lot in attention, especially with Conditional compilation.

If this pull request is acceptable, or any part of it can provide any benfit, i will be glade.

I am not native English speaker, so forgive any typos.

The error list

CA1711 // Identifiers should not have incorrect suffix

-AsyncEventHandler.cs -INotifyPropertyChangedEx.cs

Cause: EventHandler and Ex suffix.

CA1003 // Use generic event handler instances

Cause: Action instead of EventHandler, custome delegate AsyncEventHandler for EventHandler. Solution: use EventHandler delegate for Action,

CA1070 // Do not declare event fields as virtual

  • Screen.cs -> public virtual event AsyncEventHandler<ActivationEventArgs> Activated
  • Screen.cs -> public virtual event EventHandler<DeactivationEventArgs> AttemptingDeactivation
  • Screen.cs -> public virtual event AsyncEventHandler<DeactivationEventArgs> Deactivated
  • ConductorBase.cs -> public virtual event EventHandler<ActivationProcessedEventArgs> ActivationProcessed
  • PropertyChangedBase.cs -> public virtual event PropertyChangedEventHandler PropertyChanged

Cause: virtual keyword, events should not be overrding. Solution: Remove virtual keyword.

CA1052 // Static holder types should be Static or NotInheritable

  • ConductorWithCollectionAllActive.cs -> Collection::AllActive
  • ConductorWithCollectionOneActive.cs -> Collection::OneActive

CA1034 // Nested types should not be visible

  • ConductorWithCollectionAllActive.cs -> Collection::AllActive
  • ConductorWithCollectionOneActive.cs -> Collection::OneActive

Cause: Nesting public class. Solution: move class to new namespace

namespace Caliburn.Micro.Conductor.Collection {
  public class AllActive<T> : ConductorBase<T> {
  }
}
namespace Caliburn.Micro.Conductor.Collection {
  public class OneActive<T> : ConductorBase<T> {
  }
}

CA1062 // Validate arguments of public methods

Cause: arguments used without validation. Solution: validate arguments of methods, when failed, return, return null or throw.

throw new ArgumentNullException(nameof(argumentName))
throw new ArgumentException(message, nameof(argumentName))

CA2007 // Consider calling ConfigureAwait on the awaited task

This is critical issue.

read here and decide when to use ConfigureAwait(false) or ConfigureAwait(true)

CA1033 // Interface methods should be callable by child types

  • ViewAware.cs -> void IViewAware.AttachView(object view, object context)
  • Screen.cs -> async Task IActivate.ActivateAsync(CancellationToken cancellationToken)
  • Screen.cs -> async Task IDeactivate.DeactivateAsync(bool close, CancellationToken cancellationToken)

Cause: Explicit implementation of the interface methods. Solution:

  • Add public method with same signature (Will not cause breaking change).
  • Make class sealed (Breaking change).
  • Make the implementation public instead of Explicit.

CA1031 // Do not catch general exception types

This is critical issue.

Cause: catch (Exception ex). Solution: Catch Specific exception type, or rethrow the exception.

CA1716 // Identifiers should not match keywords

  • ILog.cs -> void Error(...)
  • PropertyChangedBase.cs -> public virtual bool Set<T>(...)

Cause: using of reserved keyword (Error and Set). Solution: Change the name if possible, otherwise suppress this error.

CA2008 // Do not create tasks without passing a TaskScheduler

This is critical issue.

  • DefaultPlatformProvider.cs ->
    public virtual Task OnUIThreadAsync(Func<Task> action)
      => Task.Factory.StartNew(action);

    Cause: new task without specifying TaskScheduler. Solution: read the docs and decide.

CA1849 // Call async methods when in an async method

  • ResultExtensions.cs -> result.Execute(context ?? new CoroutineExecutionContext())

Cause: There is method named 'ExecuteAsync'. Solution: ignore this error for this case, because calling ExecuteAsync will cause endless loop.

CA1822 // Mark members as static CA1812 // Avoid uninstantiated internal classes

  • SimpleContainer.cs -> FactoryFactory<T>.Create

Cause: Pure Function not accessing any instance data. Solution: Can't change this since it is used by GetInstance method with reflection, or you have to change the logic of calling this method by reflection.

vb2ae commented 10 months ago

Thank you for your pr.

vb2ae commented 10 months ago

If you merge in the latest from main it should fix the expired cert issue error when building the features and setup project

vb2ae commented 10 months ago

The code looks good. I will do a full review when you fix the issue with the build.

Thank you for adding the missing xml comments. I do like the new folder structure for the code it does make it easier to find the classes. Thank you for splitting each class into its own file.

Adding code analysis to the solution is great. I hope it catches most the issues that CodeQl found.

Question when we create the Caliburn.Micro nuget packages do the 2 code analysis packages get added as a dependency?

K4PS3 commented 10 months ago

First of all, sorry for the late response, You are welcome and I hope that my contribution will be useful.

I just pushed commit that i am Confident is building locally with MSBuild without any issues, so The CI should now success.

About your Question, the answer is NO, StyleCop package is Meta package referencing, which in turn referencing stylecop.analyzers.unstable, this package does not contain any runtime files in LIB folder, so there will not be any referencing to it at runtime, the package only contains analyzers.

I glad that you liked changes i made.

By the way, I have been and still do using your framework successfully in many projects, with conjunction with SimpleInjector.

K4PS3 commented 10 months ago

@vb2ae I just pushed another commit and hope it will fix CodeQL issues. The issues are already exist and not introduced by this pr. The 'Generic catch clause' issues still exist and needs manual fix by the framework team.

You can review the code, and give me feedback, so i can fix any issues you find this pr.

alerts

K4PS3 commented 10 months ago

Sorry, I forgot to update NameFormat in ViewModelLocatorTests.cs, the issue should be now fixed. CodeQL should be now green with 17 notes about 'Generic catch clause'.

alert2

vb2ae commented 10 months ago

thanks for fixing the codeql errors

K4PS3 commented 10 months ago

@vb2ae If you don't mind, I attached ZIP file Caliburn.Micro.git.zip with proposed changes for '.csproj' to make them more organized.

You can test them locally with 'master' branch of Caliburn.

As you see in the image, the active target framework will be shown in the root of the project, that will make it easier to recognize it. The shared XAML files will shown in its own folder. The shared code of maui shown in Maui folder in the Caliburn.Micro.Maui project, the active target shown in its own folder.

With this, as you can see, the root of the projects less distraction, even with project's nodes expanded.

These changes could be merged with this pr by another commit, and I can move the files from Caliburn.Micro.Platform to the other projects by another commit, so it will be easier to review the changes.

The power of MSBuild can make '.csproj' content smaller, I learned some tricks from the giants of MSBuild, and used here to clean '.csproj' files.

I hope that i am not mixing the things out of the order, and not breaking any rules or guides.

Looking forward for your review and comments.

folders

vb2ae commented 10 months ago

@K4PS3 thanks

KasperSK commented 10 months ago

I am intrigued about the .csproj changes looks like they could simplify maintenance of the platform projects by a lot.

vb2ae commented 10 months ago

@KasperSK are you ok with merging this?

KasperSK commented 9 months ago

Let me look it over I am not sure that I like the rearranging of the files. I have some time Monday before a flight so I will do it then.