structuremap

Injecting ISession Into My Repositories Using Structuremap in a Asp.Net MVC Application

≯℡__Kan透↙ 提交于 2019-11-30 10:18:09
My repositories all take ISession in the constructor: protected Repository(ISession session) { this.session = session; } private readonly ISession session; In an Asp.Net MVC application, using StructureMap, how would I go about setting up ISession in my the StructureMap Registry? Would I need to to add SessionFactory to the container also? Does FluentNHibernate change things? Dmytrii Nagirniak You should register ISession using a factory method. Another options (not always the best, but easy to use) is to: Implement ISession and ISessionFactory interfaces (SessionProxy and SessionFactoryProxy)

Named Instances and a Default Instance in StructureMap?

丶灬走出姿态 提交于 2019-11-30 09:19:11
In my StructureMap bootstrapping code I'm using a custom convention to scan assemblies and add interface/implementation pairs to the object graph as named instances. Essentially I have some logic which checks configuration settings and drills down to this statement depending on various conditions: registry.For(interfaceType).Use(type) .Named(implementationName); This adds all of the named instances well enough. However, I'd also like to add a default instance in the event that an instance name is not specified. However, the default instance isn't always the last one added to the graph.

StructureMap is not disposing data context when using HttpContextScoped()

不想你离开。 提交于 2019-11-30 08:58:33
My goal is to have one data context ( MainDbContext ) per HTTP request in ASP.NET MVC and dispose the data context when the request ends. I'm using the following StructureMap configuration: public static class ContainerConfigurer { public static void Configure() { ObjectFactory.Initialize(x => { x.For<MainDbContext>().HttpContextScoped(); }); } } Whenever I need a MainDbContext , I'm using this code: var dbContext = ObjectFactory.GetInstance<MainDbContext>(); This is working as expected: only one data context is being created per HTTP request. The problem is, MainDbContext is not being

Issues with my MVC repository pattern and StructureMap

浪尽此生 提交于 2019-11-30 07:47:13
I have a repository pattern i created on top of the ado.net entity framework. When i tried to implement StructureMap to decouple my objects, i kept getting StackOverflowException (infinite loop?). Here is what the pattern looks like: IEntityRepository where TEntity : class Defines basic CRUD members MyEntityRepository : IEntityRepository Implements CRUD members IEntityService where TEntity : class Defines CRUD members which return common types for each member. MyEntityService : IEntityService Uses the repository to retrieve data and return a common type as a result (IList, bool and etc) The

Telling StructureMap to use another Constructor

夙愿已清 提交于 2019-11-30 03:41:21
问题 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. 回答1: 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.

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

本小妞迷上赌 提交于 2019-11-30 02:44:47
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 Infrastructure. IToCsvService exists in a referenced assembly called Core. As you can see ToCsvService

Using Ninject (or some other container) How can I find out the type that is requesting the service?

孤街醉人 提交于 2019-11-29 15:39:46
问题 Suppose I have an interface for a service: public interface IFooService { void DoSomething(); } And a concrete implementation of that service that is a generic: public class FooService<TRequestingClass> : IFooService { public virtual void DoSomething() { } } And I have some other class that needs an instance of IFooService: public class Bar { private IFooService _fooService; public Bar(IFooService fooService) { this._fooService = fooService; } } I need to wire up my IoC container such that

StructureMap is not disposing data context when using HttpContextScoped()

家住魔仙堡 提交于 2019-11-29 13:04:37
问题 My goal is to have one data context ( MainDbContext ) per HTTP request in ASP.NET MVC and dispose the data context when the request ends. I'm using the following StructureMap configuration: public static class ContainerConfigurer { public static void Configure() { ObjectFactory.Initialize(x => { x.For<MainDbContext>().HttpContextScoped(); }); } } Whenever I need a MainDbContext , I'm using this code: var dbContext = ObjectFactory.GetInstance<MainDbContext>(); This is working as expected: only

How to inject AutoMapper IMappingEngine with StructureMap

十年热恋 提交于 2019-11-29 12:27:16
问题 Most of the examples I've found for Automapper use the static Mapper object for managing type mappings. For my project, I need to inject an IMapperEngine as part of object construction using StructureMap so that we can mock the mapper in unit tests so we can't use the static mapper. I also need to support configuring AutoMapper Profiles. My question is how can I configure the StructureMap registry so that it can supply an instance of IMappingEngine when an instance of MyService is constructed

“No IUserTokenProvider is registered” when using structuremap dependency injection

≡放荡痞女 提交于 2019-11-29 11:18:09
I have an MVC 5 project that has been modified to use int as the primary key for identity as shown in this guide I then enabled email confirmation as described in this guide Everything worked fine as expected. Then I installed structuremap.mvc5 for dependency injection and added modified DefaultRegistry.cs to public DefaultRegistry() { Scan( scan => { scan.TheCallingAssembly(); scan.WithDefaultConventions(); scan.AssemblyContainingType(typeof(MyProject.Data.MyDbContext)); scan.With(new ControllerConvention()); }); //For<IExample>().Use<Example>(); For<IUserStore<ApplicationUser, int>>().Use