C# Templates -- Delegation Methods region

C# Templates -- Delegation Methods region

Old forum URL: forums.lhotka.net/forums/t/1390.aspx


davem1958 posted on Wednesday, October 04, 2006

I'm just starting to wade thru CodeSmith, CSLA, and templates.  One thing that puzzles me is the "Delegation Methods" region, which apparently was added to support partial classes.  That seems like a lot of additional complexity to support a partial class.  Is it worth it?  Any alternative?  :)

 

rasupit replied on Wednesday, October 04, 2006

The code is in generated class and not in user class, so the amount of code is actually hidden from user.

I toyed whith several alternatives such as adding base class, and creating template methods on user class.  This I think is the most elegant way.  I also found from one of Eric Gunnerson's article (and test it) that delegates in .Net 2.0 is as performant or close to overrides.

As whether is worth it? That should be my line of question Smile [:)]  I guess I let others who uses partial class methods to chime in.

Ricky

Skafa replied on Thursday, October 05, 2006

one disadvantage of code generation with partial classes is that you cannot override methods in de 'Generated' partial class. That's something you should consider when deciding between the single/base/partial methods.

another thing is: (as for as i saw) you cannot mix the partial class and base class generation methods when the different classes depend on eachother.

rasupit replied on Tuesday, October 10, 2006

one disadvantage of code generation with partial classes is that you cannot override methods in de 'Generated' partial class.

With partial class you can't override it but you'll wire delegate with your method contain your overridable code and return false so the generated code won't execute.

======== generated code ===========

        private void ExecuteInsert(SqlConnection cn, RootObject parent)
        {
            if (!onExecuteInsert(cn, parent)) return;
            using (SqlCommand cm = cn.CreateCommand())
            {
                cm.CommandType = CommandType.StoredProcedure;
                cm.CommandText = "AddProduct";

                AddInsertParameters(cm, parent);

                cm.ExecuteNonQuery();

                _productID = (int)cm.Parameters["@NewProductID"].Value;
                onExecuteInsertComplete(cm, parent);
            }//using
        }


========== User Code ===========

        private bool executeInsert(SqlConnection cn, RootObject parent)
        {
            //your overridable code goes here
            return false; //false when you want to stop executing generated code
        }

HTH,

Ricky

davem1958 replied on Friday, October 13, 2006

Ricky, can you give me an example of why I would need to override a method in the generated class?

Still trying to decide which way to go -- partial or split base

Dave

rasupit replied on Friday, October 13, 2006

Dave,
Normally you would not need to override a method in generated class, however is there when you need it.

These delegates are place strategically to provide comparable (with split base) ability to inject/override the generated code from user class when need it.

Ricky

Copyright (c) Marimer LLC