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
Of course you can and it is perfectly fine.
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.
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.
// 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.
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.
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.
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.
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. ...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With