2

I have a C# web api project whose architecture is shown in the following Diagram (ProjectA) enter image description here

In the above diagram Controllers in Project A use the services of Project A in order to do the actual job and the Services use the Repository in order to read and write on the Database. All of them use Autofac Dependency Injection. examples: Constructor of Controller that uses various services

 public DKMenuController(ITokenService tokenService, IUserService userservice, IDKMenuService dkMenuService)
    {
        _tokenService = tokenService;
        _dkMenuService = dkMenuService;
        _userservice = userservice;
    }

Constructor of a Service that uses the underlying repository for communication with the DB

 public DKMenuService(IRepository repository)
    {
        this._repository = repository;
    }

The various services are registered when the Application starts as follows

    this.RegisterType<Repository>().As<IRepository>().InstancePerRequest();
 this.RegisterType<DKMenuService>().As<IDKMenuService>().InstancePerRequest();

Now I want to add another Project (named in the example ProjectB) that is simply a Class library and will be called from a ProjectA Service, will do something and then will use ProjectA's repository pattern and save data in DB. The problem is that this design leads to circular references and the 2 projects won't compile. There exists a similar question here How to solve circular reference? but the answer is not so clear whether it's correct. Any ideas?

Christoph Adamakis
  • 865
  • 2
  • 9
  • 26

2 Answers2

2

It may be some work, but a fix would be to move the Repository into a new ProjectRepo.

Then have ProjectA and ProjectB both reference ProjectRepo, and then ProjectA can also reference ProjectB without B needing A.

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • That should not require any `redesign`. Just a little tweaking and refactoring in the implementation(s). – H H Nov 24 '16 at 14:26
0

As far as I know what you are showing here is not a circular reference. You cannot draw a circle following the arrows on the diagram.

If the repository classes are on Project A Services then you have a circular reference but that's not drawn on the question. Having this in mind if that's the case move the repository outside Project A.

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • 1
    At class level nothing is circular, but at Project/Assembly level it would be? – Peter B Nov 24 '16 at 10:25
  • I agree with Peter B. At project/Assemly Level there exists a circular reference. Let's say that there exists a ServiceA in Project A. This service A initiates a new object of Service B. ServiceB does some things and then calls Project's A repository in order to save in DB. If you try to build Project B, it will fail cause it needs the binaries from Project A for the repository Class. If you try to build Project A first, it will fail as it needs Project's B ServiceB class – Christoph Adamakis Nov 24 '16 at 10:37
  • @ChristophAdamakis - Project A doesn't have a repository. I don't see a cycle. – Enigmativity Nov 24 '16 at 10:43
  • Repository is in ProjectA. See my diagram again – Christoph Adamakis Nov 24 '16 at 10:45
  • 1
    @ChristophAdamakis - It appears that Project A references Repository. That's not the same as the Repository being in Project A. – Enigmativity Nov 24 '16 at 11:02
  • @ChristophAdamakis - It sounds more like you are saying that Project B needs to reference the methods in Project A. It has nothing to do with the Repository (as both A & B reference the Repository). – Enigmativity Nov 24 '16 at 11:04
  • O Yes Project B needs to reference methods in Project A, as the Repository Belongs to ProjectA. I hope it's clear enough now – Christoph Adamakis Nov 24 '16 at 11:15