Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is injecting service into another service bad practice? [closed]

I am creating a web application that is tiered in the following way:

Controller > Service > Repository

So it's following a service and repository pattern.

Let's say I have 2 entities Product and Page like so:

public class Product
{
   public string Name { get; set;}
   public Page Page { get; set; )
}

public class Page
{
   public string Name { get; set;}
}

Each of these entities have a repository like so:

public class ProductRepository
{
   public Product GetProduct(int productId)
   {
      // code
   }
}

public class PageRepository
{
   public Product GetPage(int pageId)
   {
      // code
   }
}

And ofcourse, each of these repositories have a service which a repository will be injected into:

public class ProductService
{
   public bool DoesProductExist (int productId)
   {
      // code
   }
}

public class PageService
{
   public bool CreatePage (int productId, PageRequest page)
   {
      // code
   }
}

The problem I have right now, is that when calling the PageService to create a page, it needs to check if a product exists with the given productId because if not then a page shouldn't be created.

I have the following methods but I don't know if they're the best methods or if theres any better

Method 1

Should I inject ProductService into PageService to use the DoesProductExist() method, because reusable code?

Method 2

Should I inject ProductRepository into my PageService to make my own DoesProductExist() method in PageService (defeating the idea of reusable code)

Method 3

Should I create a cross service something like ProductPageService which would implement both services?

If neither of these are good methods then please feel free to suggest your own

like image 463
KTOV Avatar asked Jun 11 '19 22:06

KTOV


People also ask

Can we Autowire a service in another service?

Of course you can and it is perfectly fine.

Can we use a service in another service in spring boot?

It's ok to have dependencies from one service to another. This is mainly because of reusability. If circular dependencies happen between two services from the same module, that's ok.

How do I inject a service to another service in C#?

Another approach would be to create a new service with dependencies on both AgreementService and OrganisationService, and have that new service carry out the responsibility. The new service would of course be injected into your controller.

How can call one service from another service in asp net core?

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. Run the service. Click at display ( method ) to go to test page and click the "invoke" button. Repeat the same for calling "show" method.

What is an example of injecting a dependency into a service?

Example: ProductService is injecting IProductRepository as a dependency in its constructor then using it inside the Delete method. Define required dependencies explicitly in the service constructor.

Can a service be constructed without its dependencies?

Example: ProductService is injecting IProductRepository as a dependency in its constructor then using it inside the Delete method. Define required dependencies explicitly in the service constructor. Thus, the service can not be constructed without its dependencies.

How do I resolve a service in a method body?

If you are resolving a service in a method body, always create a child service scope to ensure that the resolved services are properly released. If a method gets IServiceProvider as an argument, then you can directly resolve services from it without care about releasing/disposing.

What are the different types of service lifetimes in dependency injection?

There are three service lifetimes in ASP.NET Core Dependency Injection: 1 Transient services are created every time they are injected or requested. 2 Scoped services are created per scope. In a web application, every web request creates a new separated service scope. ... 3 Singleton services are created per DI container. ...


2 Answers

Injection is just the tool.

Is injecting service into another service bad practice?

The primary answer is No, that is fine.

What you should pay attention to is Dependencies. It would be awful to inject a BLL service into a DAL for example. You need a clear picture of layers/tiers/modules and draw the lines of who uses who.

But your chain looks OK.

like image 148
Henk Holterman Avatar answered Nov 15 '22 19:11

Henk Holterman


  • Method 1 as is, it would create a dependency between your services.

  • Method 2 it's not a good practice to mix repositories between services.

  • Method 3 it's the best way to go, but instead of "implement both services" I would say "interact with / orchestrate both services". You can extract IProductService and IPageService interfaces and inject them into your "cross service". This way you avoid coupling. You can also use this approach (injecting IProductService into PageService) for Method 1.
like image 32
Cosmin Sontu Avatar answered Nov 15 '22 17:11

Cosmin Sontu