Skip to content

Latest commit

 

History

History

ricaun.Revit.DI.Example

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

ricaun.Revit.DI.Example

This example project uses the ricaun.DI and ricaun.Revit.DI to create a dependency injection container to work with Revit API.

How to use

1. Create a static class to hold the container

This project uses the Host.cs implementation to hold the container.

2. Initialize the container in the OnStartup method.

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>();

3. Dispose the container in the OnShutdown method.

On the OnShutdown method you could dispose the container using.

Host.Container.Dispose();

or

this.GetContainer().Dispose();

4. Use the container to resolve the dependencies.

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!