Jamie Dixon
Jamie Dixon

Reputation: 53991

Dependency Injection for built in types

This question may well have been asked before but I didn't find anything whilst searching SO.

When using Dependency Injection, how do you normally handle types such as lists, network credentials etc.

At the moment in one of my services constructors I have:

_itemsCheckedForRelations = new List<Guid>();
_reportManagementService.Credentials = new NetworkCredential([...]);

Would you refactor these out into a custom factory class/interface and inject them or do as I've done here?

I'm never quite sure how to handle these types of object creation.

Upvotes: 4

Views: 328

Answers (2)

Mark Seemann
Mark Seemann

Reputation: 233125

You can easily replace List<Guid> with IList<Guid> or ICollection<Guid> - or even IEnumerable<Guid> if you only need to read the list.

For other BCL types that don't already implement an interface or have virtual members, you'll need to extract an interface yourself. However, when doing that, you should watch out for Leaky Abstractions.

Upvotes: 3

jolySoft
jolySoft

Reputation: 3018

You can two routes; Firstly, as you say, create a wrapper for them and inject this. However this depends on how you want populate the state of the objects you're wrapping. This case that's not something I'd personally do. Check out Krzysztof Kozmic blog about dynamic parmaters:

Castle Windsor dynamic parameters

Hope this helps

Upvotes: 0

Related Questions