Using Interfaces

Posts   
 
    
gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 11-Jan-2006 18:53:33   

Hi All,

I've tried to search but couldn't find anything. If someone can give me a link to a thread, then shame on me and thanks to the supplier of the link!

How can I make 2 entitytypes implement a certain interface?

For example:

Task & Calendar event would implement IActivity

where I activity has these properties:

Subject Description RelatedPerson Date

Task has, respectivly, Description, RelatedPerson, Date, Completed (And ofcourse more, but these would

CalenderEvent, Description, RelatedPerson, Date, EndDate, Location (And ofcourse more, but these would

I want to use both elements mixed in certain collections, calender views I build, etc. But in real situation they have these fields (sort of) in common and lots of extra different fields.

How can I do this??

Just guide me to the right piece of manual, thread or answer! Every effort will be well appericiated!

Gab.

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 11-Jan-2006 20:58:35   

One solution would be to emit the code by modifying the template you are using. I just did this a few days ago. If you want to do it this way let me know and I will post an example.

gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 11-Jan-2006 22:57:48   

As far as I can see now this is my only chance wink

So any advise or samples would be great!****

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 12-Jan-2006 00:18:37   

OK. Although Frans has tried to document all this in the SDK, I could not find an example of actually doing it, and had to poke around for a while to get the hang of it. Hopefully this will save you an hour or two. Here's what I did:

Went to C:\Program Files\Solutions Design\LLBLGen Pro\Drivers\SqlServer\Templates and found the config file for the template set I was using. In my case, that was CSharpTemplateSet.config. I made a copy of this, and called it something else. I need to change my names a little bit so... let's say "MyCSharpTemplateSet.config". Then I changed the binding for SD_EntityAdapterTemplate from

 "..\..\..\SharedTemplates\C#\entityAdapter.template" 

to

 "..\..\..\SharedTemplates\C#\myEntityAdapter.template" 

plus I added these bindings:

<templateBinding templateID="Custom_EntityAdapterTemplate" templateFilename="..\..\..\SharedTemplates\C#\myCustomInclude.template" />
<templateBinding templateID="TraceSetEntityInclude" templateFilename="..\..\..\SharedTemplates\C#\myTraceSetEntityInclude.template" />
<templateBinding templateID="TraceEntityInclude" templateFilename="..\..\..\SharedTemplates\C#\myTraceEntityInclude.template" />
<templateBinding templateID="DateTimeTraceEntityInclude" templateFilename="..\..\..\SharedTemplates\C#\myDateTimeTraceEntityInclude.template" />
<templateBinding templateID="LatitudeTraceEntityInclude" templateFilename="..\..\..\SharedTemplates\C#\myLatitudeTraceEntityInclude.template" />
<templateBinding templateID="LongitudeTraceEntityInclude" templateFilename="..\..\..\SharedTemplates\C#\myLongitudeTraceEntityInclude.template" />
<templateBinding templateID="NumericTraceEntityInclude" templateFilename="..\..\..\SharedTemplates\C#\myNumericTraceEntityInclude.template" />
<templateBinding templateID="StringTraceEntityInclude" templateFilename="..\..\..\SharedTemplates\C#\myStringTraceEntityInclude.template" />

Then I went to C:\Program Files\Solutions Design\LLBLGen Pro\SharedTemplates\C# and made a copy of entityAdapter.template and named it myEntityAdapter.template. Here I made several changes, using the TDL code. For example, I added namespaces:


<[If StringValueEquals CurrentEntityName "TraceSet, Trace, DateTimeTrace, LatitudeTrace, LongitudeTrace, NumericTrace, StringTrace"]>
// emitted by my template
using My.Namspace;
<[EndIf]>

then just under the class declaration

    <[If StringValueEquals CurrentEntityName "TraceSet, Trace, DateTimeTrace, LatitudeTrace, LongitudeTrace, NumericTrace, StringTrace"]>
        // emitted by my template
        , I<[CurrentEntityName]>
    <[EndIf]>

and further down


#region Included code
// emitted by my template
<# Custom_EntityAdapterTemplate #>
#endregion


Now remember I bound Custom_EntityAdapterTemplate to my own file. Here are the contents of that file.


<[If StringValueEquals CurrentEntityName "TraceSet"]>
<# TraceSetEntityInclude #>
<[EndIf]>

<[If StringValueEquals CurrentEntityName "Trace"]>
<# TraceEntityInclude #>
<[EndIf]>

<[If StringValueEquals CurrentEntityName "DateTimeTrace"]>
<# DateTimeTraceEntityInclude #>
<[EndIf]>

<[If StringValueEquals CurrentEntityName "LatitudeTrace"]>
<# LatitudeTraceEntityInclude #>
<[EndIf]>

<[If StringValueEquals CurrentEntityName "LongitudeTrace"]>
<# LongitudeTraceEntityInclude #>
<[EndIf]>

<[If StringValueEquals CurrentEntityName "NumericTrace"]>
<# NumericTraceEntityInclude #>
<[EndIf]>

<[If StringValueEquals CurrentEntityName "StringTrace"]>
<# StringTraceEntityInclude #>
<[EndIf]>


And the files I bound to each of the placeholders (e.g., "TraceSetEntityInclude") holds actual code to be emitted for that entity. This code would be the interface implementation and other code needed.

Note that I put a comment in anywhere I emit code, so I can easily search for it in the generated file. Also note that I believe I used a mixture of approaches here; I used the hook to "include" code in the output by binding Custom_EntityAdapterTemplate, but I also directly modified the (copy of) the template file.

One thing that is minorly annoying, IMHO, is all the extra blank lines that get generated into the output file. I used to wonder about these, but now I understand that when a line is evaluated, it will result in a newline being generated, even if the expression results in only whitespace. Makes the generated code look a little funky. I even thought about adding a task at the end (if this is possible) to collapse down the newlines, but I don't have the time, really. Frans, if you are reading this, please consider. (I am the most anal person on the forum and therefore likely the only one who would suggest this).

Also a FAQ that describes a "walk-through" like I just did would be nice. simple_smile

gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 12-Jan-2006 10:52:19   

Thanks a lot, will try over te weekend... Wanted to do now but deadlines rage

Thanks, Gab

gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 12-Jan-2006 19:54:49   

I know it's bad to use fast solutions, but we all know you have to do stuff dirty now and then with very tight deadlines...

I was thinking if it is possible to just add some kind of CustomUserCode element with IInterfaceIWantToUse and make the implementation in the custom code of the EntityClasses...

Possible?

Thanks, Gab

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 12-Jan-2006 20:27:05   

Yeah, just stick it into the user code sections, it gets preserved during regeneration.