Keyed services, Hastlayer.com is running on Orchard Core - This week in Orchard (29/12/2023)

gabor.domonkos

Adding helpful methods for ContentPart, ContentType builders, and ContentItemExtensions, introducing keyed services, and a case study about modernizing and migrating Hastlayer.com to Orchard Core are the topics for the last week of this year. Let's check them out in detail!

Orchard Core updates

Adding helpful methods for ContentPart, ContentType builders and ContentItemExtensions

This is about introducing several new useful methods and extension methods for Orchard Core to make the developer's life easier. Let's see the changes!

New methods in ContentPartDefinitionBuilder

  • ContentPartDefinitionBuilder WithField<TField>(string fieldName)
  • ContentPartDefinitionBuilder WithField<TField>(string fieldName, Action<ContentPartFieldDefinitionBuilder> configuration)
  • Task<ContentPartDefinitionBuilder> WithFieldAsync<TField>(string fieldName, Func<ContentPartFieldDefinitionBuilder, Task> configuration)

New methods in ContentTypeDefinitionBuilder

  • ContentTypeDefinitionBuilder WithPart<TPart>()
  • ContentTypeDefinitionBuilder WithPart<TPart>(string name)
  • ContentTypeDefinitionBuilder WithPart<TPart>(string name, Action<ContentTypePartDefinitionBuilder> configuration)

New extension methods in ContentItemExtensions

  • bool TryGet<TPart>(this ContentItem contentItem, out TPart part)
  • bool TryGet<TPart>(this ContentItem contentItem, string name, out TPart part)
  • bool TryGet(this ContentItem contentItem, Type contentElementType, string name, out ContentElement part)

New extension method in EntityExtensions

  • bool TryGet<T>(this IEntity entity, out T aspect)

But let's not just list these here, see a little example too! On this screen, you can see the migration of the SeoMetaPart where we configured this part and added some fields to it. You can see that this part contains a field called DefaultSocialImage and by using the OfType method, we can set the type of the field (MediaField in this case). But by using one of the new overloads of the WithField method, we don't need to use the OfType method, we can pass a typed value, the type of the field itself.

Utilizing the new WithField method

Keyed services

The recently released .NET 8 introduces keyed service dependency injection container support. Currently, we don't support it in Orchard Core and the goal of this feature is to allow us to use keyed services in Orchard. Here you can see that in this CloneSingleton extension method, we need to understand whether is it a keyed service or not, and based on the ServiceKey object (which gives the key of the services, if applicable) of the ServiceDescriptor (which describes the service with its service type, implementation, and lifetime) we can decide which overload of the ClonedSingletonDescriptor method do we need to call to add our service to the ServiceCollection.

Keyed Services support

News from the community

Case study: Hastlayer.com is running on Orchard Core

The new website for Hastlayer, the .NET hardware accelerator originally built on Orchard 1, now runs on Orchard Core! Check out our case study here about the migration! We are happy with the results and the additions to our open-source modules, and stay tuned, as we keep the migrations rolling.

Modernization and Orchard Core Migration of hastlayer.com

This is an excellent time if you're considering migrating your Orchard 1 website to Orchard Core. Should you have any questions or encounter challenges, don't hesitate to reach out to us. We're always ready to bring our expertise to your unique project needs. Contact us today, and let's start making your Orchard Core website even better!

Orchard Dojo Newsletter

Lombiq's Orchard Dojo Newsletter has 484 subscribers! We have started this newsletter to inform the community around Orchard of the latest news about the platform. By subscribing to this newsletter, you will get an e-mail whenever a new post is published to Orchard Dojo, including This week in Orchard of course.

Do you know of other Orchard enthusiasts who would like to read our weekly articles? Tell them to subscribe here!

If you are interested in more news about Orchard and the details of the topics above, don't forget to check out the recording of this Orchard meeting!