0

In my Asp.net Core 5 API Project

I have a serviceLayer that the controller uses, to get data from a third layer called dataLayer. I want to use the service layer as a DLL in different projects.

This ServiceLayer Contain dependency Injections like that :

namespace ServiceLayer
{
    public class UserService :  IUserService
    {
        IUserRepository userRepository; // (From DataLayer)
        public UserService(IUserRepository repository) : base(repository)
        {
            this.userRepository = repository;
        }
        public Users GetAllPersonsById(int id)
        {
                return userRepository.GetById(id);
        }
    }

    public interface IUserService : IService<Users>
    {
        Users GetAllPersonsById(int id);
    }

How can I use the method GetAllPersonsById with the DLL ServiceLayer

Ali.K
  • 45
  • 1
  • 7

1 Answers1

1

can I use it because the dependency Injections

As soon as you reference the DLL / project you can use all classes the same ways as if they were in the project.

To use a class as a service:

  1. Provide the service
  2. Inject the service

There's a lot of documentation available, so I'll keep this short:

// provide in startup.cs
services.AddTransient<IUserService, UserService>();

// Inject where you need it
MyConstructor(IUserService userService) {}

See https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0

Provide Extension Method

If we take a look at other libs, most of them provide a method to setup the services.

Example: Entity framework core

public void ConfigureServices(IServiceCollection services)
{                    
    services.AddDbContext<MyDbContext>(options => options.UseSqlServer(...));
}

So you could:

  1. In your lib, create an extension method for IServicesCollection that adds all services of your lib.
  2. In the consuming project, call services.AddMyLibServices().

This could look like so:

public static class ServicesConfiguration
{
    public static void AddDataLayer(this IServiceCollection services)
    {
        services.AddTransient<IUserService, UserService>();
        // ... same for all services of your lib
    }

}

Here's a tutorial with more details: https://dotnetcoretutorials.com/2017/01/24/servicecollection-extension-pattern/

Lamar service registries

An optional and alternative approach are service registries. It's very similar to the extension methods but uses a class to do the setup. See https://jasperfx.github.io/lamar/documentation/ioc/registration/registry-dsl/

Composition Root

You may want to read about the composition root pattern, e.g. What is a composition root in the context of dependency injection?

In a simple app, your startup.cs is your composition root. In more complex apps, you could create a separate project to have a single place to configure your apps services.

Create the DLL

There are two ways to create the DLL:

  • As a project in your solution (so your solution has multiple projects, each will result in a separate DLL)
  • As a separate solution and as nuget package
Christoph Lütjen
  • 5,403
  • 2
  • 24
  • 33