c# - Alternatives to passing around a context object -


i have single context object want able access large number of difference classes. have code looks like

context ctx = new context(); section section = new section(ctx) {     data1 = new somedata(ctx) { value = 123 },     data2 = new someotherdata(ctx) { foo = "bar" },     subsection = new section(ctx) {         moredata = new moredata(ctx) { text = "hello!" }     } }; 

but i'd code looks like:

using(context.new()) {     section section = new section() {         data1 = new somedata { value = 123 },         data2 = new someotherdata { foo = "bar" },         subsection = new section {             moredata = new moredata { text = "hello!" }         }     };     // section } 

is possible? i'll using in asp.net .exes (and else in future) can't store static or thread local reference somewhere.

it doesn't need above, way won't have pass context every object create. thought using extension methods context.createsomedata() requires more boilerplate per class , isn't better still need context object.

ideally should work under vs2008/.net3.5, although i'd still interested if there's way @ all.

update: ended solving refactoring approach following:

section section = new section {     data1 = new somedata { value = 123 },     data2 = new someotherdata { foo = "bar" },     subsection = new section {         moredata = new moredata { text = "hello!" }     } }; section.dostuffwithcontext(new context()); 

while may not work everyone, need here.

i'll leave question open in case comes solution initial problem.

you can define static method context.retreivedata(), don't have implement boilerplate code inside of method itself.

using command pattern every specific project type can provide own implementation retreivedata() method. asp.net project can supply method retreive data session. winform executable can supply method retreive data global variable. yet project can supply method retrieving data db.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -