Hi Kyle - thanks so so so much for replying :) I've been madly
pressing refesh all morning/lunch hoping someone can help :)
> The generic solution is the simpliest. All you have to do is register
> the default for the open generic like this in your registry.
>
> this.ForRequestedType(typeof(ILoggerService<>))
> .TheDefaultIsConcreteType(typeof(LoggerService<>));
No way! :) that's it?? When i was talking about 'using generics', i
thought it would be...
public class Log4NetService<T> : ILoggingService
{ .. }
and then in the greedy constructor
public HomeController(ILoggingService<HomeController> loggingService)
{ .. }
which i guess would work, but that means i have to do that
_everywhere_ and it just doesn't feel 'right'. It feels a bit too
hardcoded, against the IoC type of concept.
but with your code above, what would a HomeController look like, then?
Just what I did, above?
> The non generic solution may take more configuration then you are
> willing to commit to.
>
> this.ForRequestedType<IController>()
> .AddInstances(instanceExp =>
> {
> instanceExp.OfConcreteType<HomeController>()
> .CtorDependency<ILoggerService>()
> .Is(loggerExp =>
> {
> loggerExp.OfConcreteType<LoggerService>()
> .CtorDependency<Type>()
> .Is(typeof(HomeController));
> });
> });
>
So with this code, this means i would have to declare _every_ instance
that uses the ILoggingService ... which sorta defeats the purpose.
It's just a really really really long way of doing the first option?
(I need to see how you define the constructor for a Controller, to see
if i'm on the right path, here)...
> If you still really want to use the non generic solutition I would
> make a type scanner to do it for me by default something like the
> following.
>
> public class DefaultMvcControllerConfiguration : ITypeScanner
> {
> private Type _controllerType = typeof(IController);
>
> #region ITypeScanner Members
>
> public void Process(Type type, PluginGraph graph)
> {
> if (type.CanBeCastTo(_controllerType))
> {
> var name = type.Name.Replace("Controller", "")
> .ToLowerInvariant();
> var instance = new ConfiguredInstance(type, name);
> Plugin plugin = PluginCache.GetPlugin(type);
> var loggerArgName =
> plugin.FindArgumentNameForType<ILoggerService>();
> var loggerInstance = new SmartInstance<LoggerService>()
> .CtorDependency<Type>().Is(type);
> instance.CtorDependency<ILoggerService>(loggerArgName)
> .Is(loggerInstance);
> graph.FindFamily(_controllerType).AddInstance(instance);
> }
> }
>
> #endregion
>
> }
>
This scanner thing is interesting. I'm very new to all this hardcore
StructureMap settings. I might have been using it for nearly all of
this year, but not all this hardcore setting stuff :) I noticed in
this scanner, you're searching for IController types. For myself, I
pass the ILoggingService to all types of constructors, not just
Controllers. So i'm not sure how this would apply to myself also.
Is there any way to mix reflection with the ForRequestedType setting?
something like (and this is very pseduo code, brainstorming...)
this.ForRequestedType(typeof(ILoggerService<>))
.TheDefaultIsConcreteType(typeof(LoggerService<>))
.CtorDependency(newInstance => newInstance.GetType());
?