You don't have to create instances first. It all just works. That's the magic of IoC Containers Example: public interface ISecurityService { } public interface ISecurityRepository { } public class SecurityService : ISecurityService { public SecurityService(ISecurityRepository repository) { Console.
WriteLine("SecurityService created"); Console. WriteLine("Repository is " + repository); } public override string ToString() { return "A SecurityService"; } } public class SecurityRepository : ISecurityRepository { public SecurityRepository() { Console. WriteLine("SecurityRepository created"); } public override string ToString() { return "A SecurityRepository"; } } public class MyClassThatNeedsSecurity { public MyClassThatNeedsSecurity(ISecurityService security) { Console.
WriteLine("My class has security: " + security); } } class Program { static void Main() { using (IUnityContainer container = new UnityContainer()) { container.RegisterType() .RegisterType(); MyClassThatNeedsSecurity myClass = container.Resolve(); } } } This will print: SecurityRepository created SecurityService created Repository is A SecurityRepository My class has security: A SecurityService You have a number of options, such as pre-creating your instances (as you showed in your follow-up post) or extending the lifetime of injected dependencies so that they're not recreated every time they're needed. But for the base case, this will work.
You don't have to create instances first. It all just works. That's the magic of IoC Containers.
Example: public interface ISecurityService { } public interface ISecurityRepository { } public class SecurityService : ISecurityService { public SecurityService(ISecurityRepository repository) { Console. WriteLine("SecurityService created"); Console. WriteLine("Repository is " + repository); } public override string ToString() { return "A SecurityService"; } } public class SecurityRepository : ISecurityRepository { public SecurityRepository() { Console.
WriteLine("SecurityRepository created"); } public override string ToString() { return "A SecurityRepository"; } } public class MyClassThatNeedsSecurity { public MyClassThatNeedsSecurity(ISecurityService security) { Console. WriteLine("My class has security: " + security); } } class Program { static void Main() { using (IUnityContainer container = new UnityContainer()) { container.RegisterType() .RegisterType(); MyClassThatNeedsSecurity myClass = container.Resolve(); } } } This will print: SecurityRepository created SecurityService created Repository is A SecurityRepository My class has security: A SecurityService You have a number of options, such as pre-creating your instances (as you showed in your follow-up post) or extending the lifetime of injected dependencies so that they're not recreated every time they're needed. But for the base case, this will work.
1 Thans TrueWill! That worked, So see if I understand this correctly, Using registerTypes will create a new instance each time I do Resolve? And using my way in my followup uses the same instance each time I use resolve?
So I suppose if need to keep an object alive its better to use RegisterInstance rather than registerType? – mark smith Sep 27 '09 at 14:43 1 @mark: Yes, with the default RegisterType call a new instance will be created every time you call Resolve and the resolved type needs the dependency (directly or indirectly). Using RegisterInstance will give the same instance every time.
You can also use the overloads to have Unity create the instance once and only once per container (like a Singleton); from the Unity help: "myContainer. RegisterType(new ContainerControlledLifetimeManager());" – TrueWill Sep 27 '09 at 15:35 Thanks! .. great help.. – mark smith Sep 27 '09 at 15:50.
Here is some more information. The constructor of my class is public SecurityService(ISecurityRepository repository) : base(repository) { } After playing around a little bit, I managed to do the following but this causes me to create instances FIRST ... It seems to work.., but its an alternative. // Create unity container my service and repository ISecurityRepository securityRepository = new SecurityRepository(); ISecurityService securityService = new SecurityService(securityRepository); container = new UnityContainer(); container.
RegisterInstance(securityRepository); container. RegisterInstance(securityService).
You don't have to create instances first. It all just works. That's the magic of IoC Containers.
Here is some more information. The constructor of my class is.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.