0

I'd like to know if it is possible to do the following:

        using (MyClass o = new MyClass())
        {
            TheClassIWantMyClassToSee x = new TheClassIWantMyClassToSee();
            x.DoStuff();
        }

I'd like to create a class (MyClass) and use it in a using block. Inside this block, I want to work with objects of a certain type (TheClassIWantMyClassToSee). When using block falls out of scope, I want to perform certain actions on these (TheClassIWantMyClassToSee) objects.

Is it possible to make a class aware of other objects declared in its scope transparently?

I realise that I could add object instances to the MyClass object, but I'd like to make it easier for developers working with the API I'm building.

Any ideas are welcome.

Thanks.

3
  • I think you are going to have to provide some more context, your question as it stands makes little sense. Can you tell us a little about the API you are building? Commented Mar 7, 2012 at 6:28
  • You were quite clear, but you are asking something quite peculiar. It would help to know why you are asking. Commented Mar 7, 2012 at 6:33
  • :) I realise that it is quite an odd question. I was mostly just checking to see if something like this was possible. I'm building a custom "data context" object which persists data to a back-end; so I just wanted to see if it would be possible to have a way where I don't need to explicitly add objects to the context, but merely use them in a using block and the scope picks up that objects were created and changed. Commented Mar 7, 2012 at 6:39

4 Answers 4

1

The only way to make MyClass aware of TheClassIWantMyClassToSee is by creating a reference from one to the other. There is no way to navigate and explore the classes that are in scope. This statement is true regardless of whether the scope relates to a using block, method block, foreach loop or other.

Why not have a simple MyClass.AddRelationship(TheClassIWantMyClassToSee child) method that makes this class aware of the other?

Sign up to request clarification or add additional context in comments.

Comments

0

What are these "certain actions"? If they're resource cleanup, then TheClassIWantMyClassToSee should implement IDisposable too, and you can use another using statement. Otherwise, you could just use a try/finally block:

using (MyClass o = new MyClass())
{
    TheClassIWantMyClassToSee x = new TheClassIWantMyClassToSee();
    try
    {
        x.DoStuff();
    }
    finally
    {
        // Do stuff here
    }
}

There's no way of adding code to the finally block which is implicitly at the end of the using statement.

5 Comments

Thanks for clearing that up, also didn't think it was possible. The "certain actions" would be saving data to a data store. I thought of using the IDisposable interface for this, but it doesn't solve the problem of implicitly having access to the variables declared within the scope. It would be a nice language feature to allow code to access the scope it is being called from in a using block. Albeit a little odd.
If you look at similar APIs such as nHibernate where a model object is stored to a database, they all require that you manually associate the model object with a session, as per my answer. It would be a nice feature, but a bit confusing!
Yup. I can see people doing horrible things with a language feature like that :) Would be very nice if Microsoft provided an interface similar to IDisposable which the using statement could pick up on and fire events when objects are created or changed. An IScopeInterrogator or something.
@JohanndeSwardt: I don't think it would pass the cost/benefit test, to be honest. After all, we've already got try/finally. If the actions on your "nested" objects is really linked to the lifetime (pre-disposal) of your "top-level" object then it sounds like it does make sense for the one to know about the others...
I agree with the cost/benefit thing. The only really good application I can see for this is transparent logging, but there are other frameworks available that do this already.
0

The more strongly typed your design, the easier it will be for others to use your API. (intellisense) You should add it to other object, most likely.

Comments

0

You can try to use MyClass as a factory for all the "insider" classes, so You always get a chance to associate the new instances with the MyClass instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.