Why create Api file which contains like MyclassResponse in Controllers file?

Mr-Aristo

New member
Joined
Oct 26, 2023
Messages
2
Programming Experience
Beginner
I am working with the project and it is complicated. So there api controllers and controllers file they created api file and inside Response classes. Why they used them i dont get it.
C#:
var data = (from factory in _db.Factories
            select new FactoryResponse
            {
             FactoryId = factory.FactoryId.ToString(),
             TagPrefix = factory.TagPrefix,
             CatalogEnable = factory.CatalogEnable,
             DataEnable = factory.DataEnable,
            }).ToList();
this is a exapmle usage.
Controllerd>Api>FactoryResponse
 
Last edited by a moderator:
Maybe you're asking, without actually asking, why the method isn't just returning the Factory objects directly, instead of creating instances of the additional FactoryResponse class. That may be done for either or both of two main reasons.

Firstly, the developer may simply not want to expose their entity classes publicly. The data model should really be something only used internally and the public interface is dedicated to that specifically and only exposes what is absolutely necessary.

Secondly, even if the developer was OK with their Factory class being exposed, it might be that that class has additional properties that don't need to be returned and/or shouldn't be returned. Again, only expose what is absolutely necessary.
 
Maybe you're asking, without actually asking, why the method isn't just returning the Factory objects directly, instead of creating instances of the additional FactoryResponse class. That may be done for either or both of two main reasons.

Firstly, the developer may simply not want to expose their entity classes publicly. The data model should really be something only used internally and the public interface is dedicated to that specifically and only exposes what is absolutely necessary.

Secondly, even if the developer was OPK with their Factory class being exposed, it might be that that class has additional properties that don't need to be returned and/or shouldn't be returned. Again, only expose what is absolutely necessary.

Thank you very much for your answer and your time.
 
Back
Top Bottom