views:

56

answers:

4

Hi!

i have always wondered how to handle Files in applications. Say we have an Object containing several data about a File like a UUID and the reference to the file on disk.

Even if this Object is immutable, there might be side effects like the file will be deleted by a part of the application but the other do not know about it, rendering the Object unusable.

Is there a handy pattern I can use for such cases? Or do I have to double-check every time I use the Object that the File reference actually points to an existing file? This might be the only solution since the file can be deleted from outside of the application, but may leave the application in hard-to-handle situation.

The problem exists the other way around: Who is responsible for deletion of the file? Since the Object might be an ordinary value object it may be referenced by other parts of the system no part of the code can delete the file for cleanup propourses to avoid side-effects...

But there might be a pattern or policy that can be used to ease the handling in such cases.

A: 

This is why many languages have exceptions.

If the real OS-level file is made invalid outside your program, your File object will not work and will raise an exception.

This is the standard approach: exceptions.

S.Lott
I know how to handle those cases, i'm looking more for a design approach for file references distributed within a system. Hard to explain when english is not your native language. :-/
Malax
A: 

1) In such case, you might want to consider having a Proxy to the handle with some state information.

2) Probably more appropriate is to use Exception handling.

jldupont
A: 

You should always double check if the file exists before doing anything with it. No matter how good your application is, there is always someone out there who can do something that you won't expect, especially when it comes to working outside of your application. A simple line of code can be the difference between a working app and a failing app.

Check if the file exists, add exceptions. Best way to go.

Aaron
+1  A: 

You seem to have two cases to deal with:

1). The resource you are using is subject to external removal, staleness and unavailability. That's pretty much the case with any external resources, for example databases, remote servers and files. So clearly you need exception handling, and you already have that covered ... however there is a little more to this, see the next case ...

2). Several parts of your applicaiton want to use the same resource and we want to mediate their usage, preventing them from interfering with each other.

For this there are two possibilities: the resoruce itself has some kind of mediation ( file locks for example ) and you exploit that. Alternatively you need to police it, and this is where (as Jean-Lou said) a proxy comes in to play. Now we probably cannot enforce the use of the proxy, if someone writes code to bypass the proxy and go direct to the File, well we have no protection - and that's just the same as as some external force damaging your resource. Assuming that we have cooperation we can put plenty of cleverness into the proxy:

a). Count the users, hence police deletion b). Count the user and automatically delete when the last one leaves. c). Pool the proxies if tey are expensive to create. d). If we get an exception, delete or mark all the now stale proxies e). Spot that two different filenames refer to the same file and police that jointly.

djna