-
Notifications
You must be signed in to change notification settings - Fork 48
How to provide your own reusable services through an Autofac registration module
If you want to be able to access your modules' Services through your Service Locator, you'll need to provide an Autofac registration module.
Your assembly file name should respect the pattern of the AppName that was fed into the SharePointServiceLocator. So, for example, if the AppName is "Company.Project", then only registration modules that belong to DLL that has a filename that follows the pattern "Company.Project*.dll".
Make sure that your Module's WSP project is configured to deploy the Core.dll to the GAC, of course.
Register your types using a class that derives from the Autofac Module
class.
using Autofac;
using IFC.IntactNet.Navigation.Core.Repositories;
using IFC.IntactNet.Navigation.Core.Services;
using IFC.IntactNet.Navigation.Contracts.Repositories;
using IFC.IntactNet.Navigation.Contracts.Services;
namespace Company.Project.Navigation.Core.RegistrationModules
{
/// <summary>
/// The navigation registration module
/// </summary>
public class NavigationRegistrationModule : Module
{
/// <summary>
/// Loads the specified builder.
/// </summary>
/// <param name="builder">The builder.</param>
protected override void Load(ContainerBuilder builder)
{
// Repositories
builder.RegisterType<AdminQuickLinkRepository>().As<IAdminQuickLinkRepository>();
// Services
builder.RegisterType<QuickLinkService>().As<IQuickLinkService>();
// Breadcrumb
builder.RegisterType<SectorPageRepository>().As<ISectorPageRepository>();
builder.RegisterType<BreadcrumbService>().As<IBreadcrumbService>();
}
}
}
From then on, using ProjectContainer.Current.Resolve<IQuickLinkService>()
from any WSP will work. Also, other modules' Core libraries will now be able to depend on Company.Project.Navigation.Contracts.dll
and add IQuickLinkService
to their Services' constructor's dependencies. In effect, we enable easy cross-module collaboration through the service locator.
When you register an interface and its implementation using the default syntax:
builder.RegisterType<QuickLinkService>().As<IQuickLinkService>();
What you are saying to Autofac is
Every time someone tries to
.Resolve
aIQuickLinkService
, please return a new instance ofQuickLinkService
i.e. Autofac will call new QuickLinkService(...)
everytime the dependency injection tree requests a IQuickLinkService
.
This behavior is called transient lifetime.