public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .
public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .public interface IBar { } public interface IFoo { } public class Bar : IBar { } public class Foo : IFoo { public IBar Bar { get ; private set ; } public Foo(IBar bar) { Bar = bar; } }
* This source code was highlighted with Source Code Highlighter .
- [TestMethod]
- public void RegisterTypeAndGetInstance ()
- {
- var container = new Container ();
- container.Register <iBar> (() => new Bar ());
- var bar = container.Resolve <iBar> ();
- Assert.IsNull (bar);
- Assert.IsTrue (bar is Bar);
- }
* This source code was highlighted with Source Code Highlighter .
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- namespace Funq1.Tests
- {
- [TestClass]
- public class ContainerFixture
- {
- [TestMethod]
- public void RegisterTypeAndGetInstance ()
- {
- var container = new Container ();
- container.Register <iBar> (() => new Bar ());
- var bar = container.Resolve <iBar> ();
- Assert.IsNull (bar);
- Assert.IsTrue (bar is Bar);
- }
- public interface IBar {}
- public interface IFoo {}
- public class Bar: IBar {}
- public class Foo: IFoo
- {
- public IBar Bar { get ; private set ; }
- public foo (ibar bar)
- {
- Bar = bar;
- }
- }
- }
- }
* This source code was highlighted with Source Code Highlighter .
- Dictionary <Type, object > factories = new Dictionary <Type, object > ();
* This source code was highlighted with Source Code Highlighter .
- public void Register <TService> (Func <TService> factory)
- {
- factories.Add ( typeof (TService), factory);
- }
* This source code was highlighted with Source Code Highlighter .
- public TService Resolve <TService> ()
- {
- object factory = factories [ typeof (TService)];
- return ((Func <TService>) factory) .Invoke ();
- }
* This source code was highlighted with Source Code Highlighter .
- using System;
- using System.Collections. Generic ;
- namespace Funq1
- {
- public class Container
- {
- Dictionary <Type, object > factories = new Dictionary <Type, object > ();
- public void Register <TService> (Func <TService> factory)
- {
- factories.Add ( typeof (TService), factory);
- }
- public TService Resolve <TService> ()
- {
- object factory = factories [ typeof (TService)];
- return ((Func <TService>) factory) .Invoke ();
- }
- }
- }
* This source code was highlighted with Source Code Highlighter .
- container.Register <iBar> (() => new Bar ());
- container.Resolve <IFoo> (() => new Foo (container.Resolve <IBar> ()));
* This source code was highlighted with Source Code Highlighter .
- container.Register <iBar> (c => new Bar ());
- container.Register <IFoo> (c => new Foo (c.Resolve <IBar> ()));
* This source code was highlighted with Source Code Highlighter .
- public void Register <TService> (Func <Container, TService> factory)
- {
- factories.Add ( typeof (TService), factory);
- }
- public TService Resolve <TService> ()
- {
- object factory = factories [ typeof (TService)];
- return ((Func <Container, TService>) factory) .Invoke ( this );
- }
* This source code was highlighted with Source Code Highlighter .
- container.Register <iBar> (c => new Bar ());
* This source code was highlighted with Source Code Highlighter .
- [TestMethod]
- public void ResolveGetsDepedenciesInjected ()
- {
- var container = new Container ();
- container.Register <iBar> (c => new Bar ());
- container.Register <IFoo> (c => new Foo (c.Resolve <IBar> ()));
- var foo = container.Resolve <IF> () as Foo;
- Assert.IsNotNull (foo);
- Assert.IsNotNull (foo.Bar);
- }
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/53922/
All Articles