views:

202

answers:

1

Hello, I'm trying to figure out what the better/cleaner/more maintainable way is, for the following problem. My "Customer" class is generated via LINQ from the database. The customer has a special activation link which is a URL that needs to be generated from data from both a Customer instance and some static configuration data.

Solution 1: Extend the partial Customer Class with GenerateActivationUrl() method which will fetch data from its own instance and call the static class which has configuration data and then render the Url and return it as a string.

Solution 2: Make a "LinkBuilder" static helper class which takes a Customer as an argument for a GenerateActivationUrl(Customer customer) method which will then take the needed data from the customer instance and also the static configuration data and then return the Url as a string.

Which solution is better, and why? Thank you!

+2  A: 

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

Personally, I'd use a partial class (in the same namespace, but a different file to the codegen) to expose this as a property:

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

This directly binds ActivationUrl as a regular property on the Customer instance.

As a property, it can participate in data-binding (unlike extension methods, which can't).

If you had to do "option 2" (perhaps you don't have access to the DAL dll), then an extension method would be appropriate:

GenerateActivationUrl(this Customer customer) { ... } // note "this"
Marc Gravell
Yes I meant partial class "extension". Not extension methods. Simply adding to the LINQ generated partial class.
Alex
Your comment still isn't 100% clear; but don't edit the generated output, if that is what you mean.
Marc Gravell
I think I'll go with what you suggested (option 1). Thanks!
Alex