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

  • Home
  • SEARCH
  • 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 6784851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:03:03+00:00 2026-05-26T17:03:03+00:00

Hi I would like to call method Foo after StructureMap creates instance of repository,

  • 0

Hi I would like to call method Foo after StructureMap creates instance of repository, so there is a method for that, called OnCreation.

x.For(typeof(IRepository<>))
 .Use(typeof(Repository<>))
 .OnCreation((ctx, instance) => { instance.Foo() });

But compiler of course can’t infer the type, so I tried to supply the generic type like this:

x.For(typeof(IRepository<>))
 .Use(typeof(Repository<>))
 .OnCreation<Repository<>>((ctx, instance) => { instance.Foo() });

This won’t compile, it would not even parse(Invalid expression term ‘>). I tried to build Action object by myself but with no luck. Then I found about InstanceInterceptor, so I’ve written a class, but I can’t figure how to plugin it in. There should be InterceptWith method, but it is not available for ConfiguredInstance which is result type of not generic Use method.

I know I can HACK that in many other ways but i would like to do it within StructureMap mapping.

Please help 🙂

  • 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-26T17:03:04+00:00Added an answer on May 26, 2026 at 5:03 pm

    If your Foo() method does not require access to the generic type then you could abstract this into a different interface:

    public interface IRepository
    {
        void Foo();
    }
    
    public interface IRepository<T> : IRepository
    {
    }
    
    public class Repository<T> : IRepository<T>
    {
        public void Foo()
        {
    
        }
    }
    

    Then to configure StructureMap:

    cfg.For(typeof(IRepository<>)).Use(typeof(Repository<>))
        .OnCreation<IRepository>((ctx, handler) =>
    {
        handler.Foo();
    });
    

    Things get incredibly tricky with open generic types and I’ve often found that it’s easier to hand additional initialization logic off to a factory. Rather than injecting an IRepository<T>, inject a IRepositoryFactory<T> that can perform any additional initialization you need (you could still inject IRepository<T> into your factory if you don’t like the static StructureMap reference):

    public interface IRepositoryFactory<T>
    {
        IRepository<T> Create();
    }
    
    public class RepositoryFactory<T> : IRepositoryFactory<T>
    {
        public IRepository<T> Create()
        {
            var repo = ObjectFactory.GetInstance<IRepository<T>>();
            repo.Foo();
    
            return repo;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to access the Foo instance foo within my manager method baz
This is an objective-c question. I would like to call a method in an
For logging purposes, I would like to call the .ToString() method of every object
I have a a hash foo = {'bar'=>'baz'} I would like to call foo.bar
I have built a C++ dll that I would like to call from C#
I have a simple Partial View that I would like to automatically call a
I would like to ensure that a method (actually a constructor in my case)
I would like to call python script files from my c++ program. I am
I would like to call Perl script files from my c++ program. I am
I would like to call my unmanaged C++ libraries from my C# code. What

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.