Developer forum

Forum » Swift » Swift and Don't Repeat Yourself (DRY)

Swift and Don't Repeat Yourself (DRY)

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I have a site that uses assortments. With the assortment, some products are not available to users that are logged in, but they are available for anymous users. When a logged in user looks at one of those products (for example, from a Text and Image item with a product), the link to the product is broken / does not exist (it has ?MainProductId= as the query string instead of a product reference).

I can fix that with code like this in my paragraph template:

 if (!productIds.Any())
 {
   return new Dynamicweb.Frontend.LinkViewModel()
   {
     Url = "/Default.aspx?ID=" + SomeIdIWantToRedirectTo
     IsExternal = false
   };
 }

However, I have this code in at least 8 templates; all of them from standard Swift I believe? (Search for string productParameter = productIds.Count == 1 ? "ProductID" : "MainProductId"; to see what I mean) Why?? A simple code fix now suddenly affects 8 templates which I all need to copy as custom, change, assign to item types as defaults in every site that uses them, add them to Git and maintain them forever Grrrrrrr. And to make it even worse: some templates like Text and Image repeat this code block *in* the same template!

Surely there must be a better way than repeating all this code in every template? Surely there must be a better way than repeating all this code in every template? 

Imar

 


Replies

 
Kevin Steffer
Kevin Steffer
Reply

I support the issue with the coding everything in templates is a challenge. 
However your assortment issue would probably be better solved at the index level with a macro is that is possible for you. 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Kevin,

I wish I could do it at the index level but I don't think I can. The products come from the Text and Image item type (and other item types that are similar) that doesn't use an index; instead you directly pick one or more products. For those you don't have access to, you then get a broken link. That's why this custom code is needed, and that's why I need to update it in all templates :-(

Imar

 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Long discussion of pros and cons.

The alternative is to wrap the code in a helper that needs to be included - or put into a seperate dll for Swift.

Then you have other issues - first of all the helper in the include does not have intellisense and you need to know a lot about what helpers exist and what they do. Also when you are in a template with the call to FancyHelper(), you cannot find that fancy helper. And when you do, you change it and then it affects 12 templates or more and some of them should not have been affected so stuff breaks.

With the dll you have other issues as well - the code is compiled and cannot be changed. You do not want that - and if you change it you take over the entire dll. Also not desirable - and it makes updates hard because you have to update DW, Swift and this custom dll.

There is no 'perfect' solution - there are just different solutions with different sets of pros and cons - we have chosen a solution where a template takes care of all - because then you can see exactly what happens and how it happens and it can be changed there without breaking anything else. And yes, the consequence is that some times you have to spend additional minutes doing the same thing - but you get it done and you get it done fast. Make the same change in Rapido and you would have spend half a day locating the code and figure out the consequence.

In the other approach with advanced and smart abstractions and coding being hidden away you need to look for a long time - and time is wasted - and it requires a lot more knowledge to change.

I agree that the balance might be a little off currently with too many things being repeated in the templates - the plan is to find a better balance where we will build in the most of the repeating stuff into the platform it self - and alternatively using partials for somethings like we do with prices and add to cart. Buttons and their links being a great candidate for putting into a partial.

BR Nicolai

 
Kevin Steffer
Kevin Steffer
Reply

Hi Nicolai, 

Your statement of writing: "With the dll you have other issues as well - the code is compiled and cannot be changed."
You probably know that it is possible to take control over compiled code if it's written in a proper design pattern regarding Inversion of Control and Dependency Injection, virtual methods etc.

The rest of your arguments are fair enough and has their rights and are just a matter of what the different partners and developers find benefits their customer projects most: no backend architecture and template coding or backend architecture and less template coding.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Yeah, fair enough Nicolai. I can see how striking a balance is difficult. It's just that it's never "spend additional minutes doing the same thing"; it usually takes a lot more as you need be aware where this is used. You usually only realize that when the customer reports it "still doesn't work". That's when you realize you have more places to fix.

Also, since there are now custom templates you need to configure all item types to use this new template. Multiply that by 2 or 3 customer facing environments + a few dev machines here and there and suddenly you're talking hours for something as simple as fixing a link.

Maybe convention over configuration would help here? If Something_Custom.cshtml exists it's used instead of Something.cshtml, similar to how template resolution can look in design folder and upwards?

Imar

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

There is no perfect solution - it willl be spaghettui a og b.

Yes, I know and understand the concept of customizable libraries - we already have a ton of it.  

Yes - adding new item types is a bit anoying because it cannot be self sustained in terms of where it is allowed in the item hierarchy - we are looking at fixing that so the itemtype itself can contain that information making it simpler to customize. We also discussed a feature in itemtypes where you can "Customize itemtype" which would make a copy, change all uses of the original to the new custom version and copy belonging template. This way starting the customization of the template is a little easier.

You can change the default template for an item type already in one central location - under item paragraph layouts, finde the item type in question and choose your custom template.

Thanks for the input.

@Kevin - we will talk further next week.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

>>There is no perfect solution - it willl be spaghettui a og b.

Agreed; it is what it is. 

>> This way starting the customization of the template is a little easier.

Nice, I like that.

>> You can change the default template for an item type already in one central location - under item paragraph layouts, finde the item type in question and choose your custom template.

We already do that. But as I said, that's lots of configuration, especially when having multiple environments.

Thanks!

 

You must be logged in to post in the forum