0

I have a .NET 7 project where I have the following projects:

  1. WebApp
  2. Web API
  3. Service Layer
  4. Repository Layer

#1 calls #2 via HTTP requests, #2 references #3 which references #4, those 2 are project references (not microservices). So I am trying to use DI to inject service layer into Web API and repo layer into service layer. But obviously, I shouldn't have access to the repo layer in the Web API, so I created the following 2 extension methods:

// service project
namespace MyApp.Service
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddServices(this IServiceCollection services)
        {
            services.AddSingleton<IMyService, MyService>();

            return services;
        }
    }
}
// repo project
namespace MyApp.Repository
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddRepository(this IServiceCollection services, string connectionString)
        {
            var settings = MongoClientSettings.FromConnectionString(connectionString);
            var client = new MongoClient(settings);

            services.AddSingleton<IMongoClient>(client);
            services.AddSingleton<IMyRepository, MyRepository>();

            return services;
        }
    }
}

Then in my Program.cs file:

var mongoDbConnectionString = builder.Configuration.GetConnectionString("MongoDB");
builder.Services.AddRepository(mongoDbConnectionString);
builder.Services.AddServices();

I would've just created one extension method in the service layer, but I needed to create a separate one in the repo layer since I am injecting the MongoDB client too.

I did not expect to be able to access AddRepository() in my Web API project but when I entered that class, VS automatically added using MyApp.Repository; at the top and it was successfully able to access it... How and why? If I can access it, have broken the separation and DI and IoC practices and have a dependency on my repo layer from my Web API. I checked under Dependencies -> Projects for my Web API project and it only lists the service project...

So my questions are:

  1. why can I access the repo layer from my Web API layer?
  2. how do I prevent it?
  3. what is the proper way to do DI with my repo class? should I just not do DI with my MongoDB client and create one extension method in the service layer?
Asheq Reza
  • 87
  • 1
  • 7
  • I think I figured out how to properly do it, but still don't know the answer to my first 2 questions. The way I am doing it is I changed the method in the repo layer from an extension method to just a regular method that takes IServiceCollection as a parameter and now I call that method in the AddServices() extension method and pass services in to AddRepository(). – Asheq Reza May 23 '23 at 06:41
  • 2
    Does this answer your question? [Disable transitive project reference in .NET Standard 2](https://stackoverflow.com/questions/46709000/disable-transitive-project-reference-in-net-standard-2) – Eugene May 23 '23 at 07:21
  • Yes it does, thank you! I’m a little surprised this is on by default, any good reason why? You end up getting tightly coupled applications then, even when you’re trying to do the opposite like in this case. – Asheq Reza May 23 '23 at 13:30
  • So everything seemed to be working until I disabled transitive package reference. Even though nowhere in the WebAPI project nor in the service project am I ever explicitly calling or referencing the MongoDB drivers, just calling the AddRepository() in the repo layer returns the error "System.IO.FileNotFoundException: 'Could not load file or assembly 'MongoDB.Driver". I thought it was either because they were all static classes or because we are loading the MongoDB client into the DI container, but I made them non-static and removed code adding to DI container and still same error... – Asheq Reza May 29 '23 at 04:08

0 Answers0