[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....
Re: [2008] Common code for Web and Windows Apps?
:wave:
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.
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.
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.
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.
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.
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!
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?