Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7505743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:00:55+00:00 2026-05-29T22:00:55+00:00

I am creating a custom ActionResult for my controllers because I’ve noticed a lot

  • 0

I am creating a custom ActionResult for my controllers because I’ve noticed a lot of repeated code that could be made reusable. It looks something like this:

 public ExtendedViewResult<T> : ActionResult
 {
      protected T Model { get; set; }
      protected IModelExtender<T> Extender { get; set; }

      public ExtendedActionResult(T model, IModelExtender<T> extender)
      {
          this.Model = model;
          this.Extender = extender;
      }
 }

 public class BaseController : Controller
 {
     public ExtendedViewResult<T> ExtendedView<T>(T model)
     {
         // I need to create the result here, but how?
         var result = new ExtendedViewResult<T>(model, ???????);

         return result;
     }
 }

The problem I am having is that I’m not sure how to construct my ExtendedViewResult object. Since the interface is generic I want to use Dependency Injection to get the proper object, but I’m not sure how to do that since I’m constructing the object myself.

I am using Ninject and Ninject.MVC3 and the default Nuget package creates a Bootstrapper class for me, and when I access the Bootstrapper.Kernel property I get the following warning:

Ninject.Web.Mvc.Bootstrapper.Kernel is obsolete. Do not use Ninject as Service Locator.

If I’m not supposed to access the kernel directly, then how can I change my code so that I can get the appropriate concrete class?

EDIT
Here is the ninject bootstrapper code. The only method I added is the GetInstance()

public static class NinjectMVC3 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    /// <summary>
    /// Starts the application
    /// </summary>
    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
        DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
        bootstrapper.Initialize(CreateKernel);
    }

    /// <summary>
    /// Stops the application.
    /// </summary>
    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    // I ADDED THIS CODE, EVERYTHING ELSE IS AUTO-GENERATED
    // BY THE NUGET PACKAGE
    public static T GetInstance<T>()
    {
        return bootstrapper.Kernel.Get<T>();
    }

    /// <summary>
    /// Creates the kernel that will manage your application.
    /// </summary>
    /// <returns>The created kernel.</returns>
    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        RegisterServices(kernel);
        return kernel;
    }

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
    }        
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:00:59+00:00Added an answer on May 29, 2026 at 10:00 pm

    Sorry this will be dry on actual code – I’ve not used ninject much, but other DI containers have this solution, and I’m sure ninject will have this construct as well.

    The issue is that you are constructing the object itself. When you’re really using a DI container and following IoC, anytime you see the keyword new should be a red flag – and using the container as a service locator is a yellow one.

    So how do we get rid of the ‘new’, since you need a new object? The answer is to have your BaseController take a dependency on a factory that can create an ExtendedViewResult. In Autofac (my container of choice), that would be as simple as having a Func<ExtendedViewResult> injected. I would be surprised if Ninject doesn’t have the same. In fact, looks like it does – this ninject wiki page points to this blog post on Ninject.Extensions.Factory.

    So that means your code for the Controller could look like this:

    public class ConcreteController : BaseController
     {
         private Func<Foo,ExtendedViewResult<Foo>> _factory;
         public BaseController(Func<Foo,ExtendedViewResult<Foo>> factory)
         { 
            _factory = factory;
         }
    
         public ExtendedViewResult<Foo> Method(Foo model)
         {            
             var result = _factory(model);    
             return result;
         }
     }
    

    If you REALLY want to have a generic method do the creation in your base class, then you will probably need to go the explicit factory interface route from the blog post linked above. With this style code though, you would not need it in most cases, and your controllers explicitly declare the dependencies they need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating custom control and because I need to do lot's of binding inside
I am creating custom Swing components and want to provide a UI that looks
I'm creating a custom ActionResult class and I need it to be able to
In WPF, we are creating custom controls that inherit from button with completely drawn-from-scratch
I'm creating custom table view cells, but result is incorrect. My code: - (UITableViewCell
I'm creating custom control that contain multiple parts. Inside template creation I'm subscribing for
I am creating custom images that I later convert to an image pyramid for
I am creating custom code snippet templates using DevExpress' CodeRush. Is there anyway for
I'm new to creating custom object in JavaScript, so it could easily be something
So I'm creating a custom ActionFilter that's based mostly on this project http://www.codeproject.com/KB/aspnet/aspnet_mvc_restapi.aspx .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.