METHOD OF IMPROVING CODE REUSABILITY BY IMPLEMENTING TRAITS IN .NET
DOI:
https://doi.org/10.35546/kntu2078-4481.2025.3.2.13Keywords:
metaprogramming, .NET, code generation, software development, traitsAbstract
The article describes a method improving code reusability by implementing traits in .NET. The main methods for improving the possibilities of reusing software components in the object-oriented programming paradigm are analyzed. The advantages and disadvantages of multiple and single inheritance are analyzed. It is established that multiple inheritance has a positive effect on modularity and on the code reusability, however, it can lead to a more complex class hierarchy, has more complex semantics and is not always supported by the programming language. As an alternative to inheritance, the use of composition to ensure the reuse of software components is considered. The concept of traits is analyzed, which utilizes composition and is implemented in a number of modern programming languages as an alternative to multiple inheritance. As a result of the analysis, it is established that traits allow you to obtain functionality similar to multiple inheritance without drawbacks it causes. A number of proposals for expanding the concept by adding the ability to define states are also analyzed. Based on this, a traits model was developed for implementation in .NET, which allows defining states and providing better control over member overriding during composition. Since composition can be implemented by utilizing metaprogramming techniques, the corresponding .NET platform metaprogramming toolkit was analyzed.As a result of the analysis, it was determined that the Incremental Source Generators tool in combination with the Roslyn API will allow achieving the desired result. For this purpose, it is proposed to use a code generator that will generate partial declarations of classes that will contain members of the corresponding trait. As an alternative to keywords, it is proposed to use metadata attributes to control the behavior of such a code generator. Such a code generator should work not only during compilation, but also react to the source code changes. This method will allow fully implementing the traits concept in .NET, which will help improve code reusability when developing on this platform.
References
Albalooshi F., Mahmood A. A Comparative Study on the Effect of Multiple Inheritance Mechanism in Java, C++, and Python on Complexity and Reusability of Code. International Journal of Advanced Computer Science and Applications. 2017. Т. 8, № 6. URL: https://doi.org/10.14569/ijacsa.2017.080614 (дата звернення: 20.08.2025).
Traits: Composable Units of Behaviour / N. Schärli та ін. ECOOP 2003 – Object-Oriented Programming. Berlin, Heidelberg, 2003. С. 248–274. URL: https://doi.org/10.1007/978-3-540-45070-2_12 (дата звернення: 20.08.2025).
Cassou D., Ducasse S., Wuyts R. Traits at work: The design of a new trait-based stream library. Computer Languages, Systems & Structures. 2009. Т. 35, № 1. С. 2–20. URL: https://doi.org/10.1016/j.cl.2008.05.004 (дата звер- нення: 20.08.2025).
A new modular implementation for stateful traits / P. Tesone та ін. Science of Computer Programming. 2020. Т. 195. С. 102470. URL: https://doi.org/10.1016/j.scico.2020.102470 (дата звернення: 20.08.2025).
Incremental Roslyn Source Generators In.NET6. URL: https://www.thinktecture.com/net/roslyn-source-generators-introduction/ (дата звернення: 20.08.2025).
Saadatmand M. Towards Automating Integration Testing of.NET Applications Using Roslyn. 2017 IEEE International Conference on Software Quality, Reliability and Security Companion (QRS-C), м. Prague, Czech Republic, 25–29 лип. 2017 р. 2017. URL: https://doi.org/10.1109/qrs-c.2017.99 (дата звернення: 20.08.2025).
Weber D., Christi A. Redusharptor: A Tool to Simplify Developer-Written C# Unit Tests. International Journal of Software Engineering & Applications. 2023. Т. 14, № 5. С. 29–40. URL: https://doi.org/10.5121/ijsea.2023.14503 (дата звернення: 20.08.2025).
Immutype. URL: https://github.com/DevTeam/Immutype (дата звернення: 20.08.2025).
AutoInterface. URL: https://github.com/beakona/AutoInterface (дата звернення: 20.08.2025).
Traits: A mechanism for fine-grained reuse / S. Ducasse та ін. ACM Transactions on Programming Languages and Systems. 2006. Т. 28, № 2. С. 331–388. URL: https://doi.org/10.1145/1119479.1119483 (дата звернення: 20.08.2025).







