How do I apply a "template" or "skeleton" of code in C# here?

Posted by Scott Stafford on Stack Overflow See other posts from Stack Overflow or by Scott Stafford
Published on 2010-02-26T15:04:54Z Indexed on 2010/03/18 18:51 UTC
Read the original article Hit count: 304

Filed under:
|
|
|

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;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection