Created by: aaronclong
The current Asp.Net Core server generator doesn’t allow for iterative server stub generation. Users are forced to generate a full solution, and despite the template modification and ignore, it still doesn’t provide a truly modular and automated solution for iterative development. Often additive changes to APIs require both clients and servers to be updated, but the current Asp.Net approach doesn’t emphasize this. We are left with modifying generated source code and manually copy/pasting changes in an error prone process.
It would be far more practical to simple generate a csproj with the models and controllers that another project would then reference. The actual core business logic could be IOCed via an interface into the generated controllers. It becomes the developer’s responsibility to actually implement the interface, which the generated code would invoke.
It would look similar to this:
public interface IPetControllerLogic
{
IList<string> GetPetList();
}
[Route("…")]
public class PetController
{
public PetController(IPetControllerLogic controllerLogic)
{ ….
}
[HttpGet]
[ProducesResponseType(typeof(IList<string>), 200)]
[ProducesResponseType(500)]
public IList<string> GetPets()
{
return this.contollerLogic.GetPetList();
}
}
It would provide greater convenience because we could regenerate our API with spec changes and avoid overwriting business logic. The code generation process would be dramatically simplified.
The downside to this approach would be some caveats in Asp.Net Core’s design. Namely MVC pulls in all controllers in referenced dlls. The consumer of this dll would automatically opt into all the controllers, and there are many cases where this functionally wouldn’t be ideal. In that case, there would need to be a config toggle to make the controller abstract. In doing so, the consumer would need to subclass it for it to be instantiated. It also affords the consumer the ability to provide extra attributes if needed.
I would like to propose a new Asp.net Core option or a separate command to do this.