Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • NDDT 68 posts 240 karma points c-trib
    Nov 08, 2016 @ 09:55
    NDDT
    0

    Circular Dependency Injection Problem

    I have a Problem with a dependency injection.

    In the first step I'm injecting Services to retrieve Data from a third-party-API into my Umbraco-Controllers. This works fine.

    Now I have a custom dashboard which saves the API-Urls and I'm trying to inject a service to get these URLs into the other Services. This doesn't work. And I'm stuck.

    A Service will look something like this:

    namespace My.Namespace.Interfaces.Services
    {
        public interface ISomeModelService : IDisposable
        {
            List<SomeModel> GetSomeModel(string parameter); 
        }
    }
    
    namespace My.Namespace.Umbraco.Shared.Services
    {
        public class SomeModelService : ISomeModelService
        {
            public void Dispose()
            {
            }
            public List<SomeModel> GetVertragskonten(string parameter)
            {  
            }
        }
    }
    

    My Global Asax:

    namespace Namespace
    {
        public class Global : UmbracoApplication
        { 
            protected override void OnApplicationStarted(object sender, EventArgs e)
            {
                base.OnApplicationStarted(sender, e);
                var configuration = GlobalConfiguration.Configuration;
                var builder = new ContainerBuilder();
                builder.RegisterInstance(ApplicationContext.Current).AsSelf();
                //register all controllers found in this assembly
                builder.RegisterControllers(Assembly.GetExecutingAssembly());
                var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith("My.Namespace."));
                foreach (var a in assemblies)
                {
                    builder.RegisterControllers(a);
                    builder.RegisterApiControllers(a);
                }
    
                builder.RegisterApiControllers(typeof(UmbracoContext).Assembly);
                builder.RegisterType<SomeModelService>().As<ISomeModelService>();
    
                var container = builder.Build();
    
                //setup the webapi dependency resolver to use autofac
                configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
                //setup the mvc dependency resolver to user autofac
                DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            }
        }
    }
    

    Some Umbraco Controller

    namespace Namespace.Controllers
    {
        public class SomeController : SurfaceController
        {
            private ISomeModelService someModelService;
    
            public KontoController(ISomeModelService s)
            {
                this.someModelService = s;
            }
        }
    }
    

    How would I inject a Service that can retrieve Data from Umbraco into the other Service. I have problems with ciruclar build dependency. Thanks for your help.

  • Claus Jensen 49 posts 157 karma points hq
    Nov 08, 2016 @ 17:45
    Claus Jensen
    0

    You could just have the service you need injected via constructor injection in your service.

    To do that, make sure you register the service interface (IDataTypeService or whatever you need to use) in your IoC container so it knows how to resolve it for using it when constructing your service.

    When that is done you should be able to just use:

    public class SomeModelService : ISomeModelService
    {
        private readonly IDataTypeService _dataTypeService;
    
        public SomeModelService(IDataTypeService dataTypeService)
        {
            _dataTypeService = dataTypeService;
        }
    
        public List<SomeModel> GetVertragskonten(string parameter)
        {
            _dataTypeService.dostuff();
        }
    }
    
  • NDDT 68 posts 240 karma points c-trib
    Nov 09, 2016 @ 09:47
    NDDT
    0

    But the build-order will still be a problem, won't it?

  • Claus Jensen 49 posts 157 karma points hq
    Nov 09, 2016 @ 10:09
    Claus Jensen
    0

    I think it is a bit hard to answer without knowing a bit more about what you're trying to do here.

    You are trying to use the Umbraco services for getting something stored inside Umbraco I assume - in your own service... is that correct?

    If that is the case I don't see where the circular dependency would come from as our services do not rely on any of your services.

    If you could highlight a bit more precisely on where your problem arises, I'll see if I can give you some more answers ;)

  • NDDT 68 posts 240 karma points c-trib
    Nov 21, 2016 @ 12:15
    NDDT
    0

    I have Services which inject a 3rd party API into Umbraco. So my Umbraco-System depends on these Services.

    Now I need to inject Data from a Custom-Dashboard into these Services which makes them dependend on Umbraco.

    I think that's the reason my project doesn't build.

Please Sign in or register to post replies

Write your reply to:

Draft