Skip to content

Code-level tools for Umbraco

License

Notifications You must be signed in to change notification settings

umbraco/Umbraco-Code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Provides code-level tools for Umbraco

MapAll

Helps replacing AutoMapper with static code, without missing properties. Adding the Umbraco.Code NuGet package to a project adds a Roslyn code analyzer and fix.

The following code:

// Umbraco.Code.MapAll
public void Map(SomeType source, OtherType target)
{}

Raises an error and fails to compile, if not all assignable properties of target (from OtherType and parents) are assigned a value. The fix can be invoked (ctrl + ;) to generate the missing properties. When the fix can find a corresponding property in source, it generates the assignment (corresponding by name, whatever the type). When no corresponding property can be found, the fix generates an assignment to default with a comment.

// Umbraco.Code.MapAll
public void Map(SomeType source, OtherType target)
{
	target.Value1 = source.Value1;
	target.Value2 = default; // fixme
}

It is possible to ignore some properties, eg:

// Umbraco.Code.MapAll -PropertyToIgnore -Another
public void Map(SomeType source, OtherType target)
{}

Volatile

Allows for classes, methods, members, interfaces, enums and attributes to be marked as volatile with an attribute. Reources marked with volatile, or from a class marked as volatile, will throw an error and fail to compile. The error can be suppressed to a warning with the assembly level attribute UmbracoSuppressVolatileAttribute.

This is intented to be used for resources that were previously marked as internal, typically because the may break in the future, but are still useful in some aspect, typically testing where it doesn't matter if a method breaks.

Marking a method as volatile looks like this:

    public class DemoClass
    {
        [UmbracoVolatile]
        public void VolatileMethod()
        {
            // Do volatile things here.
        }

    }

Whenever DemoClass.VolatileMethod is invoked there'll be raised an UmbracoCodeVolatile error, to suppress it to a warning use the assembly level UmbracoSuppressVolatileAttribute:

[assembly: UmbracoSuppressVolatile]
namespace VolatileDemo
{
    public class DemoClass
    {
	[UmbracoVolatile]
        public void VolatileMethod()
        {
           // Volatile things here...
        }

    }

    public class DemoClass2
    {
        public void Test()
        {
            var testClass = new DemoClass();
            testClass.VolatileMethod();
        }
    }
}

Now there'll only be raised a warning even though DemoClass.VolatileMethod is marked as volatile.

The same thing goes for objects, if you do something like this:

    [UmbracoVolatile]
    public class DemoClass
    {
        public void VolatileMethod()
        {
            // Do volatile things here.
        }

    }

All of DemoClasses methods and members will be marked as volatile.

For more examples see the unit tests.

The Attributes

It's worthwile noting that the attributes are compared by name and not by type.

This means that it's not needed to use the attributes that are included in this project (the namespace of analyzers is not accecible), any attribute named UmbracoVolatileAttribute or UmbracoSuppressVolatileAttribute will do the trick.

Sources and References

Inspired by, and probably stealing code from:

Some pages:

Some projects: