1

I have a rather simple solution setup:

Web Project --> Service layer --> Repositories

My IoCbootstrapper class is located in my web project.

var c = new Container();
c.Register<IUserService, UserService>();        
c.Register<ILoggingService, LoggingService>();

That's fine but my services are depending on repositories.

Is there a way to have a "parital bootsrapper" in Service layer class library that would then be somehow passed as a configuration to a web project since this way an instance of service cannot be created as web project is not aware of repositories?

Cœur
  • 37,241
  • 25
  • 195
  • 267
talfirevic
  • 111
  • 1
  • 9

2 Answers2

1

Have service layer expose an action that takes the container and registers what is needed.

Service layer which references Repositories

namespace Project.ServiceLayer {
    public static class DryIocExtesnions {
        public static void AddServiceLayer(this IContainer c) {
            c.Register<IUserRepository, UserRepository>();        
            c.Register<IOtherRepository, OtherRepository>();
        }
    }
}

IoCbootstrapper located in web project.

var c = new Container();
c.Register<IUserService, UserService>();        
c.Register<ILoggingService, LoggingService>();

//Register service layer 
c.AddServiceLayer();

draw back, depending on your preference is that now the service layer has to be aware of concerns from the composition root. You could abstract the to your own container but that may be overkill.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • I ended up doing basically the same thing, only thing I was curious if DryIoC has some functionality to scan dependant assemblies for container configurations that would do this process "automatically". But having my web project "know" about this composition was a totally acceptable trade off. Having this type of "leak" in separation of concerns is not something I'm worried at the moment. – talfirevic Nov 28 '16 at 13:02
  • DryIoC does not have that natively but you can create a custom abstraction/contract yourself for that and have all the directly referenced layers implement it. From there you can scan referenced assemblies for that interface via reflection, activate those implementations and invoke the contract method with the container. – Nkosi Nov 28 '16 at 13:20
  • yeah, and just as you commented initially, this seems like an overkill for me at this point. – talfirevic Nov 28 '16 at 15:12
0

May be you can use container.WithAutoFallbackResolution with repositories assemblies, or some variant of container.RegisterMany

dadhi
  • 4,807
  • 19
  • 25