Results 1 to 8 of 8

Thread: [2008] Common code for Web and Windows Apps?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    31

    [2008] Common code for Web and Windows Apps?

    All:

    Is it feasible (or even desirable...) to write one set of dll's that can be referenced both by windows apps and web apps.

    How do I test to set the boolean of I_am_web_app

    Code:
    If I_am_web_app=True then
    
         myapppath = HttpContext.Current.Request.ApplicationPath 
    
         'and other web code
    
    elseif I_am_windows_vb_app=True then
    
        'do something else
         myapppath = System.Windows.Forms.Application.ProductName
         'not strictly a path, but you get the idea.....
    
    End If
    I'm sure its simple, fundamental and i've plain missed it in the doc....
    Last edited by jono2000; May 27th, 2008 at 11:47 AM. Reason: forgot title...

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [2008] Common code for Web and Windows Apps?


    As far as how to determine if it's a web app or a windows app, I cannot say, but it is desireable to have a common dll or set of dll's that encapsulate your business logic. i.e. the non-GUI stuff. That way you keep things in one place and minimize the maintenance.

    If you have two pieces of code that essentially do the same thing, then it's better to condense them into one function or class so that when you have to make a change to them you don't have to change things in two (or more) places. Maintenance is faster and easier, and so is debugging.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2008] Common code for Web and Windows Apps?

    The easiest would be to use the configuration settings. In web and windows apps, you have the web.config and app.config files respectively. Set up a property in there and then use the ConfigurationManager to pull it out.

    Inside the config file you'd have:

    Code:
    <configuration>
      <AppType>
        <add key="ApplicationType" value="{web|windows}" />
      </AppType>
    </configuration>
    Then inside of your dll:

    Code:
    Dim nvAppType As NameValueCollection = CType(ConfigurationManager.GetSection("AppType"), NameValueCollection)
    
    Dim sAppType As String = nvAppType ("ApplicationType")
    
    if sAppType = "web" then
        ...
    elseif sAppType = "windows then
       ...
    end if
    It may be overly complex to use the NameValueCollection and you might be able to just pull the value straight out, but that's the only way that I know how to do it off the top of my head. Any dll that's being run in your app will use the ConfigurationManager to read either the web or app.config, depending on which type of application it is.
    (VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Common code for Web and Windows Apps?

    Your class library should not need to know whether it is dealing with a web application or a windows application.

    To answer your question, however, you can first test for

    HttpContext.Current

    first, then get its application path value out.

    Again, this part of your design should be revisited as it isn't supposed to matter where the call comes from.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    31

    Re: [2008] Common code for Web and Windows Apps?

    yes, thats my goal, to make the library client-independent.

    so, if HttpContext.Current is nothing i can safely assume I've got a windows app, no exceptions?

    thanks, thats a good way of dealing with this.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Common code for Web and Windows Apps?

    As mendhak says, there should be no need for your DLL to make a distinction. If you need the app name then it should be a property that gets set by the app. Alternatively you could read it from the config file, which will exist for both.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    31

    Re: [2008] Common code for Web and Windows Apps?

    by programmatically setting the app name i can enforce some consistency and make the code simpler for the users hence my desire to set as much as possible leaving the user-programmer just to add their relevant bits, although of course there has to be some responsibility taken by all concerned!

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Common code for Web and Windows Apps?

    If you go down the distinction route then you'll have unnecessary duplication (or rather similar) code everywhere.

    In this specific scenario, I wouldn't try to determine the application path myself, I'd get the developer to pass that in. If a file needs to be processed, give me the full path of the file. Don't let me guess what it is based upon what sort of an application you are.

    I don't know what you mean by programmatically setting the app name. What relevance does that hold in your overall design and why is it important? How does it make the code easier to use?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width