structuremap

Asp.net MVC RouteBase and IoC

倾然丶 夕夏残阳落幕 提交于 2019-12-03 09:09:05
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. 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 general solution to the problem. I'll try to explain the part that is responsible for plain custom HTML-pages

Dispose of Injected HttpClient

十年热恋 提交于 2019-12-03 09:08:50
Our MVC application calls a WebAPI action using HttpClient. I decided to inject the HttpClient using StructureMap and override dispose in the controller public HomeController(HttpClient httpClient) { _httpClient = httpClient; } protected override void Dispose(bool disposing) { if (disposing && _httpClient != null) { _httpClient.Dispose(); } base.Dispose(disposing); } The StructureMap ObjectInitialize basically looks like this.. x.For<HttpClient>().Use(() => new HttpClient() { BaseAddress = "my/uri/"}); When I build this, CodeAnalysis complains "Dispose objects before losing scope" and points

Injecting multi-tenant repositories with StructureMap in ASP.NET MVC

无人久伴 提交于 2019-12-03 07:52:15
问题 I'm implementing StructureMap in a multi-tenant ASP.NET MVC application to inject instances of my tenant repositories that retrieve data based on an ITenantContext interface. The Tenant in question is determined from RouteData in a base controller's OnActionExecuting . How do I tell StructureMap to construct TenantContext(tenantID); where tenantID is derived from my RouteData or some base controller property? Base Controller Given the following route: {tenant}/{controller}/{action}/{id} My

How to configure StructureMap for asp.net MVC 5

二次信任 提交于 2019-12-03 07:06:32
问题 I'm getting below error. I setup it similar to asp.net mvc 4. No parameterless constructor defined for this object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.MissingMethodException: No parameterless constructor defined for this object. Finally found the actual exception "Activation error occured while trying to

Dependency Injection and development productivity

早过忘川 提交于 2019-12-03 06:58:10
Abstract For the past few months I have been programming a light weight, C# based game engine with API abstraction and entity/component/scripting system. The whole idea of it is to ease the game development process in XNA, SlimDX and such, by providing architecture similar to that of the Unity engine. Design challenges As most game developers know, there are a lot of different services you need to access throughout your code. Many developers resort to using global static instances of e.g. a Render manager(or a composer), a Scene, Graphicsdevice(DX), Logger, Input state, Viewport, Window and so

StructureMap register generic types against all possible concrete implementations

老子叫甜甜 提交于 2019-12-03 06:57:14
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 void Execute(ICommand command) { Console.WriteLine("{0}: Have seen a command of type {1}", GetType()

Inject AutoMapper

喜欢而已 提交于 2019-12-03 06:50:06
I have been working on injecting AutoMapper into controllers. I like the implementation of Code Camp Server. It creates a wrapper around AutoMapper's IMappingEngine. The dependency injection is done using StructureMap. But I need to use Castle Windsor for my project. So, how do we implement the following dependency injection and set-up using Windsor? I am not looking for line-by-line equivalent implementation in Castle Windsor. If you want to do that, please feel free. Instead, what is Windsor equivalent of StructureMap's Registry and Profile? I need Profile to define CreateMap<> like the

StructureMap controller factory and null controller instance in MVC

末鹿安然 提交于 2019-12-03 06:22:39
I'm still trying to figure things out with StructureMap and one of the issues i'm running into is my Controller Factory class blowing up when a null controller type is passed to it. This only happens when the application builds for the first time, after which every subsequent build works fine. Even when i shutdown Visual Studio and reopen the project (I'm not running this in IIS). It's almost like there is some sort of caching going on. This is what the controller class looks like: public class IocControllerFactory : DefaultControllerFactory { protected override IController

ASP.NET MVC: HTTPContext and Dependency Injection

佐手、 提交于 2019-12-03 03:54:39
问题 Currently I have an ActionFilter that gets the current users name from HttpContext and passes it into the action which uses it on a service method. eg: Service.DoSomething(userName); I now have a reason to do this not at the action level but the controller constructor level. Currently I'm using structure map to create controllers and inject the service. I'm looking at something like: public interface IUserProvider { string UserName { get; } } public class HttpContextUserProvider :

What conventions/idioms/patterns are you using configuring IOC Containers using the new Fluent Interfaces

笑着哭i 提交于 2019-12-03 03:26:59
I am in the middle of moving over a large body of code to Castle Trunk which includes the new fluent interface for configuring the container. Since the project has a huge windsorConfig xml file that is beyond maintainable, I thought I would start to take advantage of this new feature. I know other containers (e.g. StructureMap 2.0) also contain fluent interfaces for container configuration, so this question isn't based around Windsor. My question is what conventions/idioms/patterns are you using for container configuration using the new fluent style interfaces? My first thought was to create a