structuremap

How can I enrich object composition in StructureMap without invoking setter injection?

大憨熊 提交于 2019-12-10 16:55:05
问题 I'm trying to build an implementation of the IHttpControllerActivator interface for with with StructureMap, so that I can resolve a dependency of a controller which takes a dependency on the HttpRequestMessage being processed in the MVC Web API pipeline. My implementation of Create is as follows: public IHttpController Create( HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) { return (IHttpController)this.Container .With(request) .With

Multiple constructor with Structuremap changing the scope?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 16:53:01
问题 To illustrate the problem, here is a simplified version of my setup. I have a factory like this one : public interface IFactory{ } public class Factory : IFactory { public Factory() { Console.WriteLine("parameterless"); } //public Factory(int i) //{ // Console.WriteLine("with parameter : {0}", i); //} } the program to test this is a consoleApp. Enough to prove my point. static void Main(string[] args) { Init(); var factory1 = ObjectFactory.GetInstance<IFactory>(); var factory2 = ObjectFactory

StructureMap: How to set lifecycle on types connected with ConnectImplementationsToTypesClosing

China☆狼群 提交于 2019-12-10 14:14:50
问题 In my registry I have Scan(scanner => { scanner.AssemblyContainingType<EmailValidation>(); scanner.ConnectImplementationsToTypesClosing(typeof(IValidation<>)); }); What am I supposed to do to define these all as Singletons? Also as an aside to this question, is there any reason to not define everything that is stateless as a singleton object that's registered in StructureMap? 回答1: Kevin's answer is correct for versions 2.5.4 and older. In the current StructureMap trunk (and when 2.5.5+ is

Tell StructureMap to use a specific constructor

故事扮演 提交于 2019-12-10 12:27:58
问题 I have two services that require an XPathDocument . I want to be able to define named instances of XPathDocumnet to use in the configuration of the two services. I also want to be able to tell StuctureMap which constructor of XPathDocument to use. When I try to get an instance of XPathDocument it tells me that it can't find the plugged type for XmlReader . I want to use the constructor that requires a string uri for the xml file. I cannot seem to get this to work. Here is the StructureMap

Named singleton instance in StructureMap (Multiple nHibernate session factories)

那年仲夏 提交于 2019-12-10 08:42:39
问题 I have a scenario where I have two Nhibernate SessionFactorys I need to register an use with StructureMap. Only Foo needs mySessionFactory sessions. Like this: For<ISessionFactory>().Singleton().Use(NHibernateConfiguration.GetDefaultSessionFactory()); For<ISession>().HybridHttpOrThreadLocalScoped().Use(x => x.GetInstance<ISessionFactory>().OpenSession()); For<ISessionFactory>().Singleton().Use(AnotherNHibernateConfiguration.GetDefaultSessionFactory).Named("mySessionFactory"); For<ISession>()

StructureMap Constructor arguments

谁说我不能喝 提交于 2019-12-10 00:59:00
问题 I'm new to structureMap. How do I define constructor arguments for the following class with fluent configuration? Thanks public BlobContainer(CloudStorageAccount account , string containerName , string contentType , BlobContainerPermissions blobContainerPermissions) { } 回答1: For primitive types you would go about as @ozczecho answered: For<BlobContainer>() .Use<BlobContainer>() .Ctor<string>("containerName").Is("theContainerName") .Ctor<string>("contentType").Is("theContentType"); provided

StructureMap: How to replace object at runtime

霸气de小男生 提交于 2019-12-09 18:44:05
问题 I am trying to inject mocked instance of ISession (NHibernate) to structure map. Currently it all wires it up in a Bootstrap method, but I want to replace the one that is injected with a mocked one. I tried EjectAllInstancesOf but it throw execption. [TestFixtureSetUp] public void TestFixtureSetup() { Bootstrapper.Bootstrap(); //TODO: need to remove already wired up types that we are mocking. var mockSession = MockRepository.GenerateStub<ISession>(); var mockLoggerFactory = MockRepository

Which PreApplicationStartMethod should I use?

给你一囗甜甜゛ 提交于 2019-12-09 09:42:35
问题 I noticed that when I installed StructureMap from NuGet into my ASP.NET MVC3 project, Dave Ebbo's WebActivator package was also added as a dependency. WebActivator provides a PreApplicationStartMethod attribute and, in the boilerplate code added at install time, it is used to initialise the IoC container and dependency resolver in it's own class, instead of doing this inside Global.asax 's Application_Start method. Given that ASP.NET 4 already has its own System.Web

Asp.net MVC RouteBase and IoC

一曲冷凌霜 提交于 2019-12-09 07:18:28
问题 I am creating a custom route by subclassing RouteBase. I have a dependency in there that I'd like to wire up with IoC. The method GetRouteData just takes HttpContext, but I want to add in my unit of work as well....somehow. I am using StructureMap, but info on how you would do this with any IoC framework would be helpful. 回答1: Well, here is our solution. Many little details may be omitted but overall idea is here. This answer may be a kind of offtop to original question but it describes the

StructureMap register generic types against all possible concrete implementations

房东的猫 提交于 2019-12-09 06:07:55
问题 I have the following: public interface ICommand { } public class AddUser : ICommand { public string Name { get; set; } public string Password { get; set; } } public interface ICommandHandler<T> : IHandler<T> where T : ICommand { void Execute(T command); } public class AddUserHandler : ICommandHandler<AddUser> { public void Execute(AddUser command) { Console.WriteLine("{0}: User added: {1}", GetType().Name, command.Name); } } public class AuditTrailHandler : ICommandHandler<ICommand> { public