Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registry equivalent in Unity

Is there any equivalent to the Registry class from StructureMap in Unity?

I like to think about a layer/component/library to configure it self - thus populating the container. So the "parent" layer will just need to know about the Registration class.

like image 502
Michael Avatar asked Mar 19 '10 13:03

Michael


1 Answers

No, there isn't. In our current project we have manually mimic'd the concept of a Registry although our implementation isn't nearly as powerful as a StructureMap Registry.

If all you are wanting is modularized container configuration, what you could do is create a marker interface (maybe IRegistry) and then have your application scan for IRegistry classes. With Unity, you can resolve classes that haven't been registered into Unity yet, so you could simply resolve each IRegistry implementation as you find it. The registry classes could take the container as a constructor parameter and then each class could configure the container as needed for that layer/component/library.

public interface IRegistry 
{ 
    void Configure();
}

public class ServicesRegistry : IRegistry
{
    public ServicesRegistry(IUnityContainer container)
    {
        _container = container;
    }

    public sub Configure()
    {
        // Configure container for layer
    }
}

Now in your app startup somewhere you could have an application bootstrapper that either knows about all your registries or knows how to scan for them.

like image 197
Jeremy Wiebe Avatar answered Oct 23 '22 04:10

Jeremy Wiebe