0

Trying to implement Dependency Injection in an ASP.Net Web API project.

I would like to be able to inject an instance of Account into some of my services.

The Account instance should be created with the users Guid and this is not known until runtime.

So in my service I have:

        public TransactionService(Account acc)
        {
            _account = acc;
        }

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

container.RegisterType<Instanet.Engine.Account>(new InjectionConstructor(new Guid("xxxxxx")));

This, of course, isn't any good as it would be using the same Account for every user/request etc.

If I try to use something like :

container.RegisterType<Instanet.Engine.Account>(new InjectionConstructor(GetTheUsersID()));

... where GetTheUsersID() needs to either examine a cookie or the ASP.Net Identity request it's of course not available in the app startup.

So - Where/How (in simple terms please, this DI stuff is hurting my brain) do I implement this so I can inject an instanced Account into any of the services that may need it.

2 Answers 2

1

You generally don't want to mix state and behavior for components that get resolved via the container--DI should be used for components that can be modeled as pure services.

That said, sometimes it makes sense to wrap global or context-specific state in a service component.

In your case, if you only need the UserId locally in a one or more services (in other words, not passing it from one service to another). You mentioned being able to get the UserId from a cookie, so maybe it would look something like:

public class CookieService : ICookieService
{
  public int GetCurrentUserId()
  {
    //pseudo code
    return HttpContext.Current.GetCookie["UserId"];
  }
}

Now you can inject ICookieService where a UserId is needed.

More complex cases may require an Abstract Factory:

http://blog.ploeh.dk/2012/03/15/ImplementinganAbstractFactory/

Sign up to request clarification or add additional context in comments.

1 Comment

I couldn't agree more, about mixing behaviour a little. The problem I have though is that so many of legacy methods the services interact with require an account instance - so until our new API is built I have to use the legacy stuff. DI just seems like the better way to have the account ready, rather than newing it up in the services. I do like your idea though, i'll give it a whirl tomorrow :D thanks for the great info.
0

If there is only one Account instance possible for the session, then I would create an Account instance in the bootstrap code before all your services are running.

Then you can populate the guid and all other data in your account instance, and register the initialized instance of Account class in Unity via container.RegisterInstance method.

Later it will resolve to what you need.

Does it help?

1 Comment

Thanks, though doesn't really help much. The bootstrap code is, as far as I understand, there to run when the app first fires up. Hence the issue - some info I need isn't available at this time. The Account is also legacy stuff, which makes it more difficult.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.