Page 31 - MSDN Magazine, June 2019
P. 31
easy
multicolor
hit-
highlighting
®
After all is said and done, you’re looking at 9,500 lines of code. If you build a meta service capable of extracting an existing data- base schema, it becomes pretty obvious that you can generate this code using scaffolding—arguably avoiding any coding at all, yet still producing 9,500 lines of perfectly architected code, easily extended, using all the relevant design patterns and best practices. With only two seconds of scaffolding, your computer has done 80 percent of your work.
All you have to do now is go through the results of the scaffolding process, and override methods for your services and controllers for the domain types that require special attention for whatever reason. You’re done with your Web API. Because the controller endpoints all have the exact same structure, duplicating this scaffolding process in the client layer is as easy as reading the API JSON declaration files generated by Swagger. This enables you to create your service layer for something such as Angular or React. And all this because your code and your Web API have predictable structures, based on gen- eralization principles and the avoidance of repetition.
To put this in perspective, you’ve managed to create an HTTP REST Web API project that’s probably twice as large as the open source Sugar CRM project in complexity, and you did 80 percent of the work in seconds. You’ve facilitated a software factory assem- bly line that’s based on standardization of components and reuse of structure, while making the code for all your projects much easier to read and maintain. Even the parts that require modification and special behavior can be reused in your next project, thanks to the way controller endpoints and services are dynamically loaded into your Web API, without any dependencies.
If you work for a consulting company, you probably start several new projects each year with similar types of requirements, where you need to solve the commonalities for each new project. With an understanding of a client’s requirements and some initial implementation, a super-DRY approach enables you to literally finish an entire project in seconds. And of course, the composition of elements in your projects can be further reused, by identifying common modules, such as Authentication and Authorization. By implementing these modules in a common Web API project, you can apply them to any new project that poses similar problems to those you’ve seen before.
For the record, I’m making this sound easy, but the fact is that avoiding repetition is hard. It requires a willingness to refactor, refac- tor, refactor. And when you’re done refactoring, you need to refactor a bit more. But the upside is too good to ignore. DRY principles can let you almost magically create code, by simply waving your scaf- folding wand and composing modules out of pre-existing parts.
At the end of the day, the principles articulated here can help you leverage existing best practices to create your own Web APIs while avoiding repetition. There’s a lot of good that can come from this approach, and hopefully it helps you appreciate the awesomeness of DRY. n
Thomas hansen is a Zen software wizard currently living in Cyprus, where he juggles software code working its way through FinTech and trading systems.
Thanks to the following Microsoft technical expert for reviewing this article: James McCaffrey
msdnmagazine.com
Instantly Search Terabytes
dtSearch’s document filters support: • popular file types
• emails with multilevel attachments • a wide variety of databases
• web data
Over 25 search options including:
• efficient multithreaded search
•
• forensics options like credit card search
• SDKs for Windows, UWP, Linux, Mac, iOS in beta, Android in beta
• FAQs on faceted search, granular data classification, Azure and more
Visit dtSearch.com for
• hundreds of reviews and case studies
• fully-functional enterprise and developer evaluations
The Smart Choice for Text Retrieval® since 1991
dtSearch.com 1-800-IT-FINDS
Developers:
• APIs for C++, Java and .NET, including cross-platform .NET Standard with Xamarin and .NET Core