you do not expose the internal parts of the library, so as long as you do not change the endpoint, you can optimize it, release new versions and the clients of this library need not to change anything in their code.the construction of a very complex library is simplified to one single endpoint, which only requires some configuration (be it a string, an array.) described in documentation.The project itself calls new on desired object based on the configuration string and after the construction of the endpoint, should it not throw an exception, you are set to use all the functionality of the library. In fact, the one public endpoint, the Cache class, acts as a factory itself. When using this approach, the factories are inside the cache project. In your main project, the code then could be as simple as this: var cache = new ("type:redis,connection:,options:") īy parsing the string, the constructor will then see, you want to cache to redis, and provided the configuration, it will use the configuration and pass it onto the internal classes of the cache library, which know what to do with it.īe aware the configuration does not need to be a string, you may use Inside this constructor the Cache endpoint will instantiate the factories belonging to the cache project, it will the use those to instantiate the correct implementations. It is a variable in which you define what you want to have, you insert your configuration, and this one public endpoint, the class, will, in its constructor, parse this string, extract the data it needs from it, and based on the user input construct everything necessary. Notice the configuration string as a parameter. Not saying very much, is it? What exactly is the magic I am talking about? The class constructor could look like this: public class Cache : CachingInterface If you were to follow this ideology, all the classes inside your cache library project would be set to internal except one class, perhaps a class, which would be set to public, thus if you were to use the cache library, the only class you would have publicly access to is one single endpoint. Creating a public endpoint of the library to expose it When dealing with construction of classes belonging to another project (this will usually be some common library which contains code useful throughout more projects), you usually have two options how to expose the project to the outer world.įor this demostration purpose I chose the cache layer, which, from my experience, usually has very similar API throghout many different projects, only the configuration is different. The part where the object graph is constructed, the newing of the classes. No matter how object oriented your code is, a part of OO code will always have to be procedural. In the end you either have factories in the library itself, or the library does not have them and the client (the user) of the library is responsible for creating them and instantiating the library himself. It depends on how configurable and extendable you want to have the reusable library (project), without touching it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |