This example project uses the ricaun.DI and ricaun.Revit.DI to create a dependency injection container to work with Revit API.
This project uses the Host.cs implementation to hold the container.
Add the Revit dependencies in the container using.
Host.Container.AddRevitSingleton(application);
If your IExternalApplication
class have IHost
you could add it to the container using.
var container = this.GetContainer();
container.AddRevitSingleton(application);
And you could add the dependencies of your project like.
container.AddScoped<IMessageService, MessageService>();
On the OnShutdown
method you could dispose the container using.
Host.Container.Dispose();
or
this.GetContainer().Dispose();
To resolve the dependencies you could use the Host.Container
to resolve the dependencies.
var messageService = Host.Container.Resolve<IMessageService>();
or if your class have IHost
you could use the Resolve
method to resolve the dependencies.
var messageService = this.Resolve<IMessageService>();
The Command.cs has a example of how to use the container to resolve a custom ICommand
in the Command<T>
.
Do you like this project? Please star this project on GitHub!