locked
Properties within a class library RRS feed

  • Question

  • Hello,

    I have an issue for which I'm sure there must be solution. I have a main application project and a class library within a solution where the intention is to use the class library for other projects. I have a class that handles my data access from xml files that requires the path to the xml files and I would like this to be set in the properties of the application. How can I get access to the propperties of the application from the class library?

    Thanks in advance,

    Neil.
    Friday, April 11, 2008 1:14 PM

Answers

  • I do agree with Morten on one aspect.  I strongly recommend you not make your data access classes static.  This will cause multiple unpredictible issues with your classes when they're called from separate UIs. If you put those classes on a server, and have multiple applications accessing the classes at once, you could get some unpredictable results.

     

    Obviously, if you're referencing the class library from the main application, you can't reference the main application from the class library, because that would create a redundant dependency. 

     

    I would suggest solving the issue by making your classes instance, and passing in the properties somehow, this will be the most maintainable way to handle this.

    Friday, April 11, 2008 2:34 PM

All replies

  • Im a bit unsure of what you wish to do.

     

    Here is a couple of ideas anyway.

     

    if you have the path in your app and can't get it into your xml object

    • You can modify your xml class contructor to take a path to your xml file.

    • You can make a public property from your xml class and set it from your app.

    if you can't get your path into your application at all.

    Friday, April 11, 2008 1:29 PM
  • Hi Morten,

    Let me clarify things a little further. Firstly, the data access class is a static class and therefore has no constructor. I wish to set the path variable via the settings in the application project. I access the settings in the application via the following method

    string path = Properties.Settings.Default.DataPath;

    But this does not work in the class library obviously. What I am asking for is to be able to access the settings of the main application from within a class library. Potentially this class library will be called from several different applications and consequently different data stores, therefore the path must come from the application. As there are many methods within this particular class then I don't want to have to pass in the path with method calls either.

    Cheers,

    Neil.
    Friday, April 11, 2008 1:37 PM
  • Im not sure that would be possible. Even if it is, it sounds like a bad solution to me.

     

    I would definitely consider not making my class static for this solution.

    Im primarily using static methods for small utilities.

    Friday, April 11, 2008 2:10 PM
  • I do agree with Morten on one aspect.  I strongly recommend you not make your data access classes static.  This will cause multiple unpredictible issues with your classes when they're called from separate UIs. If you put those classes on a server, and have multiple applications accessing the classes at once, you could get some unpredictable results.

     

    Obviously, if you're referencing the class library from the main application, you can't reference the main application from the class library, because that would create a redundant dependency. 

     

    I would suggest solving the issue by making your classes instance, and passing in the properties somehow, this will be the most maintainable way to handle this.

    Friday, April 11, 2008 2:34 PM
  • David,

    I do agree with you about the redundant dependancy, I hadn't thought of it like that. However, it would never be the case that separate UI's would be calling the same 'instance' of the class library as they would be on different platforms.

    Let me explain a bit more. We have a server type application that sits on top of a database and reads/writes data to some xml files. We also have a client application residing on a completely separated device that needs to read/write from the xml files. It is therefore never the case that independent UI's will be using the same class library 'instance' or the same xml file data store.

    I simply wanted to re-use the classes for accessing the xml files in both the client and the server where the xml files will reside in different locations. Ideally I would like a way of accessing the app.config of the calling assembly

    Neil.
    Tuesday, April 15, 2008 10:39 AM