Better code: extending the LINQ class or creating a separate helper class?

I'm trying to figure out what's a better / cleaner / more convenient way, for the next problem. My Client class is created via LINQ from a database. The client has a special activation link, which is a URL that needs to be generated from both the client instance data and some static configuration data.

Solution 1: Extend the partial Customer class with the GenerateActivationUrl () method, which will retrieve data from its own instance and call a static class that has the configuration data and then displays the Url and returns it as a string.

Solution 2: Create a static helper class "LinkBuilder" that takes a Customer as an argument to the GenerateActivationUrl (Customer customer) method, which then takes the required data from the customer instance as well as static configuration data, and then returns the Url as a string.

Which is the best solution and why? Thank!

+2


source to share


1 answer


What exactly do you mean by "extension"? inheritance? extension methods?

Personally, I would use an incomplete class (in the same namespace, but a different file for the codegen) to show this as a property:

namespace MyDalNamespace {
    partial class Customer {
        public string ActivationUrl {get {/* some code */ }}
    }
}

      

This binds directly ActivationUrl

as a regular instance property Customer

.



As a property, it can participate in data binding (as opposed to extension methods, which cannot).

If you needed to do "option 2" (you may not have access to the dll dlls), then you need an extension method:

GenerateActivationUrl(this Customer customer) { ... } // note "this" 

      

+2


source







All Articles