开发者

Dependency Injection for built in types

开发者 https://www.devze.com 2023-03-31 12:39 出处:网络
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开发者_运维问答

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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消