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 8196891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:37:15+00:00 2026-06-07T05:37:15+00:00

I have this registration in StructureMap ObjectFactory.Initialize(x => { x.For<IPageModel>().UseSpecial(y => y.ConstructedBy( r =>

  • 0

I have this registration in StructureMap

ObjectFactory.Initialize(x => {
    x.For<IPageModel>().UseSpecial(y => y.ConstructedBy( r => ((MvcHandler) HttpContext.Current.Handler).RequestContext.RouteData.GetCurrentModel<IPageModel>()));
});

And then I access this object in my constructor like this

public HomeController(IPageModel model) {}

Now I would like to register all concrete types that implements the interface IPageModel and when asked for I want to use the same For<> statement to get the correct instance.

It seems like i could use Scan together with my own convention to do this but I can’t figure out exactly how to do it.

This is some example code

x.Scan(scanner =>
{
    scanner.AssembliesFromApplicationBaseDirectory();
    scanner.Convention<MySpecialConvetion>();
});

public class MySpecialConvetion : IRegistrationConvention {
    public void Process(Type type, Registry registry) {
        if(type.IsAssignableFrom(typeof(IPageModel))) {
            registry.For<CONCRETE IMPLEMENTATION>().UseSpecial(y => y.ConstructedBy( r => ((MvcHandler) HttpContext.Current.Handler).RequestContext.RouteData.GetCurrentModel<CONCRETE IMPLEMENTATION>()));
        }
    }
}

Edit: It seems like I need to use the non-generic For, but how can I handle the construction my self using the non-generic For?

  • 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-06-07T05:37:16+00:00Added an answer on June 7, 2026 at 5:37 am

    I got this working by creating a generic method definition, and used reflection to populate the types. Easier to show than explain:

        public class MySpecialConvetion : IRegistrationConvention
        {
            public static readonly MethodInfo RegisterMethod = typeof (MySpecialConvetion)
                .GetMethod("Register", BindingFlags.NonPublic | BindingFlags.Static)
                .GetGenericMethodDefinition();
    
            public void Process(Type type, Registry registry)
            {
                if (type.IsAssignableFrom(typeof (IPageModel)))
                {
                    var specificRegisterMethod = RegisterMethod.MakeGenericMethod(new[] { type });
                    specificRegisterMethod.Invoke(null, new object[] { registry });
                }
            }
    
            static private void Register<T>(Registry registry)
                where T : IPageModel
            {
                registry
                    .For<T>()
                    .UseSpecial(y => y.ConstructedBy(r => GetCurrentPageModel<T>()));
            }
    
            static private T GetCurrentPageModel<T>()
                where T : IPageModel
            {
                var handler = (MvcHandler) HttpContext.Current.Handler;
                if (handler == null)
                    return default(T);
                return handler.RequestContext.RouteData.GetCurrentModel<T>();
            }
        }
    

    I added an intermediate step and checked for a null handler since I didn’t have one in my site. But this should get you the missing piece you needed.

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

Sidebar

Related Questions

I have this code for displaying content for login and registration in my PHP
I have a registration form where this CSS rules apply. when user submits with
We have a registration system Database and basically what this query does is check
I have a check box in my registration form like this: <form name=reg id=reg
in my project i have one registration form which is developed in C#.net.to this
Let's say I have an user registration form. In this form, I have the
I'm using ExtJS on a registration page which should have no effect on this.
I have this registration form box, and i really like how the background gets
I have this registration form which is then validated using "bassistance's" validate.js: $('#pForm').validate({ rules:
I am running Joomla! 2.5.1 for my website. I wish to have this mass-registration

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.