0
votes

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?

1
Does the WhatDoIHave output show BabyRepository registered for IBabyRepository? - Joshua Flanagan
Also, show the constructor of the controller that you are trying to instantiate. - Joshua Flanagan
WhatDoIHave is in function part of the StrcutMap objectfactory container it shows what Interfaces are mapped to what concrete’s - Neurath
Added the Controller and constructor - Neurath

1 Answers

0
votes

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)));
        }
    }
}