structuremap

Does Structuremap support Lazy out of the box?

落花浮王杯 提交于 2019-12-01 00:17:22
问题 Does structuremap allow you to do constructor injection in a lazy fashion? Meaning not creating the object which is injected until it is used? 回答1: UPDATE: StructureMap v3 implements this out of the box, so this trick is no longer necessary. StructureMap version 2 doesn't, but with a few tricks you can get it to do what I believe you are looking for. First of all, you can already wire up Lazy<T> instances manually like this: container = new Container(x => { x.Scan(y => { y.TheCallingAssembly(

Call multiple classes with the same interface

浪子不回头ぞ 提交于 2019-11-30 23:23:31
I have an interface like public interface IAddressProvider { string GetAddress(double lat, double long); } In my consuming class I want to cycle through the concrete providers until I get a result, like (simplified): string address; address = _cachedAddressProvider.GetAddress(lat, long); if(address == null) address = _localDbAddressProvider.GetAddress(lat, long); if(address = null) address = _externalAddressProvider.GetAddress(lat, long); return address ?? "no address found"; I can then mock each provider for unit testing, setting null as the return value to appropriately test all code paths.

Telling StructureMap to use another Constructor

爷,独闯天下 提交于 2019-11-30 19:29:53
I have a class with 2 constructors. MyClass() and MyClass(IMyService service) How do I tell StructureMap then whenever I do a 'new MyClass()' it should actually call the second constructor and not the first constructor. Please help. If you call new MyClass() , then StructureMap is not involved at all. No amount of StructureMap configuration will change the behavior. If you call ObjectFactory.GetInstance<MyClass>() , StructureMap will by default call the constructor with more parameters. If you want StructureMap to use a different constructor, you can specify the constructor (via PHeiberg's

Call multiple classes with the same interface

不打扰是莪最后的温柔 提交于 2019-11-30 18:05:53
问题 I have an interface like public interface IAddressProvider { string GetAddress(double lat, double long); } In my consuming class I want to cycle through the concrete providers until I get a result, like (simplified): string address; address = _cachedAddressProvider.GetAddress(lat, long); if(address == null) address = _localDbAddressProvider.GetAddress(lat, long); if(address = null) address = _externalAddressProvider.GetAddress(lat, long); return address ?? "no address found"; I can then mock

Mapper not initialized, When Use ProjectTo()

人盡茶涼 提交于 2019-11-30 17:17:09
I Use Automapper 5.2.0 In My Project. When I Use ProjectTo() In Code Get This Error: Mapper not initialized. Call Initialize with Appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance. Service Code public async Task<FreelancerProfileViewModel> GetFreelancerProfile() { var id = Guid.Parse(_identity.GetUserId()); var model = await

How to define a Structuremap named instance in Code

て烟熏妆下的殇ゞ 提交于 2019-11-30 13:56:26
问题 I want to create a Structuremap named instance in code, without config file I want to be able to create the instance like this: var namedInjector = ObjectFactory.GetNamedInstance<IInjectable>("Other"); I cant define such a type in code. I have found this sample but it uses the old syntax of a previous version and defines the named instance as: .ForRequestedType<MementoType>() .AddConcreteType<ConcreteType>(instanceName) In the latest structuremap version there is no .AddConcreteType

DI/IoC Container Performance Benchmark Comparison?

萝らか妹 提交于 2019-11-30 13:49:57
问题 I've found some 2008 benchmark results for testing the performance of several of the top .NET DI/IoC containers here. But I haven't been able to find any updated results. Are there any benchmarks out there that compare some of the big IoC containers (StructureMap, Unity, Ninject, Autofac, Castle Windsor, etc.)? 回答1: I would not recommend using performance benchmarks to pick an IoC container. There are many, many more important factors, such as feature set, development roadmap and

StructureMap IRegistrationConvention to register non default naming convention?

我的未来我决定 提交于 2019-11-30 13:42:31
I currently have a bunch of repositories like so IMyRepository IAnotherRepository They all inherit from IRepository (if this helps) How can I get structuremap to use an IRegistryConvention scanner to register my concrete types which are named SqlMyRepository SqlAnotherRepository I had read that article but it didn't give me quite what I needed. The AddAllTypesOf registered all the concrete types against the IRepositoryInterface but instead I require that each concrete type is registered against the interface with equivilent naming. ie. For<IMyRepository>().Use<SqlMyRepository>(); Also I need

How to configure ASP.NET Identity ApplicationUserManager with StructureMap

天涯浪子 提交于 2019-11-30 13:01:50
I am using asp.net identity in my project and using structuremap as DI framework. the problem is when i use constructor injection then ApplicationUserManager not configured all of it's members e.g TokenProvider, ... this is my ApplicationUserManager class: public class ApplicationUserManager : UserManager<User, long> { public ApplicationUserManager(IUserStore<User, long> store) : base(store) { } public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) { var manager = new ApplicationUserManager(new CustomUserStore(context.Get

How do I use StructureMap with generic unclosed types using Scan with a “greedy” constructor

天涯浪子 提交于 2019-11-30 12:28:18
问题 Between various Stack Overflow questions and blog posts there is a pretty reasonable amount of documentation on the topic of open generics and StructureMap. Unfortunately, I must be missing something as my attempts at using scan to perform the configuration along with a class implementation that has a "greedy" constructor have yet work. I want StructureMap to grab an instance of the below class via references to its implemented interface. ToCsvService exists in an unreferenced assembly called