Bookmark and Share

Create Code in JustCode

The Q2 release of JustCode includes four new features that will enable you to quickly generate code and eliminate some of the busy work that plagues us as developers. I tested these new features on nopCommerce, an ASP.NET open source shopping cart.

Create Derived Type

When a class or interface is selected, you have the option to create a derived type (or class that implements the interface). The Create Derived Type feature is available in the Code submenu of JustCode, and it’s also available from the C section of the Visual Aid. The class is generated in the same file as base class. Using this feature, I can generate a derived class of BaseNopFrontEndPage. The default name will be the name of class with the suffix Derived, and it will create overrides for all of the virtual members by default. This behavior can be changed through the General tab in the Options menu if you do not wish it to create the overrides.

public partial class BaseNopFrontendPageDerived : BaseNopFrontendPage
{
    protected override void RenderExecutionTimerValue(HtmlTextWriter writer)
    {
        base.RenderExecutionTimerValue(writer);
    }

    public override PageSslProtectionEnum SslProtected
    {
        get
        {
            return base.SslProtected;
        }
    }

    public override bool AllowGuestNavigation
    {
        get
        {
            return base.AllowGuestNavigation;
        }
    }
}

Create Missing Event Handler Method for ASP.NET

Have you ever deleted your event handler from a web form? I have, and I tend to delete the attribute in the aspx file and add it back so that Visual Studio will regenerate the method for me. This isn’t ideal when the handler has been renamed, and an extra step is necessary to rename the newly created method. You can skip these steps in with JustCode. Just select the handler name in the aspx file, and either click Fix in the JustCode menu or select F section from the Visual Aid to bring up the option to create the method.

Create_1

One this is selected, a stub will be created.

public void SaveButton_Click(object sender, EventArgs e)
{
    // TODO: Implement this method
    throw new NotImplementedException();
}

Create Partial Class

Partial classes can be used to separate certain types of code from others. If I’m doing code generation, I would prefer the generated code be output into a designer file that contains a partial class to keep it separate from custom code. This prevents accidental changes to generated code that will be overwritten the next time code generation occurs.

If a class is marked partial, you can generate a partial class in a sister file by choosing Create Partial Class in the Code menu the C section of the Visual Aid.

For fun, I created a partial class to contain methods that aren’t event handler methods and named the file CountryAdd.ascx.logic.cs. I then edited the project file and added a new tag to the file called <DependentUpon> to make it linked to the control like a typical code behind.

public partial class CountryAddControl
{
    protected Country Save()
    {
        Country country = ctrlCountryInfo.SaveInfo();
        return country;
    }
}
<Compile Include="Administration\Modules\CountryAdd.ascx.logic.cs">
   <DependentUpon>CountryAdd.ascx</DependentUpon>
   <SubType>ASPXCodeBehind</SubType>
</Compile>

Create Overload

If you code like me, you will use the create overload feature in JustCode more than the others introduced in this article. Some may prefer to use optional parameters, but it is considered a design flaw in libraries since compilers can ignore the default value.

The SEOHelper class has a RenderTitle method to ensure the store name is included in the title.

public static void RenderTitle(Page page, string title, bool overwriteExisting)
{
    bool includeStoreNameInTitle = IoC.Resolve<ISettingManager>()
.GetSettingValueBoolean("SEO.IncludeStoreNameInTitle"); RenderTitle(page, title, includeStoreNameInTitle, overwriteExisting); }

By clicking on the overwriteExisting parameter, the option for Create Overload becomes available. It can be accessed through the Code menu or the C section of the Visual Aid. Selecting it creates the following overload.

public static void RenderTitle(Page page, string title)
{
    RenderTitle(page, title, false);
}

This also works with out/ref parameters, constructors, and it handles generics.

Conclusion

I dislike writing rote code, and these new features in JustCode eliminate the busy work. There are other new features in JustCode, so check back for new navigation features!

blog comments powered by Disqus

KodefuGuru.GetInfo()

Chris Eargle
LinkedIn Twitter Technorati Facebook

Chris Eargle
Telerik Developer Evangelist, C# MVP

JustCode

Telerik .NET Ninja

 

INETA Community Speakers Program

 

MVP - Visual C#

 

Friend of RedGate

World Map

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.