Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would argue that you are violating two principles: the single responsibility principle (SRP) and the open/closed principle (OCP).</p> <p>You are violating the SRP because the bootstrapping class have more than one reason to change: if you alter model binding or the auto mapper configuration.</p> <p>You would be violating the OCP if you were to add additional bootstrapping code for configuring another sub-component of the system.</p> <p>How I usually handle this is that I define the following interface.</p> <pre><code>public interface IGlobalConfiguration { void Configure(); } </code></pre> <p>For each component in the system that needs bootstrapping I would create a class that implements that interface.</p> <pre><code>public class AutoMapperGlobalConfiguration : IGlobalConfiguration { private readonly IConfiguration configuration; public AutoMapperGlobalConfiguration(IConfiguration configuration) { this.configuration = configuration; } public void Configure() { // Add AutoMapper configuration here. } } public class ModelBindersGlobalConfiguration : IGlobalConfiguration { private readonly ModelBinderDictionary binders; public ModelBindersGlobalConfiguration(ModelBinderDictionary binders) { this.binders = binders; } public void Configure() { // Add model binding configuration here. } } </code></pre> <p>I use Ninject to inject the dependencies. <code>IConfiguration</code> is the underlying implementation of the static <code>AutoMapper</code> class and <code>ModelBinderDictionary</code> is the <code>ModelBinders.Binder</code> object. I would then define a <code>NinjectModule</code> that would scan the specified assembly for any class that implements the <code>IGlobalConfiguration</code> interface and add those classes to a composite.</p> <pre><code>public class GlobalConfigurationModule : NinjectModule { private readonly Assembly assembly; public GlobalConfigurationModule() : this(Assembly.GetExecutingAssembly()) { } public GlobalConfigurationModule(Assembly assembly) { this.assembly = assembly; } public override void Load() { GlobalConfigurationComposite composite = new GlobalConfigurationComposite(); IEnumerable&lt;Type&gt; types = assembly.GetExportedTypes().GetTypeOf&lt;IGlobalConfiguration&gt;() .SkipAnyTypeOf&lt;IComposite&lt;IGlobalConfiguration&gt;&gt;(); foreach (var type in types) { IGlobalConfiguration configuration = (IGlobalConfiguration)Kernel.Get(type); composite.Add(configuration); } Bind&lt;IGlobalConfiguration&gt;().ToConstant(composite); } } </code></pre> <p>I would then add the following code to the Global.asax file.</p> <pre><code>public class MvcApplication : HttpApplication { public void Application_Start() { IKernel kernel = new StandardKernel( new AutoMapperModule(), new MvcModule(), new GlobalConfigurationModule() ); Kernel.Get&lt;IGlobalConfiguration&gt;().Configure(); } } </code></pre> <p>Now my bootstrapping code adheres to both SRP and OCP. I can easily add additional bootstrapping code by creating a class that implements the <code>IGlobalConfiguration</code> interface and my global configuration classes only have one reason to change.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload