Last active
December 18, 2015 03:35
-
-
Save syed-afraz-ali/53113b10aea734867b43 to your computer and use it in GitHub Desktop.
Configuring Structuremap in Console Applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net.NetworkInformation; | |
using System.Text; | |
using System.Threading.Tasks; | |
using MediatR; | |
using StructureMap; | |
namespace MediatR_101 | |
{ | |
public class IoC | |
{ | |
public static IContainer GetContainer() | |
{ | |
return new Container(cfg => | |
{ | |
cfg.Scan(scanner => | |
{ | |
scanner.AssemblyContainingType<Ping>(); | |
scanner.AssemblyContainingType<IMediator>(); | |
scanner.WithDefaultConventions(); | |
scanner.ConnectImplementationsToTypesClosing(typeof(IRequestHandler<,>)); | |
scanner.ConnectImplementationsToTypesClosing(typeof(IAsyncRequestHandler<,>)); | |
scanner.ConnectImplementationsToTypesClosing(typeof(INotificationHandler<>)); | |
scanner.ConnectImplementationsToTypesClosing(typeof(IAsyncNotificationHandler<>)); | |
}); | |
cfg.For<SingleInstanceFactory>().Use<SingleInstanceFactory>(ctx => t => ctx.GetInstance(t)); | |
cfg.For<MultiInstanceFactory>().Use<MultiInstanceFactory>(ctx => t => ctx.GetAllInstances(t)); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using StructureMap; | |
namespace MediatR_101 | |
{ | |
class Program | |
{ | |
private static IContainer _container; | |
static void Main(string[] args) | |
{ | |
_container = IoC.GetContainer(); | |
// We should get an instance of SimpleController with all its dependencies | |
var controller = _container.GetInstance<SimpleController>(); | |
Console.ReadLine(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MediatR; | |
namespace MediatR_101 | |
{ | |
public class SimpleController | |
{ | |
private IPersonRepository _repository; | |
public SimpleController(IPersonRepository repository) | |
{ | |
_repository = repository; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment