Search code examples
asp.netasp.net-mvcvb.netowinkatana

Why are OWIN startup classes declared as partial, yet sometimes not?


If I use VS2015.1 to create a new sample project (either WebForms or MVC), two files are created for configuring OWIN:

\Startup.cs (or .vb)

using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(WebApplication6.Startup))]
namespace WebApplication6
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

\App_Start\Config.Auth.cs (or .vb)

namespace WebApplication6
{
    public partial class Startup {

        public void ConfigureAuth(IAppBuilder app)
        {
           // removed for brevity
        }
    }
}

I see that both classes are declared as partial, but in the many examples online of how to use OWIN, the classes are not partial (see here, here, here and here).

Could anyone please explain if there is a correct or best approach, and what difference it makes to the application whether partial is used or not?


Solution

  • I think I found an answer here, but would still appreciate anything others can add in the context of OWIN specifically:

    https://stackoverflow.com/a/3601918/792888

    The biggest use of partial classes is make life easier on code generators / designers. Partial classes allow the generator to simply emit the code they need to emit and do not have to deal with user edits to the file. Users are likewise free to annotate the class with new members by having a second partial class. This provides a very clean framework for separation of concerns.

    So the partial class is simply compiled into a class. The partial declaration allows other classes with the same name to co-exist (which are then all compiled together into a single class).