I don't know the IoC framework you are using.
I do know Spring.NET - in Spring you specify in the config whether
an object should be singleton (in this context this just means
the same instance used everywhere - it is not the GoF singleton)
or a new instance should be created for each reference.
Here comes a Spring.NET example.
code
----
using System;
using Spring.Context;
using Spring.Context.Support;
namespace E
{
public interface I
{
string WhoAmI();
}
public class X : I
{
private static int totno = 0;
private int no;
private I o;
public X(I o)
{
this.o = o;
totno++;
no = totno;
}
public string WhoAmI()
{
return "I am X " + no + "/" + totno + " with: " + o.WhoAmI();
}
}
public class Y : I
{
private static int totno = 0;
private int no;
public Y()
{
totno++;
no = totno;
}
public string WhoAmI()
{
return "I am Y " + no + "/" + totno;
}
}
public class Program
{
public static void Main(string[] args)
{
IApplicationContext ctx = ContextRegistry.GetContext();
for(int i = 0; i < 5; i++)
{
I o = (I)ctx.GetObject ("X");
Console.WriteLine(o.WhoAmI());
}
Console.ReadKey();
}
}
}
app.config
----------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context"
type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects"
type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="
http://www.springframework.net">
<object name="X" type="E.X" singleton="false">
<constructor-arg ref="Y"/>
</object>
<object name="Y" type="E.Y" singleton="true"/>
</objects>
</spring>
</configuration>
Output
------
I am X 1/1 with: I am Y 1/1
I am X 2/2 with: I am Y 1/1
I am X 3/3 with: I am Y 1/1
I am X 4/4 with: I am Y 1/1
I am X 5/5 with: I am Y 1/1
Arne