Call for design change in codegen templatesCall for design change in codegen templates
Old forum URL: forums.lhotka.net/forums/t/5356.aspx
WMeints posted on Saturday, September 06, 2008
Although the codegen templates that are out there are all pretty good there is something which I think is a great miss at the moment.
In my projects I always work with a pattern where the Business Objects make use of a data repository class to retrieve raw record data from the database and perform other CRUD related operations. The contract for the repository class is pretty simple. Each method accepts a Data Transfer Object with the data and returns either a collection of DTO's or a single DTO as result or in the case of a delete operation nothing.
The repository classes are seperated by using an interface and letting a separate factory class construct the actual instance for me. This enables the use of Inversion Of Control mechanics like Spring.NET or recently Unity.
A typical DataPortal_Select method may look like this:
private void DataPortal_Select(SingleObjectCriteria x) {
IMyDataRepository repository = RepositoryFactory.CreateMyDataRepository();
MyData rawData = repository.SelectById(x.Id);
LoadObjectData(rawData); //Load the raw object data from the DTO
}
What I would like to see in more codegen templates is something similar to this. The factory in this case may be a simple matter of just returning a new instance of the right repository by using the new operator. This will provide developers with an even better way to extend the possibilities of the generated code and more importantly you can provide mockups for the repository classes and test the business objects seperately.
I would love to hear your thoughts on this and I hope that more people see the advantages of this pattern.
IanK replied on Wednesday, September 10, 2008
You might want to check out Miguel Castro from Steelblue (See link on Website) CSLAex library. He uses a single DataAccess layer similar to what you are discussing and his codegenerator default templates target towards this.
I adopted his code and for example have a fetch something like this:
Private
Overloads Sub DataPortal_Fetch(ByVal criteria As Criteria)
Using reader As SafeDataReader = _
New DAC.CentralDataAccess(True).ReaderFromProcedureById(Of Integer)("ProcName", criteria.ID)
If reader.Read() Then
Try
PopData(reader)
Catch ex As Exception
Throw
End Try
End If
End Using
End Sub
Copyright (c) Marimer LLC