Reputation: 647
Let's say that I have this simple class defined:
public class State
{
public int Id { get; set; }
public string Name { get; set; }
}
I will then define, create and register 50 named instances. User logs on and I know (say from his configuration settings) which state he's from. In my controller it would be easy to say
var userState= ObjectFactory.GetNamedInstance<State>("Idaho");
However I'd much rather pass it in a constructor, like this:
public class HomeController : ControllerBase
{
private State _state;
public HomeController(State state)
{
_state = state;
}
}
However, for this to work a particular instance would have to be passed. Is there a way to specify this?
Another thing I just thought off. Instead of defining 50 individual states I could define it as a collection of states, like this:
public class StateCollection
{
public List<State> States { get; set; }
}
And then pass StateCollection in a constructor. Client could then use LINQ and select appropriate state. Is that a reasonable approach? What if collections are large?
Upvotes: 0
Views: 229
Reputation: 172835
What you want to do is called context based injection. Although it might be possible to do so, context based injection is usually an indication of a error in the design of the application. In your case, you've seem to be missing a simple ICurrentUserService
abstraction, that provides information about the current user in the system:
public interface ICurrentUserService
{
State State { get; }
}
Client could then use LINQ and select appropriate state. Is that a reasonable approach?
No it probably isn't, because this puts the client in charge of finding the right state for the user, while you already know this. Again, use the right abstraction. This makes the client code much easier.
What if collections are large?
This sounds like premature optimization to me. If it's fast enough, it's fast enough. But what when the collection grows over time and it gets slower? Well, that might be awkward to fix when you implemented this as LINQ statements in the consumers, because now you will have to change all the consumers. If you write this LINQ statement inside an implementation of the ICurrentUserService
abstraction (or whatever you want to call it), it's just a matter of one simple rewrite. Just change the LINQ query to the use of a Dictionary<string, State>
and you're done.
Upvotes: 2