Migrating from Table Models to Domain Models via OCL
I’ve been dwelling on this idea of trying to use some variation of the Object Constraint Language within .Net, and I think I’m finally at the point where I can try to discuss it in a coherent manner. OCL is used to describe the constraints on objects within UML, but I’d like to enhance it be used in more than UML.
One of the things I seem to find myself doing time and time again, when building multi-tier applications, is using the Data Mapper Pattern (see Martin Fowler’s PoEAA book) to map my data (retrieved from some data source using a Data Access Layer) to my Domain Model (the Line of Business Layer), but that requires quite a bit of code. .Net does not have a tool to help in situations where the business logic is complex enough to warrant a Domain Model approach. Instead it uses the Table Model, via Strongly Typed Datasets. With Strongly Typed Datasets you can take an XML Schema and transform the data structure into classes. But this approach does not work well for more complex systems, since the generated classes contain only the behaviors required to maintain the data structure. There is no way to add business rules (aka constraints or behaviors) to the generated classes.
So, I got to thinking that the thing which was missing in the Strongly Typed Dataset approach was the business rules, and that most of the business rules can be defined using OCL. There seems to also be a similar problem in XML Schema, where in that language they would also like to add something more than just the structure of the data. Daniel Cazzulino has done a bunch of stuff with Schematron for .Net, and seems to be another (partial) solution to the problem. If we can create another namespace (let’s call it eXtensible Constraint Language for now), for use within XML-Schema, then we could add the extra business rules to the schema, and be able to create a version of Strongly Typed Datasets, but one that includes these business rules. Then as a developer, I would have the choice of using either the Table Model, or when things are a bit more complex, the Domain Model, and have tools to make coding for either model easier.
The one final thing that I see is missing is when a constraint is not met. Currently, when validating an XML document to a Schema you will generate schema exceptions. But in the case of constraints, I’d like to be able to do one better, and bind each constraint to exception. But I don’t want just a list of canned exceptions, but to be able to create my own. So we would need another language (lets call it eXentsible Exception Language) that can be used to describe the Exceptions, and the hierarchies. Then you can bind a XCL to a XEL, and when you generate the enhanced Strongly Typed Dataset, you will get the constraints and the exceptions. The bonus is that you can also translate the Schemas embedded with XCL and XEL to some very good system documentation, and/or generate the Schemas from UML.
Does anyone know if someone is working on something like this? Is this something other developers are interested in, or is it just something that intrigues me?
The preceding blog entry has been syndicated from the DonXML Demsak’s All Things Techie Blog. Please post all comments on the original post.