P
Peter Morris
Hi all
I hope there is someone out there who is familiar with Unity? I have a
question...
//A service that needs to be implemented
public interface IMyService
{
void OutputString(string text);
}
//An implementation of this service
public class MyServiceImpl : IMyService
{
public void OutputString(string text)
{
Console.WriteLine(text);
}
}
//Something I don't want UNITY to create
public class DataContext
{
public string ID;
}
//A class that requires the service + the data context
public class Person
{
readonly DataContext DataContext;
readonly IMyService MyService;
public Person(DataContext dataContext, IMyService myService)
{
DataContext = dataContext;
MyService = myService;
}
public void DoIt(string text)
{
MyService.OutputString(text);
}
}
//Now the code to set up my container...
Container = new UnityContainer();
Container.RegisterType<IMyService, MyServiceImpl>(new
ContainerControlledLifetimeManager());
What I want to do now is to use my container to create an instance of
Person. I want Unity to pass in the IMyService, but I want to pass in the
DataContext myself...
DataContext dc = new DataContext();
dc.ID = "My Data Context";
//How do I send in my instance of DataContext?
Person p = Container.Resolve<Person>();
p.DoIt("hello");
Unity will create the DataContext instance itself. I want something like...
Person p = Container.Resolve<Person>(dc);
I know this exact code is not possible or I wouldn't be posting. What I
want to know is, is there a way I can pass in specific values to be used
when injecting dependencies?
Thanks
I hope there is someone out there who is familiar with Unity? I have a
question...
//A service that needs to be implemented
public interface IMyService
{
void OutputString(string text);
}
//An implementation of this service
public class MyServiceImpl : IMyService
{
public void OutputString(string text)
{
Console.WriteLine(text);
}
}
//Something I don't want UNITY to create
public class DataContext
{
public string ID;
}
//A class that requires the service + the data context
public class Person
{
readonly DataContext DataContext;
readonly IMyService MyService;
public Person(DataContext dataContext, IMyService myService)
{
DataContext = dataContext;
MyService = myService;
}
public void DoIt(string text)
{
MyService.OutputString(text);
}
}
//Now the code to set up my container...
Container = new UnityContainer();
Container.RegisterType<IMyService, MyServiceImpl>(new
ContainerControlledLifetimeManager());
What I want to do now is to use my container to create an instance of
Person. I want Unity to pass in the IMyService, but I want to pass in the
DataContext myself...
DataContext dc = new DataContext();
dc.ID = "My Data Context";
//How do I send in my instance of DataContext?
Person p = Container.Resolve<Person>();
p.DoIt("hello");
Unity will create the DataContext instance itself. I want something like...
Person p = Container.Resolve<Person>(dc);
I know this exact code is not possible or I wouldn't be posting. What I
want to know is, is there a way I can pass in specific values to be used
when injecting dependencies?
Thanks