1

Topic: Context transmission through set of abstraction layers of application

All greetings. Came to collect time stones - to make architecture of application presentable. Now it is described by a well-aimed phrase "there is no time to think, it is necessary to dig". It is shown in everything, since cyclical  in assembly, thanking , and finishing Constructor Injection c 28 dependences. If for the first it is necessary to tear off simply hands, last - a consequence of unreasoned architecture. But it already is and there is a question - as though to contrive and, without rewriting ten megabytes of the code, to result an existing disgrace in more or less adequate type. In this connection a question - what to do? Is certain  applications which, being torn through set of abstraction layers, all leaves more deeply in a jungle of internal-assemblages and in passing acquires parameters-dependences, to the lower points of the falling coming to that to 28 arguments in the designer of the next class without which he cannot live. Here both Id sessions, and Id the current task, and a user id, and  and still what the hell, on the person awful,  inside. It would be desirable to throw out it in any easily accessible (both in respect of the code, and in respect of performance) a context with which it will be easy to communicate. For given by that context the DB, thanks to that it only one access is carried out through  (sometime it is mandatory shoots - _-) appears. And here for all remaining it misses, and these dependences per aspera ad astra are pulled. We take, for an example, . Here application is started and quickly starts to write a broad gull. The broad gull name goes down in arguments a command of a line. , it is difficult to refuse it (though and it is possible) but while it is tolerant. There is an authentification of the user. Now the broad gull is written already to the directory of this hapless user. The user executes some command, the broad gull is written all to the same folder, but already in a file with a name of an executed command. The command begins  and works with different computers in a network. Each task starts to write a broad gull to a file with a name mentioned above a command + computer name. In process, we start to use more low-level interfaces which too write a broad gull and not at all do not know anything neither about users, nor about commands, about hosts. But thus too want to write to a broad gull a current state and  errors, leaving on  if the grid blinked, but still there is a chance to receive the response from the remote machine. And to write that broad gulls it is necessary in the same files with a name of a command, a host, in a folder of the user. There are ThreadLocal-collections  through which transit all calls. But low-level components too not a bast  and besides  top-level, pull asynchronous methods on lower which are at all fulfilled in a casual flow from a pool and in turn too pull even more low-level components which still want to write broad gulls to the same files, as far  their component. So it is necessary to drag through all layers  and to register it in a static ThreadLocal-collection from the previous series. Here approximately and chains of arguments in designers and methods which from the version to the version become more and more are born. Sometimes you see a similar disgrace, you will contract 10 arguments into a minicontext, you will change calls. Month-other the code lives, and then lo and behold - was added two more arguments, and in your context superfluous properties which are necessary in 4 of 7 methods, but , naturally, always were added. The code becomes even more, suffers , productivity, and simple components acquire the difficult logic necessary for initialization of one of components of lower layer which simply does not know - where to it to write a broad gull. Some time I smother torment thoughts about Dependeny Injection. But in the company already there was an unsuccessful experience of implementation of this piece (about which (oh) I neither a dream, nor spirit), ended with cutting out of this with a considerable quantity . In general, did not get accustomed, yes it and is clear - lifetime and the order of initialization of objects were misunderstood from a word absolutely, and frequently objects which they did not ask were returned to people, and have been registered at all do not understand whom, when and for what purpose. In general, all is bad, and besides because of unreasoned architecture,  on a knee that though somehow worked in style write-only. In general, I will be grateful to any  and  which prompt - how to be in a similar situation. It is desirable, with real examples, close to the described situation with . What to do, where to look, what to read, what to study? What patterns approach, and what do not need to be touched in no event. If DP, with links to books and articles on the correct development and a configuration of this miracle in realities.NET. Thanks for attention.

2

Re: Context transmission through set of abstraction layers of application

Hello, LWhisper, you wrote: LW> All greetings. To begin with here that it is necessary to make: 1. To select purely utilitarian classes, which in itself with dependences all do not work also necessary explicitly declare in API, through properties / parameters. You most likely want to test this layer and  with "recall, what dependences forgot to suppose" to you hardly it is pleasant. 2. To select pieces which you want to give in the form of dependences, whenever possible to issue them in the form of interfaces (certainly, without fanaticism). 3. To implement immutable a class-context which will be responsible for transmission / substitution of dependences. Something very superficial, type ServiceContainer. IMPORTANT: Never, under no circumstances do not repeat here this error in design (the method by default returns null if service is not found). It was possible to work on the project with such decision if it is short -  permanently. Methods which in a case "" return null, it is necessary to call with prefix Try. Otherwise to catch to you null reference in the most sudden places till the end of project life. 4.  typical services in the form of extension-methods. I.e. the majority of calls should look as context. GetLogger (), instead of as context. GetService <ILogger> (). Without it to distinguish "it is used standard API" from "it is dragged an exotic hogwash" it is impossible also the code turns in  dependences. 5. For fresh  (read, for 4.6): consider possibility to transfer a context implicitly, through AsyncLocal <T>. Simplifies transmission of a context to var logger = Context. CurrentContext. Logger ();//Context - static class and with using static - to var logger = CurrentContext. Logger (); needlessly to drag a context through parameters. Here after all it is made, the majority of problems with dependences resolves and remained it will be possible to consider in detail. Was specific: LW> There is an authentification of the user. Now the broad gull is written already to the directory of this hapless user. At the moment of authentification there is a context substitution, in a new context is redefined . LW> the User executes some command, the broad gull is written all to the same folder, but already in a file with a name of an executed command. Again context substitution. : support a case does not kill you for necessity to collect a broad gull from ten files? LW> the command begins  and works with different computers in a network. Each task starts to write a broad gull to a file with a name mentioned above a command + computer name.  Though in an amicable way for was specific this task needs be not to used one , and to add separate for each class of tasks. LW> some time I smother torment thoughts about Dependeny Injection. But in the company already there was an unsuccessful experience of implementation of this piece (about which (oh) I neither a dream, nor spirit), ended with cutting out of this with a considerable quantity . DI is already  over a dragged context. If with it the brothel DI only disguises a problem, instead of helps to cure it.

3

Re: Context transmission through set of abstraction layers of application

S> 3. To implement immutable a class-context which will be responsible for transmission / substitution of dependences. Something very superficial, type ServiceContainer. Aha, thanks, I will look. S> 5. For fresh  (read, for 4.6): consider possibility to transfer a context implicitly, through AsyncLocal <T>. Here the most interesting also begins.NET 4.6 and VS2015 is a serious step, to which else  and . I already described problem ThreadLocal. But because in.NET there is no hierarchy of flows as I understand, AsyncLocal precisely also it is necessary to support manually in all types which launch flows. But it would be healthy to esteem about it more in detail. The price of a question, application field etc. S> At the moment of authentification happens context substitution, in a new context is redefined . In the presence of implicit transmission of a context looks simply enough and conveniently. At explicit, unfortunately, it is necessary to drag it in the most remote places, in each method, in each class which when or wanted, wants or something can want . S> Offtop: support a case does not kill you for necessity to collect a broad gull from ten files? No, it to itself(himself) is quite happy, as in one file it would be rather unpleasant to read gigabytes of dens from parallel flows. S> though in an amicable way for was specific this task needs be not to used one , and to add separate for each class of tasks. It is possible more in detail? S> DI is already  over a dragged context. If with it the brothel DI only disguises a problem, instead of helps to cure it. Understood, thanks.

4

Re: Context transmission through set of abstraction layers of application

Hello, LWhisper, you wrote: LW> Here the most interesting also begins. LW>.NET 4.6 and VS2015 is a serious step, to which else  and . In vain. Targeting under 4.6 can and in 2012 be cut. On all that ancient to sit - a rare masochism. LW> I already described problem ThreadLocal. ThreadLocal generally obsolete it is necessary to declare. The best method to snip off a rake in a combination with  I yet did not see. Well , it is not pleasant AsyncLocal - it is possible with LogicalCallContext . In 4.0 it with   was not on friendly terms, since 4.5 all apprx. the Example. LW> but because in.NET there is no hierarchy of flows as I understand, AsyncLocal precisely also it is necessary to support manually in all types which launch flows. Generally call context from the first  was. Links in the previous paragraph.  nobody knows,  LW> the Price of a question, application field etc. In sense the question price? Here it? static readonly AsyncLocal <string> _current = new AsyncLocal <string> (); static void Main (string [] args) {_current. Value = "Calling DoAsync"; Console. WriteLine ("In Main \tcurrent:" + _current. Value); DoAsync ().Wait (); Console. WriteLine ("In Main \tcurrent:" + _current. Value); Console. Write ("Done..."); Console. ReadKey ();} static async Task DoAsync () {await Task. Delay (500); Console. WriteLine ("In DoAsync\tcurrent:" + _current. Value); _current. Value = "Calling DoAsync2"; Console. WriteLine ("In DoAsync\tcurrent:" + _current. Value); await DoAsync2 (); Console. WriteLine ("In DoAsync\tcurrent:" + _current. Value);} static async Task DoAsync2 () {await Task. Delay (500); Console. WriteLine ("In DoAsync2\tcurrent:" + _current. Value); _current. Value = "Calling DoInThread"; Console. WriteLine ("In DoAsync2\tcurrent:" + _current. Value); var t = new Thread (DoInThread); t. Start (); t. Join (); Console. WriteLine ("In DoAsync2\tcurrent:" + _current. Value);} static void DoInThread () {Thread. Sleep (500); Console. WriteLine ("In DoInThread\tcurrent:" + _current. Value); _current. Value = "From DoInThread"; Console. WriteLine ("In DoInThread\tcurrent:" + _current. Value);} It works as the automatic machine. We open we msdn/google examples, we write  and it is used with a clear conscience. Something breaks - learn on disappeared .  for such things are mandatory. LW> in the presence of implicit transmission of a context looks simply enough and conveniently. At explicit, unfortunately, it is necessary to drag it in the most remote places, in each method, in each class which when or wanted, wants or something can want . Well. And what else variants? The same principle in all other approaches will be, the wrapper differs only. S>> though in an amicable way for was specific this task needs be not to used one , and to add separate for each class of tasks. LW> it is possible more in detail? Something of type PresentationTraceSources. I.e. separate  for each of categories of tasks. Otherwise incorrectly changed context - and all output joyfully  not in that file.

5

Re: Context transmission through set of abstraction layers of application

Hello, LWhisper, you wrote: LW> Here approximately and chains of arguments in designers and methods which from the version to the version become more and more are born. Sometimes you see a similar disgrace, you will contract 10 arguments into a minicontext, you will change calls. Month-other the code lives, and then lo and behold - was added two more arguments, and in your context superfluous properties which are necessary in 4 of 7 methods, but , naturally, always were added. The code becomes even more, suffers , productivity, and simple components acquire the difficult logic necessary for initialization of one of components of lower layer which simply does not know - where to it to write a broad gull. Combination DI + pattern Wrapper for this purpose is ideally suited. LW> Some time I smother torment thoughts about Dependeny Injection. But in the company already there was an unsuccessful experience of implementation of this piece (about which (oh) I neither a dream, nor spirit), ended with cutting out of this with a considerable quantity . In general, did not get accustomed, yes it and is clear - lifetime and the order of initialization of objects were misunderstood from a word absolutely, and frequently objects which they did not ask were returned to people, and have been registered at all do not understand whom, when and for what purpose. In general, all is bad, and besides because of unreasoned architecture,  on a knee that though somehow worked in style write-only. It is a question of specific DI-containers?

6

Re: Context transmission through set of abstraction layers of application

Hello, 0x7be, you wrote: 0> combination DI + pattern Wrapper For this purpose is ideally suited. DI in itself does not solve a problem of transmission of dependences on a call chain. It is necessary to drag the container that transforms DI into the perverted variety service locator. Wrapper too does not help, since in similar tasks the list of dependences is in advance unknown.

7

Re: Context transmission through set of abstraction layers of application

Hello, Sinix, you wrote: S> DI in itself does not solve a problem of transmission of dependences on a call chain. It is necessary to drag the container that transforms DI into the perverted variety service locator. S> Wrapper too does not help, since in similar tasks the list of dependences is in advance unknown. What for the container??? We declare the interface: interface ILogger {void log (string message);} It is injected it in the component-user: class MyComponent {public MyComponent (..., Ilogger logger...) {...} } We have "sheet" implementation ILogger which carries out physical record where it is necessary. Necessarily we add wrappers which add the necessary attributes in the writeable message: the component name, the flow identifier, date/time, etc. As a result has no components in which it is injected  concepts about all these subtleties, to it gave a pipe - it there blows, and where it blows - him does not excite. Presence of the DI-container for this circuit strictly . Eventually it is possible to construct the necessary structure of objects "hands".

8

Re: Context transmission through set of abstraction layers of application

Hello, 0x7be, you wrote: 0> It is had "sheet" implementation ILogger which carries out physical record where it is necessary. And, in this sense? Then yes, it turns out about the same that wrote above, only instead of a context the di-container appears. At  just one of problems - as this container on all call chain to drag. There are any ideas?

