Quantcast
Channel: DevNet Questions
Viewing all articles
Browse latest Browse all 8901

how to combine 2 methods with different objects (same code in both methods)

$
0
0
New poster here, but have been using SO for a long time now.I'm a bit rusty from C# coding and I have something I'd like to optimize in my code. I have 2 methods that have the exact same code, but with a different object (the object has some different properties, but the ones that are used are the same in both methods). The query is built differently before the call to the method. Is there a way to combine the methods easily so that I can still use it with one or the other object?private static void SetQueryMainFilters(DocumentQueryObject documentQuery, DocumentQuery repeaterQuery, out DocumentQuery outRepeaterQuery) { if (documentQuery.NodeID @ 0) { repeaterQuery.WhereEquals(KenticoConstants.NODE_ID, documentQuery.NodeID); } if (documentQuery.DocumentID @ 0) { repeaterQuery.WhereEquals(KenticoConstants.DOCUMENT_ID, documentQuery.DocumentID); } outRepeaterQuery = repeaterQuery; } private static void SetQueryMainFilters(DocumentQueryObject documentQuery, MultiDocumentQuery repeaterQuery, out MultiDocumentQuery outRepeaterQuery) { if (documentQuery.NodeID @ 0) { repeaterQuery.WhereEquals(KenticoConstants.NODE_ID, documentQuery.NodeID); } if (documentQuery.DocumentID @ 0) { repeaterQuery.WhereEquals(KenticoConstants.DOCUMENT_ID, documentQuery.DocumentID); } outRepeaterQuery = repeaterQuery; } One of the method that calls the duplicated methodpublic static MultiDocumentQuery RepeaterMultiDocumentQuery(DocumentQueryObject documentQuery) { MultiDocumentQuery repeaterQuery = new MultiDocumentQuery(); [...] SetQueryMainFilters(documentQuery, repeaterQuery, out repeaterQuery); return repeaterQuery; } Currently, I have a MultiDocumentQuery object and a DocumentQueryObject. The code is the same in both methods, but the object is different.Is there a proper way to do this so I can avoid code duplication? If you have a better solution to offer me, I have my ears open for suggestions. I need to be able to either call the Multi query builder or the regular query builder, with pretty much the same properties but different behavior. It uses Kentico's DocumentQuery and MultiDocumentQuery.Thanks!

Viewing all articles
Browse latest Browse all 8901

Trending Articles