0

Initialization of Container:

public override void Initialize()
        {

            ObjectFactory.Initialize(x =>
                                         {
                                             x.Scan(s =>
                                                        {
                                                            s.TheCallingAssembly();
                                                            s.AssemblyContainingType<IRegistar>();
                                                            s.WithDefaultConventions();

                                                        });

                                         });

        }

Global.asax.xc

  var dependencyContainer = new DependencyContainer();
            dependencyContainer.Initialize();

            ControllerBuilder.Current.SetControllerFactory(new DependencyControllerFactory());

The Get Instance:

public class DependencyControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                return base.GetControllerInstance(requestContext, controllerType);
            }

            try
            {
                return ObjectFactory.GetInstance(controllerType) as Controller;
            }
            catch (StructureMapException exception)
            {
                Debug.WriteLine(ObjectFactory.WhatDoIHave());
                throw;
            }
        }
    }

The Controller

private IBabyRepository _babyReposoitory; {

  public BabyController(IBabyRepository babyRepository)
  {
      _babyReposoitory = babyRepository;
  }

Class trying to DI/Ioc

public class BabyRepository : IBabyRepository, IRepository<Baby>, IRegistar
{
    Just a Default constructor here();
    code...
}

The Error:

StructureMap Exception Code: 202 No Default Instance defined for PluginFamily Domain.Repository.IBabyRepository, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Me

What am I doing wrong?

Neurath
  • 121
  • 1
  • 2
  • 6

1 Answers1

0

You don't tell StructureMap how to create BabyRepository.

I think your scan needs to be something like this:

        Scan(y =>
        {
            y.AssemblyContainingType<IRegistar>();
            y.Assembly(Assembly.GetExecutingAssembly().FullName);
            y.With(new RepositoryScanner());
        })

Then need to code up the custom RepositoryScanner:

public class RepositoryScanner : IRegistrationConvention
{
    public void Process(Type type, Registry registry)
    {
        if (type.BaseType == null) return;

        if (type.GetInterface(typeof(IRepository).Name) != null)
        {
            var name = type.Name;

            registry
               .For<IRepository>()
               .AddInstances(y => y.Instance(new ConfiguredInstance(type).Named(name)));
        }
    }
}
ozczecho
  • 8,649
  • 8
  • 36
  • 42
  • 1
    This looks good but it there anyway to make structure map scan for injection with out having to define each Interface? – Neurath Feb 15 '11 at 01:02
  • For example would I use a generic interface ISomeStuff and use that on all the point to all the Interfaces I wish to be DI'ed? – Neurath Feb 15 '11 at 01:03