9

Re: Context transmission through set of abstraction layers of application

Hello, Sinix, you wrote: S> In vain. Targeting under 4.6 can and in 2012 be cut. On all that ancient to sit - a rare masochism. , found. S> ThreadLocal generally obsolete it is necessary to declare. The best method to snip off a rake in a combination with  I yet did not see. Aha. S> Well , it is not pleasant AsyncLocal - it is possible with LogicalCallContext . In 4.0 it with   was not on friendly terms, since 4.5 all apprx. S> the Example. S> Generally call context from the first  was. Links in the previous paragraph.  nobody knows,  Thanks, I will familiarize.:D)) S> Something of type PresentationTraceSources. I.e. separate  for each of categories of tasks. Otherwise incorrectly changed context - and all output joyfully  not in that file. And once again thanks!

10

Re: Context transmission through set of abstraction layers of application

Hello, 0x7be, you wrote: 0> It is a question of specific DI-containers? Mocknity extension for MS Unity was used.

11

Re: Context transmission through set of abstraction layers of application

Hello, Nahlobuch, you wrote: And that if (lag behind, mice, I am a strategist): And then any interested person through Context. GetLogger () gets ILogger, already stuffed, h'm, "set dressers" and, so, writing there where it is necessary. Here there, above, Sinix offered variants with AsyncLocal and LogicalCallContext. If it will be possible by means of one of them transparently to drag a context to any component after that any of variants approaches.

12

Re: Context transmission through set of abstraction layers of application

Hello, Sinix, you wrote: S> Then yes, it turns out about the same that wrote above, only instead of a context the di-container appears. And the component generally should not to know anything about the container. Services are necessary to it - to it them give through parameters of the designer. As - him does not excite. S> at  just one of problems - as this container on all call chain to drag. There are any ideas? At  a problem in dragging all those parameters which I suggest to pack in different .

13

Re: Context transmission through set of abstraction layers of application

Hello, LWhisper, you wrote: 0>> It is a question of specific DI-containers? LW> Mocknity extension for MS Unity Was used. And what for? What hinders hands to create and inject the necessary objects each other without special container magic with autowiring ` an ohm and guessings about why the container produced not that copy?

14

Re: Context transmission through set of abstraction layers of application

Hello, 0x7be, you wrote: S>> Then yes, it turns out about the same that wrote above, only instead of a context the di-container appears. 0> and the component generally should not to know anything about the container. Services are necessary to it - to it them give through parameters of the designer. As - him does not excite. Well so in that and a problem that "not to know about the container" it is impossible, at  it only lowermost of layers. And above - the component causes other components in which it is necessary to drag dependences too. And further on a chain. And dependences should be resolved dynamic, in advance they are unknown, since implementation normally for the unpretentious interface hides. S>> at  just one of problems - as this container on all call chain to drag. There are any ideas? 0> at  a problem in dragging all those parameters which I suggest to pack in different . . You are similar simple with such systems did not face. In them a typical problem - a call chain in 5-7 layers, in each of which the dependences about which the causing code should not to know anything can be demanded. And total number of dependences - not units, and is closer to pair tens. For they dobavljajutsja/are cleaned literally on the move, under a current dial-up of requirements. Something of type strongly non-linear  turns out.

15

Re: Context transmission through set of abstraction layers of application

Hello, Sinix, you wrote: S> Well so in that and a problem that "not to know about the container" it is impossible, at  it only lowermost of layers. And above - the component causes other components in which it is necessary to drag dependences too. And further on a chain. And dependences should be resolved dynamic, in advance they are unknown, since implementation normally for the unpretentious interface hides. And why in these other components of dependence should drag causing code, instead of what constructs it? S> Neeet. You are similar simple with such systems did not face. In them a typical problem - a call chain in 5-7 layers, in each of which the dependences about which the causing code should not to know anything can be demanded. And total number of dependences - not units, and is closer to pair tens. For they dobavljajutsja/are cleaned literally on the move, under a current dial-up of requirements. The causing code also should not know. Should know the code constructing. Anyway, it while the theory, I do not know that there is exact at the author.

16

Re: Context transmission through set of abstraction layers of application

Hello, 0x7be, you wrote: 0> And why in these other components of dependence should drag causing code, instead of what constructs it? Because then the requirement "we drag dependences" it is replaced on "we drag the code which constructs objects", i.e. as a matter of fact changes nothing. Here the typical circuit: static SomeResult DumpA (..., Context context) {var data = context. Get <IAReportService> ().PrepareData (...); var dumper = context. Get <IDumpService> (); return dumper. Dump (data);} And inside IAReportService access to , to the Internet is necessary to us, let us assume, , (we tighten updates of the data if yet) and to service with API from the integrated external system. Inside IDumpService () - IO (the temporary file),  is used and service-HELPER for redirect of a data stream client even after method end - not to hold superfluous resources and not to cause a method anew at a communication breakaway. Nuances in the following: 1. Dependences are in advance unknown. As a last resort happens here so: tomorrow the client asks - we throw out a part of calculations in a pool of tasks, the Internet we change for reading of the transferred file, a broad gull we will show creeping line on the client and IDumper generally is replaceable on implementation of the client. If needed specific implementation will be  on the fly, without the reassembly of all economy. Thus clients from other organization on the same service receive a context with old dependences. 2. Both services can cause other business services and so yet does not bother. Both requirements as a result do not leave a choice - in each of levels the current context explicitly/implicitly should be dragged. It will become explicitly, transmission through parameters, or the container will push itself in created object at the moment of call Get <> () is already nuances of implementation, purely essentially approach from it does not exchange.

17

Re: Context transmission through set of abstraction layers of application

Hello, Sinix, you wrote: S> Because then the requirement "we drag dependences" it is replaced on "we drag the code which constructs objects", i.e. as a matter of fact changes nothing. Here the typical circuit: S> S> static SomeResult DumpA (..., Context context) S> {S> var data = context. Get <IAReportService> ().PrepareData (...) ; S> var dumper = context. Get <IDumpService> (); S> return dumper. Dump (data); S>} S> And precisely it is necessary to drag these dependences through a call chain (i.e. in parameters of methods)? I somehow manage before  dependences through the designer. S> nuances in the following: S> 1. Dependences are in advance unknown. As a last resort happens here so: tomorrow the client asks - we throw out a part of calculations in a pool of tasks, the Internet we change for reading of the transferred file, a broad gull we will show creeping line on the client and IDumper generally we replace with implementation of the client. If needed specific implementation will be  on the fly, without the reassembly of all economy. Thus clients from other organization on the same service receive a context with old dependences. Here tiny nuance. I as a whole against such approach when in a method body there is an extraction of links from some Service Provider ` and. Such approach does exterior dependences of a class "mislaid" in the procedural code and practically eliminates compile-time control of that the object graph is collected as it is necessary.

18

Re: Context transmission through set of abstraction layers of application

S>> Well , it is not pleasant AsyncLocal - it is possible with LogicalCallContext . In 4.0 it with   was not on friendly terms, since 4.5 all apprx. S>> the Example. S>> Generally call context from the first  was. Links in the previous paragraph.  nobody knows,  LW> Thanks, I will familiarize.:D)) CallContext - a smart piece! Works! Many thanks! And is any best practices on development of business logic with usage of contexts? How to inform to readers-writers of the code, what here this piece depends on the same ILogger which is necessary for initializing and if it not to make you will fail out of the blue with NotInitializedException? And can and you will not fail, and fails at the customer who went under the most rare scenario which has been not covered  and avoided steadfast look QA?

19

Re: Context transmission through set of abstraction layers of application

Hello, Sinix, you wrote: S> At  just one of problems - as this container on all call chain to drag. There are any ideas? ExecutionContext Class? Well about the similar it was told above, yes (LogicalCallContext).

20

Re: Context transmission through set of abstraction layers of application

Hello, Sinix, you wrote: S> Neeet. You are similar simple with such systems did not face. In them a typical problem - a call chain in 5-7 layers, in each of which the dependences about which the causing code should not to know anything can be demanded. And total number of dependences - not units, and is closer to pair tens. For they dobavljajutsja/are cleaned literally on the move, under a current dial-up of requirements. That case, when a cart ahead of a horse and abusing DP and IoC. If the causing code does not know, means causing the causing should know. From such architecture and statements not on itself becomes. S> Something of type strongly non-linear  turns out.

21

Re: Context transmission through set of abstraction layers of application

Hello, Sinix, you wrote: S> As a last resort happens here so: tomorrow the client asks - we throw out a part of calculations in a pool of tasks, the Internet we change for reading of the transferred file, a broad gull we will show creeping line on the client and IDumper generally is replaceable on implementation of the client. Here, in my opinion, for for the sake of peace of mind of developers it is better to make fork.

22

Re: Context transmission through set of abstraction layers of application

Hello, 0x7be, you wrote: 0> I somehow manage before  dependences through the designer. Well so it works, only when dependences are static also lifetime of service very little. When dependences change on circumstances as at , it is necessary or to produce    (that kills all idea with a superficial context on a root), or all the same to drag a context on a call chain. 0> here tiny nuance. I as a whole against such approach when in a method body there is an extraction of links from some Service Provider ` and. I do not know, whether  to write that At  it is required to palm off dependences __. Roughly speaking using (var c = BeginTransaction ()) {service. DoSomething (obj1); c. Commit ();} using (var c2 = BeginTransaction ()) {service. DoSomething (obj2); c2.Commit ();}  service one, a context (in this example - transactions) - different. The heap  is tolerant, when at you ten requests a minute. When it is more - hosh-not-hosh on GC already it is necessary to look.

23

Re: Context transmission through set of abstraction layers of application

Hello, Sharov, you wrote: S>> Neeet. You are similar simple with such systems did not face. In them a typical problem - a call chain in 5-7 layers, in each of which the dependences about which the causing code should not to know anything can be demanded. And total number of dependences - not units, and is closer to pair tens. For they dobavljajutsja/are cleaned literally on the move, under a current dial-up of requirements. S> that case, when a cart ahead of a horse and abusing DP and IoC. Well if it needlessly so is made - unconditionally yes. And when almost the key biz-requirement is reduced to "to the logician of handling to collect on the move, almost under each request individually", all other variants start to demand a heap of time for finishing by a file. Certainly, there there are nuances, both on testing, and on designing API / to observance of contracts, but it already absolutely . S> Here, in my opinion, for for the sake of peace of mind of developers it is better to make fork. I speak - did not face:P Fork to support  costs. Even not so much , how many time - developers, QA, implementers, support, fuss with an individual hosting etc., etc. And with the advent of various combinations of finishings this time does not become in general less, all is easy and simple, but is equal until exceptions are individual. When there is no - is not present

24

Re: Context transmission through set of abstraction layers of application

Hello, Sinix, you wrote: S> Fork to support  costs. Even not so much , how many time - developers, QA, implementers, support, fuss with an individual hosting etc., etc. And with the advent of various combinations of finishings this time less not Here it begins necessary to dance from the customer - if general and monetary, volens-nolens. Otherwise will support more expensively code base, which all and for all. For the separate single customer it is possible and .

25

Re: Context transmission through set of abstraction layers of application

Hello, LWhisper, you wrote: LW> And is any best practices on development of business logic with usage of contexts? Certainly.-1. To consider idea 0x7be - all objects short-lived, turn out a method call of the context, at the moment of a call the context fills the link to in object. The most reliable method to transfer a context if there are no problems with excessive . 0. Whenever possible to replace CallContext on AsyncLocal. The magic since all other variants work Further begins are very original, demand a minimum.net 4.5 __ this behavior is not documented. 1. ATTENTIVELY to read here this post. Especially the text selected with the fat. 2. To consider that CallContext a current flow it is substituted for an asynchronous context at the moment of a call delegate. EndInvoke (). The Same - for  for some channels. 3. For WCF services c as a hosting under IIS on hearings CallContext works not always. Details I do not remember, Google finds any hogwash. 4. On the same hearings, in any of versions, callContext I was lost after.ConfigureAwait (false) I suspect that it was in any of  as our tests for it did not fall. 5. Tests-tests-tests. If you decided to use not documentary variant cover with tests the main combinations of calls - through a new flow / thread pool /  / await, await atop IO (everyones ReadXxxAsync), combinations.BeginInvoke ()/.EndInvoke (), SynchronizationContext etc. Interpose debug  which will check a correctness of filling of a context. LW> how to inform to readers-writers of the code, what here this piece depends on the same ILogger which is necessary for initializing and if it not to make you will fail out of the blue with NotInitializedException? And can and you will not fail, and fails at the customer who went under the most rare scenario which has been not covered  and avoided steadfast look QA? Well and it already the general for all DI-frejmvorkov a problem. The most simple method - is used only documentary dependences which are issued in the form of extension-methods, they should be accessible always. For exotic or we write tests + debug , or let to myself falls is it will be possible better, than start in  the unchecked code.