c# - Unity Dependency Injection - How to create the instance to be injected at runtime -


ASP.Net is trying to implement dependency injection in the web API project.

I am able to inject a frequency of account in some of my services.

The user should create a account example Guid and this runtime is not known.

So I have in my service:

  Public Transaction Service (Account ACC) {_account = ACC; }   

And in my application startup, I can do this - where the container is a new UnityContainer :

  container.RegisterType & Lt; Instanet.Engine.Account & gt; (New injection congestion (New Guid ("xxxxxx"));   

This is not good at all because it will use the same code for each user / request etc.

If I try to use something like this :

  container. Registration Type & lt; Instanet.Engine.Account & gt; (New Injection Constructor (GetTheUsersID ());   

... Where either GetTheUsersID () is required to check a cookie or ASP.NET identity request, this is definitely not available in App Startup.

So - where / how (please, in simple terms, this digit is hurting my mind) I apply it so that I instanced account can be injected.

You generally do not want to mix behaviors for state and components, which are resolved through the container They should be used for DI - those components can be prepared as pure services.

He said, sometimes it is understood to wrap a global or context-specific situation as a component.

In your case, if you need a local ID in one or more services locally (in other words, do not pass it from one service to another) you have mentioned that the cookie is userId If possible, you might see something like this:

  Public class cookie service: ICiki service {Public Injects GetCurrentUserId () {// Pseudo Code Returns HttpContext.Current .GetCookie ["User Id "]; }}   

Now you can inject ICookieService where a UserId is required.

More complex cases may require an abstract factory:



Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -