In the last chapter, we analyze and understand modelbinder and this article will add our own modelbinder in the example to get the effect we expect. This time we have a simple demo to the whole station of the Modelbinder to do a solution.
First, we give the engineering document diagram:
Note that in this case, my goal is to implement a generic, extensible Modelbinder engine for the entity object of the whole station. Because we want to convert between entity objects and URL strings, we can define the corresponding methods to implement them for convenience, so we define the IEntity interface, The interface is defined as follows:
Explained, the Fill method converts the corresponding string into an object. The ToString method is to convert the object to a specific string. That is, as long as an object implements the interface, We can easily convert the object from the URL string to the object. That's exactly what we're trying to achieve. Another way is to let ientity to achieve iconvertible, but iconvertible method is too much, lazy will not use it.
Then, in the project, BaseEntity is an abstract class that implements only the methods of object and string conversion in ientity. For versatility, here's the reflection. The Fill method implements: