0

I have one API projects with all endpoints and once ClassLibrary which holds all Dtos(model properties). Api projects have reference to ClassLibraray(Common). Actually I want to implement IValidatableObject for my Dtos to validate against the data in DB in classLibraray. For that I need to call Servicees which are available in the API projetcs. I cant refer API to Common(ClassLibraray) projects as its causing circular dependency. I am trying to resolve CD using Interface in newly created classlibraray but no luck. Anyone who can help me here with the solutions would be great. Thank you.

I have tried making Dtos class as partial and one same partial class in API but no luck. Also I am trying to inherit that model in the API with IValidatableObject but that not helping though PFB how I am trying using inheritance in API sulution

public class DepartmentDto : CreateDepartmentDto, IValidatableObject {

Mukesh
  • 53
  • 6
  • _"I have one API projects"_ Is it one or more? – Jeroen van Langen Feb 07 '23 at 07:20
  • 1
    Please View: https://stackoverflow.com/questions/2052579/circular-dependencies – Jazz. Feb 07 '23 at 07:21
  • 5
    I think the classlibrary shouldn't have a reference to the API. – Jeroen van Langen Feb 07 '23 at 07:22
  • @JeroenvanLangen Its one API – Mukesh Feb 07 '23 at 07:39
  • @JeroenvanLangen API have reference to ClassLibraray project. Now I need to have API reference to ClassLibraray to make use of existing services in API solutions. – Mukesh Feb 07 '23 at 07:47
  • @Jazz. We cant refator the classlibaray as dtos in classlibraray exposed as nuget otherwise I would have moved Dtos to API to resolve it – Mukesh Feb 07 '23 at 07:49
  • Do you need the Dtos in other projects or only in that API? – Jeroen van Langen Feb 07 '23 at 07:56
  • @JeroenvanLangen. I need the Dtos in ClassLibraray(Common Project). from Here I need to call a service written in the API project. PFB the dtos in Comon Project public class DepartmentDto : IValidatableObject { public Guid SiteGuid { get; init; } public IEnumerable Validate(ValidationContext validationContext) { var results = new List(); // Call API.Service.getID(this.SiteGuid) } return results; – Mukesh Feb 07 '23 at 08:31
  • I wouldn't put the code of calling an API in a Dtos library. Dtos are just the descriptions of the data, not how to get the data. The API is responsible for getting the data. You program should have a reference to the API and the Dto, The API has only a reference to the Dtos. Dtos are just "dumb". – Jeroen van Langen Feb 07 '23 at 08:47

1 Answers1

0

Step 1. Create an interface for your API and put it in a separate assembly.

interface IMyApi
{
    void Foo();
}

class MyApi : IMyApi
{
    public void Foo()
    {
        //etc
    }
}

Step 2. Register your API with your IoC container (i.e. service provider) if you have not already done so.

Step 3. Inject your service provider into the validation context when you construct it.

var validationContext = new ValidationContext(myDto, myServiceProvider);

Step 4. Retrieve the service within the Validate method.

public class DepartmentDto : CreateDepartmentDto, IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var myApi = validationContext.GetService(typeof(IMyApi));
        myApi.Foo();
    
        //etc
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Validate method is not getting trigger . API endpoint using CreateDepartmentDto which is in classlibrary and above code is in API project. I am just implementing the CreateDepartmentDto here along with IValidatableObject otherwise above code looks good. public class DepartmentDtos : CreateDepartmentDto, IValidatableObject { public IEnumerable Validate(ValidationContext validationContext) { – Mukesh Feb 07 '23 at 08:45
  • Sounds like a [different question](https://stackoverflow.com/questions/6431478/how-to-force-mvc-to-validate-ivalidatableobject) – John Wu Feb 07 '23 at 10:07