StructureMap Dependencies for Arguments in Constructor - structuremap

If I have the following, and I were to say ObjectFactory.GetInstance<Master>() is it possible to tell StructureMap to make the I_A instance to A_User the same instance as the I_A passed to Master?
public interface I_A { }
public interface I_B { }
public class A_User {
public A_User(I_A A) { }
}
public class Master {
public Master(I_A _, I_B __, A_User ___) { }
}

The default behavior of StructureMap will always give you the same instance within a "build session" (effectively, a single call to GetInstance). You should not have to configure anything extra to get the behavior you want.
If it is not working as you expect, please post more details, or mention it on the StructureMap mailing list.

UPDATED:
As #Joshua Flanagan points out below, this is default SM behaviour. The following unit tests show that. The first tests uses the default behaviour. The second shows how you would get a unique instance if you wanted it:
using System;
using System.Collections.Generic;
using NUnit.Framework;
using StructureMap;
using StructureMap.Pipeline;
namespace SMTest
{
[TestFixture]
public class TestSOQuestion
{
class Foo : IFoo { }
interface IFoo { }
private interface IBar {
IFoo Foo { get; set; }
}
class Bar : IBar
{
public IFoo Foo { get; set; }
public Bar(IFoo foo)
{
Foo = foo;
}
}
class UsesFooAndBar
{
public IBar Bar { get; set; }
public IFoo Foo { get; set; }
public UsesFooAndBar(IFoo foo, IBar bar)
{
Foo = foo;
Bar = bar;
}
}
[Test]
public void TestOtherAnswer()
{
IContainer container = new Container(x =>
{
x.For<IFoo>().Use<Foo>();
x.For<IBar>().Use<Bar>();
});
var usesFooAndBar = container.GetInstance<UsesFooAndBar>();
Assert.AreSame(usesFooAndBar.Foo, usesFooAndBar.Bar.Foo);
}
[Test]
public void TestNonDefaultBehaviour()
{
IContainer container = new Container(x =>
{
x.For<IFoo>().AlwaysUnique().Use<Foo>();
x.For<IBar>().Use<Bar>();
});
var usesFooAndBar = container.GetInstance<UsesFooAndBar>();
Assert.AreNotSame(usesFooAndBar.Foo, usesFooAndBar.Bar.Foo);
}
}
}

Related

typemock threw InvalidCast exception

I have following code snipped and I tried to test it using typemock but got InvalidCast exception. ISomeClass is just an interface and SomeClass implements that interface. UseOfSomeClass is another class which uses SomeClass and its constructor takes SomeClass as parameter. I need to test UseOfSomeClass. How do I inject a typemock fake object into a class constructor?
public interface ISomeClass
{
void DoSomething();
}
public class SomeClass : ISomeClass
{
public void DoSomething()
{
Console.WriteLine("Do something!");
}
}
public class UseOfSomeClass
{
public SomeClass SomeClassProperty { get; set; }
public bool IsChecked { get; set; }
public UseOfSomeClass(SomeClass someClass)
{
SomeClassProperty = someClass;
}
public void MyMethod()
{
SomeClassProperty.DoSomething();
IsChecked = true;
}
}
Then test:
[TestClass]
public class UseOfSomeClassTest
{
[TestMethod]
public void TestMethod1()
{
var fakeSomeClass = Isolate.Fake.Instance<ISomeClass>();
var use = new UseOfSomeClass((SomeClass)fakeSomeClass);
use.MyMethod();
Assert.IsTrue(use.IsChecked);
}
}
Thanks!
Typemock Isolator allows you to mock concrete classes, so there's no need to fake ISomeClass in your test case. You can just fake SomeClass and send it as a parameter to the ctor.
[TestMethod, Isolated]
public void TestMethod1()
{
var fakeSomeClass = Isolate.Fake.Instance<SomeClass>();
var use = new UseOfSomeClass(fakeSomeClass);
use.MyMethod();
Assert.IsTrue(use.IsChecked);
}

Ninject Conditional Self bind to change scope (For Task-scheduler) not working properly?

Within MVC Web Application DbContext binding work properly with InRequestScope()
kernel.Bind<DbContext>().ToSelf().InRequestScope();
kernel.Bind<IUnitOfWork<DbContext>>().To<UnitOfWork<DbContext>>();
But from a Task Scheduler call DbContext in InRequestScope() unable to update Db Table (without any error), until I change Binding to InSingletonScope() OR InThreadScope()
Question: So is their any way change scope to InSingletonScope() / InThreadScope() for a Task Scheduler Call. ?
// For Task Scheduler Call, I tried bellow bind, but not working properly
kernel.Bind<DbContext>().ToSelf()
.When(request => request.Target.Type.Namespace.StartsWith("NameSpace.ClassName"))
.InSingletonScope();
** And probably I miss some thing. Need help.
Code Snippet Updated
#region Commented Code
public EmailTask() : this
( DependencyResolver.Current.GetService<IMessageManager>(),
, DependencyResolver.Current.GetService<IUnitOfWork<DbContext>>()) { }
#endregion
public EmailTask(IMessageManager messageManager, IUnitOfWork<DbContext> unitOfWork)
{
this._messageManager = messageManager;
this._unitOfWork = unitOfWork;
ProcessEmail();
}
public class NonRequestScopedParameter : IParameter { ... }
public void ProcessEmail()
{
var temp = SomeRepository.GetAll();
SendEmail(temp);
temp.Date = DateTime.Now;
SomeRepository.Update(temp);
unitOfWork.Commit();
}
public class ExecuteEmailTask : ITask
{
private readonly IResolutionRoot _resolutionRoot;
private int _maxTries = 5;
public ExecuteEmailTask(IResolutionRoot resolutionRoot)
{
_resolutionRoot = resolutionRoot;
}
public void Execute(XmlNode node)
{
XmlAttribute attribute1 = node.Attributes["maxTries"];
if (attribute1 != null && !String.IsNullOrEmpty(attribute1.Value))
{
this._maxTries = int.Parse(attribute1.Value);
}
/// send email messages
var task = _resolutionRoot.Get<EmailTask>(new NonRequestScopedParameter());
}
}
In Web.Config
<ScheduleTasks>
<Thread seconds="60">
<task name="ExecuteEmailTask" type="namespace.ExecuteEmailTask, AssemblyName" enabled="true" stopOnError="false" maxTries="5"/>
</Thread>
</ScheduleTasks>
In Global.asax
protected void Application_Start()
{
/* intialize Task */
TaskConfig.Init();
TaskManager.Instance.Initialize(TaskConfig.ScheduleTasks);
TaskManager.Instance.Start();
}
Ninject Bind Syntax
kernel.Bind<DbContext>().ToSelf().InRequestScope(); // Default bind
kernel.Bind<DbContext>().ToSelf()
.When(x => x.Parameters.OfType<NonRequestScopedParameter>().Any())
.InCallScope(); // For Scheduler
Note: EmailTask class also have SomeReposity as a Constructor Argument.
Queries:-
But what is the bind syntax to resolve TaskScheduler(IResolutionRoot resolutionRoot) ?
What is the configuration code to run TaskScheduler ?
As say to put IFakeDbContext directly into constructor, can this work with IUnitOfWork<FakeDbContext> ?
Problem
Task unable to call with Overloaded Constructor , it is only able to call TaskScheduler default Constructor.
Question 4: Can any way to invoke TaskScheduler(IResolutionRoot resolutionRoot) from TaskScheduler default constructor ?
Sample Code Snippet to create Task & run using System.Threading.Timer
private ITask createTask()
{
if (this.Enabled && (this._task == null))
{
if (this._taskType != null)
{
this._task = Activator.CreateInstance(this._taskType) as ITask;
}
this._enabled = this._task != null;
}
return this._task;
}
Question 5: Can I resolve TaskScheduler(IResolutionRoot resolutionRoot) here ?
Solved
public ExecuteEmailTask() :
this(DependencyResolver.Current.GetService<IResolutionRoot>())
OR
public ExecuteEmailTask() : this(new Bootstrapper().Kernel) { }
public ExecuteEmailTask(IResolutionRoot resolutionRoot)
{
_resolutionRoot = resolutionRoot;
}
First of, you should note that InSingletonScope() is usually a bad idea for DbContext's/Sessions. What happens if some other service changes data in the meantime? I would recommend investigating what effects this has.
For the scenario you first described, a correctly formulated .When(...) should work.
As an alternative to the .When(...) binding you could also use a .Named("FooBar") binding.
The constructor of the scheduled task would then need to look like:
ctor(Named["FooBar"] DbContext dbContext);
However, note, that this only (easily) works in case you need to inject the DbContext into a single constructor. If the task features dependencies and these need the same DbContext instance, too, it gets a bit tricker.
Since you updated your answer and say that this is the case, i would recommend an entirely different approach: Using a request parameter as basis for the When(...) condition combined with InCallScope binding. See below for an example.
Brace yourself, this is ab it of code :) The implementation requires the ninject.extensions.NamedScope extension (nuget).
I've also used xUnit and FluentAssertions nuget packages to execute the tests.
public class Test
{
// the two implementations are just for demonstration and easy verification purposes. You will only use one DbContext type.
public interface IFakeDbContext { }
public class RequestScopeDbContext : IFakeDbContext { }
public class CallScopeDbContext : IFakeDbContext { }
public class SomeTask
{
public IFakeDbContext FakeDbContext { get; set; }
public Dependency1 Dependency1 { get; set; }
public Dependency2 Dependency2 { get; set; }
public SomeTask(IFakeDbContext fakeDbContext, Dependency1 dependency1, Dependency2 dependency2)
{
FakeDbContext = fakeDbContext;
Dependency1 = dependency1;
Dependency2 = dependency2;
}
}
public class Dependency1
{
public IFakeDbContext FakeDbContext { get; set; }
public Dependency1(IFakeDbContext fakeDbContext)
{
FakeDbContext = fakeDbContext;
}
}
public class Dependency2
{
public IFakeDbContext FakeDbContext { get; set; }
public Dependency2(IFakeDbContext fakeDbContext)
{
FakeDbContext = fakeDbContext;
}
}
public class TaskScheduler
{
private readonly IResolutionRoot _resolutionRoot;
public TaskScheduler(IResolutionRoot resolutionRoot)
{
_resolutionRoot = resolutionRoot;
}
public SomeTask CreateScheduledTaskNow()
{
return _resolutionRoot.Get<SomeTask>(new NonRequestScopedParameter());
}
}
public class NonRequestScopedParameter : Ninject.Parameters.IParameter
{
public bool Equals(IParameter other)
{
if (other == null)
{
return false;
}
return other is NonRequestScopedParameter;
}
public object GetValue(IContext context, ITarget target)
{
throw new NotSupportedException("this parameter does not provide a value");
}
public string Name
{
get { return typeof(NonRequestScopedParameter).Name; }
}
// this is very important
public bool ShouldInherit
{
get { return true; }
}
}
[Fact]
public void FactMethodName()
{
var kernel = new StandardKernel();
// this is the default binding
kernel.Bind<IFakeDbContext>().To<RequestScopeDbContext>();
// this binding is _only_ used when the request contains a NonRequestScopedParameter
// in call scope means, that all objects built in the a single request get the same instance
kernel.Bind<IFakeDbContext>().To<CallScopeDbContext>()
.When(x => x.Parameters.OfType<NonRequestScopedParameter>().Any())
.InCallScope();
// let's try it out!
var task = kernel.Get<SomeTask>(new NonRequestScopedParameter());
// verify that the correct binding was used
task.FakeDbContext.Should().BeOfType<CallScopeDbContext>();
// verify that all children of the task get injected the same task instance
task.FakeDbContext.Should()
.Be(task.Dependency1.FakeDbContext)
.And.Be(task.Dependency2.FakeDbContext);
}
}
Since, as you say, the task scheduler does not make use of the IoC to create the task, it only supports a parameterless constructor. In that case you can make use DependencyResolver.Current (however, note that i'm in no way an expert on asp.net /MVC so i'm not making any claims that this is thread safe or working 100% reliably):
public class TaskExecutor : ITask
{
public TaskExecutor()
: this(DependencyResolver.Current.GetService<IResolutionRoot>())
{}
internal TaskExecutor(IResolutionRoot resolutionRoot)
{
this.resolutionRoot = resolutionRoot;
}
public void Execute()
{
IFooTask actualTask = this.resolution.Get<IFooTask>(new NonRequestScopedParameter());
actualTask.Execute();
}
}

Swapping nested dependency when requesting a specific object in StructureMap 3

I am trying to swap a nested dependency when resolving a specific instance using StructureMap. In 2.x I was able to do this with the following UseSpecial code, but it does not work in 3.x
The code is basically saying... when requesting an instance of IObject, swap the default instance for ITwo with AnotherTwo which is a dependency of IObject's dependency.
public class MyStructureMapRegistry : Registry
{
public MyStructureMapRegistry()
{
For<IObject>().UseSpecial(cfg => cfg.ConstructedBy(x =>
{
x.RegisterDefault(typeof(ITwo), x.GetInstance<AnotherTwo>());
return x.GetInstance<DependsOnOne>();
}));
}
}
The following is the sample object graph that I am trying to wire up.
public interface IObject { }
public interface IOne { }
public interface ITwo { }
public class DependsOnOne : IObject
{
IOne _one;
public DependsOnOne(IOne one)
{
_one = one;
}
}
public class DependsOnTwo : IOne
{
ITwo _two;
public DependsOnTwo(ITwo two)
{
_two = two;
}
}
public class Two : ITwo { }
public class AnotherTwo : ITwo { }

How to use Ninject to create instances in child classes?

I am learning how to use nInject for a new application I am developing, and I have created the following sample code that can be copied/pasted into a simple console app. It successfully returns an instance of IFoo, but I have a question about it.
How would I modify the code to have the FooManager class create an instance of the Foo object without doing a 'new'. Does the kernal have to be injected as well? But if the kernal is injected and I change the line to read var foo = _kernel.Get<IFoo>(), isn't that introducing a service locator anti-pattern?
namespace IOCTest
{
class Program
{
static void Main(string[] args)
{
using (IKernel kernel = new StandardKernel(new StandardModule()))
{
// do something with the kernal
var mgr = kernel.Get<IFooManager>();
var foo = mgr.GetById(1);
}
}
}
public class StandardModule : Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<IDatabase>()
.To<Database>()
.InTransientScope();
Bind<IFooManager>()
.To<FooManager>()
.InTransientScope();
}
}
//******************************************************
public interface IDatabase
{
object[] GetScalar(int id);
}
public class Database : IDatabase
{
public object[] GetScalar(int id)
{
return new object[] { "RowName" };
}
}
//******************************************************
public interface IFooManager
{
IFoo GetById(int id);
}
public class FooManager : IFooManager
{
private IDatabase _db;
public FooManager(IDatabase db) { _db = db; }
public IFoo GetById(int id)
{
var results = _db.GetScalar(id);
var foo = new Foo(); // <-- HOW DO I ELIMINATE THIS DEPENDENCY?
foo.Name = results[0].ToString();
return foo;
}
}
//******************************************************
public interface IFoo
{
string Name { get; set; }
}
public class Foo : IFoo
{
public string Name { get; set; }
}
//******************************************************
}
First you have to think about the purpose of Foo. Is this some kind of a datacontainer or some service?
In the first case your code is perfect like it is. Datacontainers have no dependencies and shouldn't be created by the IoC container.
In the second case read about Ninject.Extensions.Factory.
http://www.planetgeek.ch/2011/12/31/ninject-extensions-factory-introduction/
https://github.com/ninject/ninject.extensions.factory/wiki
There are a couple ways to eliminate that dependency. You could do the same thing you did with the Database dependency and use constructor injection. You could do property injection (https://github.com/ninject/ninject/wiki/Injection-Patterns). Another way, and perhaps what you are looking for, would be service location. To do that you can update your FooManager ctor to require an IKernel. This will be resolved automatically and you can then use the kernel that is passed in to get Foo.
public class FooManager : IFooManager
{
private IDatabase _db;
private IKernel _kernel;
public FooManager(IDatabase db, IKernel kernel) { _db = db; _kernel = kernel;}
public IFoo GetById(int id)
{
var results = _db.GetScalar(id);
// var foo = new Foo(); // <-- HOW DO I ELIMINATE THIS DEPENDENCY?
var foo = kernel.Get<IFoo>(); // Like this perhaps
foo.Name = results[0].ToString();
return foo;
}
}

Autofac vs. Structuremap, how do I inject all instances of an interface?

In autoFac, I can register multiple implementation of an interface. When autofac instantiates my object, all instances are passed to the constructor.
From autofac’s documentation: here
For example, when Autofac is injecting a constructor parameter of type
IEnumerable it will not look for a component that supplies
IEnumerable. Instead, the container will find all
implementations of ITask and inject all of them.
Is this functionality available in StructureMap?
For my classes:
public interface IFoo
{
}
public class Foo1 : IFoo
{
}
public class Foo2 : IFoo
{
}
public class UsingFoo
{
public UsingFoo(IEnumerable<IFoo> allFoos)
{
foreach (var foo in allFoos)
{
}
}
}
How do I register my implementations, so that when UsingFoo is instantiated, the constructor will be passed all implementations of IFoo?
In StructureMap you can do:
ObjectFactory.Intialize(x => x.Scan(y => y.AddAllTypesOf<IFoo>()));
That will register all types of IFoo
Then when you resolve UsingFoo, they will be injected.
Edit:
I just quickly wrote this up in a console app:
ObjectFactory.Initialize(x =>
{
x.Scan(y =>
{
y.AddAllTypesOf<IFoo>();
});
});
var usingFoo = ObjectFactory.GetInstance<UsingFoo>();
Edit:
You made me doubt myself, so I double checked.
It works fine.
Here's a working example I quickly wrote in a console app:
public interface IFoo
{
string Text { get; }
}
public class Foo1 : IFoo
{
public string Text
{
get { return "This is from Foo 1"; }
}
}
public class Foo2 : IFoo
{
public string Text
{
get { return "This is from Foo 2"; }
}
}
public class Bar
{
private readonly IEnumerable<IFoo> _myFoos;
public Bar(IEnumerable<IFoo> myFoos)
{
_myFoos = myFoos;
}
public void Execute()
{
foreach (var myFoo in _myFoos)
{
Console.WriteLine(myFoo.Text);
}
}
}
class Program
{
static void Main(string[] args)
{
ObjectFactory.Initialize(x =>
{
x.UseDefaultStructureMapConfigFile = false;
x.Scan(y =>
{
y.TheCallingAssembly();
y.AddAllTypesOf<IFoo>();
});
});
var myBar = ObjectFactory.GetInstance<Bar>();
myBar.Execute();
Console.WriteLine("Done");
Console.ReadKey();
}
}
The output is:
This is from Foo 1
This is from Foo 2
Done

Resources