I am trying to register multiple implementations of the same interface...and then...use the Setter to set the property on my Application instance. I have tried multiple online examples and always get the SAME instance plugged-into the 2 Application properties.
- NOTE: I have tried numerous online examples, below is just the latest version
For example...
When I look at the application object in the Quick Watch, I get the following
MY CONFIGURATION:
Obviously, I have parsed-out all the other objects...
public ContainerRegistry()
{
Scan(
scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.LookForRegistries();
scan.AssembliesFromApplicationBaseDirectory(f => f.FullName.StartsWith("Demo.Common", true, null));
scan.AssembliesFromApplicationBaseDirectory(f => f.FullName.StartsWith("XXX.XXX.MeasurementContracts", true, null));
scan.AddAllTypesOf(typeof(IMeasurementContractsApplication));
scan.AddAllTypesOf(typeof(IInstanceProvider));
scan.SingleImplementationsOfInterface();
});
// --------
// NAMED INSTANCES - IInstanceProvider
For<IInstanceProvider>().Use<DistributionListProvider>();
For<IInstanceProvider>().Add<FirstDeliveryNoticeDocumentRecallManager>().Named("firstDeliveryNoticeDocumentRecallManager");
// --------
// APPLICATION
For<IMeasurementContractsApplication>().Use<MeasurementContractsApplication>()
// Component
.Setter(x => x.DistributionListProvider).Is<DistributionListProvider>()
.Setter(x => x.FirstDeliveryNoticeDocumentRecallManager).IsNamedInstance("firstDeliveryNoticeDocumentRecallManager");
}
APPLICATION Example:
Obviously, I have parsed-out all the other objects...
public class MeasurementContractsApplication : IMeasurementContractsApplication
{
[SetterProperty]
public IMeasurementContractsUnitOfWork UnitOfWork { get; set; }
[SetterProperty]
public IInstanceProvider DistributionListProvider { get; set; }
[SetterProperty]
public IInstanceProvider FirstDeliveryNoticeDocumentRecallProvider { get; set; }
}
IInstanceProvider's:
public class DistributionListProvider : ProviderBase, IInstanceProvider
{
// Purposely left-out Properties, Methods etc.
}
public class FirstDeliveryNoticeDocumentAdminUpdateProvider : ProviderBase, IInstanceProvider
{
// Purposely left-out Properties, Methods etc.
}
public class ProviderBase
{
[SetterProperty]
public IMeasurementContractsUnitOfWork UnitOfWork { get; set; }
}
----------------------------------------
UPDATES: From Questions Posed to Me
In order to bring things down to an EXTREMELY BASIC level...I decided to implement a MINIMAL set of 2 classes to try suggestions-on:
public interface ITesting
{
string Name();
}
public class Foo : ITesting
{
public string Name()
{
return string.Empty;
}
}
public class Bar : ITesting
{
public string Name()
{
return string.Empty;
}
}
public ContainerRegistry()
{
Scan(
scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.LookForRegistries();
scan.AssembliesFromApplicationBaseDirectory(f => f.FullName.StartsWith("Demo.Common", true, null));
scan.AssembliesFromApplicationBaseDirectory(f => f.FullName.StartsWith("XXX.MeasurementContracts", true, null));
scan.AddAllTypesOf(typeof(IMeasurementContractsApplication));
scan.AddAllTypesOf(typeof(IManager<>));
scan.AddAllTypesOf(typeof(IDocumentDependency));
scan.AddAllTypesOf(typeof(IDataItemProviderFor<>));
scan.AddAllTypesOf(typeof(IDatasetBuilderFor<>));
scan.AddAllTypesOf(typeof(IXmlTransformerFor<>));
scan.AddAllTypesOf(typeof(IWorkflowProvider));
scan.AddAllTypesOf(typeof(IInstanceProvider));
scan.AddAllTypesOf(typeof(IPdfConverterClient));
scan.AddAllTypesOf(typeof(IReportFor<>));
scan.AddAllTypesOf(typeof(IAdminUpdateCommandFor<>));
scan.AddAllTypesOf(typeof(ITesting));
scan.SingleImplementationsOfInterface();
});
Component Providers
For<IMeasurementContractsApplication>().Use<MeasurementContractsApplication>()
Setter(x => x.Bar).Is<Bar>()
Setter(x => x.Foo).Is<Foo>();
}
One of 2 outcomes always happens
- The property is NULL
- I get the following error message
"No default Instance is registered and cannot be automatically determined for type 'IInstanceProvider' No default instance is specified."
Q: Where are the target implementations located?
XXX.MeasurementContracts.Business
Contains the "ContainerRegistry" and all classes, interfaces etc.
XXX.MeasurementContracts.Web
Contains "StructuremapMvcConfig", the "IoC" initializer and its' own "DefaultRegistry"
MeasurementContracts.UnitTests
Adds the Business "ContainerRegistry" inside its "IoC" initializer...and then adds its own "ContainerRegistry" afterwards.
ATTEMPT: trying Named Registrations I added the following to the "ContainerRegistry", and while, BOTH are populated...they are of type "Bar"
// Component Providers
For<ITesting>().Use<Bar>().Named("Bar");
For<ITesting>().Add<Foo>().Named("Foo");
// Component Providers
For<IMeasurementContractsApplication>().Use<MeasurementContractsApplication>()
.Setter(x => x.Bar).Is<Bar>()
.Setter(x => x.Foo).Is<Foo>();
HOW DO I RESOLVE "Foo"? I have also tried ".Setter(x => x.Foo).Is(c => c.GetInstance("Foo"));"
ANALYSIS: Using container.WhatDoIHave() Okay, using "WhatDoIHave" displays that I have correctly configured the "ITesting" instances.
- Transient - XXX.MeasurementContracts.Business.Providers.Bar('bar') - bar (Default)
- Transient - XXX.MeasurementContracts.Business.Providers.Foo('foo') - foo
HOW DO I RESOLVE "Foo" & "Bar" into their respective Properties?