0

i have an interface "IRunAtStartup" which is implemented across several classes. the interface has a method "Execute" which needs to be executed for all the classes on the application startup.

at the time of compilation, i am not aware the number of classes implementing the interface and hence this really needs to be written in a generic way.

Using Unity for the IOC and following are 2 approaches i have tried 2 approaches

1) throws runtime error:

container.RegisterTypes(runAtStartUp, WithMappings.FromAllInterfaces);

2) throws a compilation error - "cannot resolve type" in the register command inside for loop. i am relatively new to Unity world and suspect that i am missing some basic issue here.

IEnumerable<Type> runAtStartUp = typeFinder.FindClassesOfType<IRunAtStartup>(true);

foreach (var type in runAtStartUp)
{
    container.RegisterType<IRunAtStartup, type>(type.AssemblyQualifiedName);
}
foreach (var task in container.ResolveAll<IRunAtStartup>())
            {
                task.Execute();
            }
5
  • Can you post the stack trace? Approach 2 seems OK to me, there may be a problem with one of the types being registered.
    – cynic
    Jul 24, 2014 at 8:49
  • approach 2 does not even compiles. here is a screenshot of the same: dl.dropboxusercontent.com/u/11374665/approach-2.JPG
    – Vikram
    Jul 24, 2014 at 8:55
  • Oh. You are using invalid C# syntax, missed that. Use container.RegisterType(typeof(IRunAtStartup), type, type.AssemblyQualifiedName) instead.
    – cynic
    Jul 24, 2014 at 9:01
  • That works perfect... do you want to post this as the answer so that i can mark it as answer ? i knew i was missing something basic, just did not realize what it was...
    – Vikram
    Jul 24, 2014 at 9:10
  • I've posted the answer.
    – cynic
    Jul 24, 2014 at 9:13

1 Answer 1

1

The idea of the approach 2 is correct (use named registrations), however you have a C# syntax error. The method to use is:

container.RegisterType(typeof(IRunAtStartup), type, type.AssemblyQualifiedName);
6
  • i have a slightly different scenario now but i suspect i am making again a syntactical mistake... can you spot something right away ? foreach (var settingClass in typeFinder.FindClassesOfType<ISettings>(true)) { container.Resolve<ISettingService>().LoadSetting<typeof(settingClass)>("domId"); }
    – Vikram
    Jul 24, 2014 at 10:55
  • here is a link with screenshot. link appreciate your help, @cynic
    – Vikram
    Jul 24, 2014 at 11:00
  • You can create a non-generic version of the method on the right - that is, LoadSettings(Type settingsType, string domainGuid...). Or you can use reflection to call the existing generic version. You simply can't call generic methods in C# the way you do - between the < > goes the type name, known at compile time, not the System.Type instance known at run time.
    – cynic
    Jul 24, 2014 at 11:07
  • thanks for prompt response. however, ive seen something similar in an open source application which is using autofac, here is a screenshot of the same link
    – Vikram
    Jul 24, 2014 at 11:12
  • 1
    Note that the TSettings in that case is a type parameter. You can "pass" a type parameter to another generic method if the generic constraints allow it (that is, you write LoadSetting<TSettings>() and it compiles). You can get a System.Type instance inside a generic method via typeof(TSettings). What you cannot do is to call either BuildRegistration<TSettings> or LoadSetting<TSettings> with a System.Type instance.
    – cynic
    Jul 24, 2014 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.