views:

126

answers:

3

In my business layer, I need many, many methods that follow the pattern:

public BusinessClass PropertyName
{
    get
    {
        if (this.m_LocallyCachedValue == null)
        {
            if (this.Record == null)
            {
                this.m_LocallyCachedValue = new BusinessClass(
                     this.Database, this.PropertyId);
            }
            else
            {
                this.m_LocallyCachedValue = new BusinessClass(
                     this.Database, this.Record.ForeignKeyName);
            }
        }
        return this.m_LocallyCachedValue;
    }
}

I am still learning C#, and I'm trying to figure out the best way to write this pattern once and add methods to each business layer class that follow this pattern with the proper types and variable names substituted.

BusinessClass is a typename that must be substituted, and PropertyName, PropertyId, ForeignKeyName, and m_LocallyCachedValue are all variables that should be substituted for.

Are attributes usable here? Do I need reflection? How do I write the skeleton I provided in one place and then just write a line or two containing the substitution parameters and get the pattern to propagate itself?

EDIT: Modified my misleading title -- I am hoping to find a solution that doesn't involve code generation or copy/paste techniques, and rather to be able to write the skeleton of the code once in a base class in some form and have it be "instantiated" into lots of subclasses as the accessor for various properties.

EDIT: Here is my solution, as suggested but left unimplemented by the chosen answerer.

// I'll write many of these...
public BusinessClass PropertyName
{
    get
    {
        return GetSingleRelation(ref this.m_LocallyCachedValue, 
            this.PropertyId, "ForeignKeyName");
    }
}

// That all call this.
public TBusinessClass GetSingleRelation<TBusinessClass>(
    ref TBusinessClass cachedField, int fieldId, string contextFieldName)
{
    if (cachedField == null)
    {
        if (this.Record == null)
        {
            ConstructorInfo ci = typeof(TBusinessClass).GetConstructor(
                new Type[] { this.Database.GetType(), typeof(int) });
            cachedField = (TBusinessClass)ci.Invoke(
                new object[] { this.Database, fieldId });
        }
        else
        {
            var obj = this.Record.GetType().GetProperty(objName).GetValue(
                this.Record, null);
            ConstructorInfo ci = typeof(TBusinessClass).GetConstructor(
                new Type[] { this.Database.GetType(), obj.GetType()});
            cachedField = (TBusinessClass)ci.Invoke(
                new object[] { this.Database, obj });
        }
    }

    return cachedField;
}
+1  A: 

Check out CodeSmith. They have a free trial and it's not too expensive if you want to purchase it. I've used it and it's great for generating code based on databases (which is what I'm guessing you're doing). Once you have your template setup, you can regenerate the code at any time. You can have it read the property names right from the database schema or you can enter the values you want to use. I'm sure you could even get it to read the values from a file if you wanted to generate a whole batch of classes at once.

TLiebe
I haven't looked at that package yet, but I am hoping for a non-code-generating solution. I'm currently using the Microsoft SQLMETAL and this layer is built on top of it (for better or worse).
Scott Stafford
What about using Code Snippets? Have a look at http://msdn.microsoft.com/en-us/library/f7d3wz0k%28VS.80%29.aspx
TLiebe
That's a neat feature too, but I was hoping to avoid any copy/paste-type solution so that I can make changes to the template as necessary.
Scott Stafford
+1  A: 

You could check out using T4 Templates. I am not quite sure which is "the" resource for T4, but I found a good article on it in VisualStudioMagazine.

It is free, has an easy to use syntax and is actually used by a lot of projects (e.g. Subsonic) for code generation, so you should be able to find some real-world scenarios.

Johannes Rudolph
A: 

You can code-gen using CodeSmith or MyGeneration or the like. You'd probably store a list of classes and properties somewhere and then pass that data to the code generator. You may want to investigate using pre-build events to re-gen those classes prior to compiling the solution.

Or, you could bake this functionality into a base class or helper method.

public BusinessClass MyProperty
{
    get { return GetCached("MyProperty", "PropertyId", "FKName", "LocalValue"); }
}

I'll leave the body of GetCached() up to you, but it's basically the same as what you posted with the variables passed in as arguments.

If any of those values are the same for all properties in a class then you could of course pull them from instance variables, and only pass to GetCached() those things that vary on a per-property basis.

Bottom line: if there's a way to abstract the logic of what you're doing into a base method, so that using that logic becomes a one-liner, then that's probably the best way to go because it's easier to override when you have special cases. If you can't do that, code generation can do the grunt work for you, but you'll need to work out things like when do I re-gen, how do I regen, etc.

Seth Petry-Johnson
Right, I agree that generation is for the birds. I tried to write a method that does it, but I would have to pass in `this.Record.ForeignKeyName` and I can't do that because this.Record might be NULL... Are you suggesting that I pass in the string "this.Record.ForeignKeyName" and use reflection?
Scott Stafford
@Scott: rather than pass in a value to the method, why doesn't the method access the property directly? For instance, if you declare both `GetCached()` and `Record` in the same base class, then GetCached() has can its own null-check. You only want the method arguments to be the things that change at each call site. Does that make sense, or am I misunderstanding something?
Seth Petry-Johnson
Thanks. See the (edited) question for the body of GetCached.
Scott Stafford