Conditional Import - for Assemblies not present

99 views
Skip to first unread message

JaffaB

unread,
Jan 17, 2008, 7:16:41 AM1/17/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi all,

This may sound an odd question, but is there anyway to have a
conditional imports statement. Take an example where at the top of
my ASP form, I have...

Imports MSProject = Microsoft.Office.Interop.MSProject

If the app is being installed on lots of machines, if project has not
been installed on a few machines, it will fall apart as soon as it
tries to include Project.

My app can check if the registry is on the machine and avoid the
project logic, but can I put a conditional inclusion of the Imports
section?

Thanks

Andrew Badera

unread,
Jan 18, 2008, 3:55:27 AM1/18/08
to DotNetDe...@googlegroups.com
I would be very interested to see how one would write their code to "conditionally" use an assembly, or not, as described. This doesn't feel like good engineering to me.

--Andrew Badera


Michael O

unread,
Jan 18, 2008, 11:32:06 AM1/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I needed to this for a solution once. I had an abstract ADO.NET-
related library that could be conditionally compiled to include
assemblies for Oracle Corp's provider, MySQL's provider or one for
PostgreSQL respectively. There was a "vanilla" that didn't reference
any of them. It was necessary to have this multiple build situation
because it was inefficient to have require non-.NET Framework
assemblies when the consumer of the library had no intention of using
them.

This is how I did it: First I retained the vanilla Debug and Release
builds. The former required all assemblies. The latter required only
System.Data and System.Data.OracleClient - which come included in the
Framework. Additionally I created Release MySQL, Release ODAC and
Release PostgreSQL builds each with a helpful conditional compile
symbols of MYSQL, ODAC and POSTGRESQL respectively. Conditional
compile symbols are set from the Properties of the project.

There was only a remarkably small amount of #if wrappers in my code on
those helpful conditional compile symbols for code depending on non-
Framework assemblies.

However, this is not enough. To this point, all I had done was
conditionally *compile* certain parts of my code. I was still
unconditionally referencing my non-Framework assemblies. In order to
resolve this, I had to roll my sleeves up and tweek the .csproj xml
file directly, which I include below. I cobbled together the know-how
on doing this from a variety of links that I no longer have. But you
can probably do a search on MSDN and Google for more information based
on my post. This is from my Visual Studio 2005 Pro edition C#
project.

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/
developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</
Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{whatever}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>com.aastra.us.OmniData</RootNamespace>
<AssemblyName>MyProject.CoreLibrary</AssemblyName>
<ApplicationIcon>favicon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|
AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;ODAC MYSQL POSTGRESQL</
DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
'Release ODAC|AnyCPU' ">
<OutputPath>bin\Release ODAC\</OutputPath>
<DefineConstants>TRACE;ODAC</DefineConstants>
<Optimize>true</Optimize>
<DebugType>none</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
'Release MYSQL|AnyCPU' ">
<OutputPath>bin\Release MYSQL\</OutputPath>
<DefineConstants>TRACE;MYSQL</DefineConstants>
<Optimize>true</Optimize>
<DebugType>none</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
'Release POSTGRESQL|AnyCPU' ">
<OutputPath>bin\Release POSTGRESQL\</OutputPath>
<DefineConstants>TRACE;POSTGRESQL</DefineConstants>
<Optimize>true</Optimize>
<DebugType>none</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="Oracle.DataAccess, Version=2.102.2.20,
Culture=neutral, PublicKeyToken=89b483f429c47342,
processorArchitecture=x86" Condition="'$(Configuration)'=='DEBUG'OR'$
(Configuration)'=='Release ODAC'">
<HintPath>..\lib\Oracle.DataAccess.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MySql.Data, Version=5.0.2.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL"
Condition="'$(Configuration)'=='DEBUG'OR'$(Configuration)'=='Release
MYSQL'">
<HintPath>..\lib\MySql.Data.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Npgsql, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=5d8b90d52f46fda7, processorArchitecture=MSIL"
Condition="'$(Configuration)'=='DEBUG'OR'$(Configuration)'=='Release
POSTGRESQL'">
<HintPath>..\lib\Npgsql.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mono.Security, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=0738eb9f132ed756" Condition="'$
(Configuration)'=='DEBUG'OR'$(Configuration)'=='Release POSTGRESQL'">
<HintPath>..\lib\Mono.Security.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Data.OracleClient" />
<Reference Include="System.Transactions" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
</ItemGroup>

...(uninteresting parts intentionally removed)

</Project>

I don't recommend editing your .csproj file directly, but you don't
have that much to lose.

Michael O
http://blog.crisatunity.com

Andrew Badera

unread,
Jan 18, 2008, 11:35:53 AM1/18/08
to DotNetDe...@googlegroups.com
This seems to beg for a Provider pattern ...


    <Reference Include="Npgsql, Version= 1.0.0.0, Culture=neutral,

Michael O

unread,
Jan 18, 2008, 11:45:50 AM1/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
It completely adheres to the provider pattern - that is why there is
only a handful of #if lines.

On Jan 18, 10:35 am, "Andrew Badera" <and...@badera.us> wrote:
> This seems to beg for a Provider pattern ...
>
> > <Reference Include="Npgsql, Version=1.0.0.0, Culture=neutral,

Michael O

unread,
Jan 18, 2008, 11:48:36 AM1/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You can't provider pattern your way to create certain ADO.NET
workings, like delineating provider-specific parameter data type
enumerations. Trust me, I'm completely steeped in the ADO.NET
provider pattern.

On Jan 18, 10:35 am, "Andrew Badera" <and...@badera.us> wrote:
> This seems to beg for a Provider pattern ...
>
> > <Reference Include="Npgsql, Version=1.0.0.0, Culture=neutral,

JaffaB

unread,
Jan 18, 2008, 4:05:13 AM1/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting


On Jan 18, 8:55 am, "Andrew Badera" <and...@badera.us> wrote:
> I would be very interested to see how one would write their code to
> "conditionally" use an assembly, or not, as described. This doesn't feel
> like good engineering to me.
>
> --Andrew Badera
>
> On 1/17/08, JaffaB <jaffa_br...@yahoo.co.uk> wrote:
>
> > Hi all,
>
> > This may sound an odd question, but is there anyway to have a
> > conditional imports statement.   Take an example where at the top of
> > my ASP form, I have...
>
> > Imports MSProject = Microsoft.Office.Interop.MSProject
>
> > If the app is being installed on lots of machines, if project has not
> > been installed on a few machines, it will fall apart as soon as it
> > tries to include Project.
>
> > My app can check if the registry is on the machine and avoid the
> > project logic, but can I put a conditional inclusion of the Imports
> > section?
>
> > Thanks
>
> --
> --Andy Baderahttp://andrew.badera.us/
> and...@badera.us
> (518) 641-1280- Hide quoted text -
>
> - Show quoted text -

Ok, so how would you do it. I have logic on a screen for importing
data from project or CSV, but clearly...

1) Cant guarentee that Project will be installed on the client
2) Cant deploy the project DLL etc files as part of my application
3) Dont want to have two screens - one with project and one without as
we are doubling up with source
4) The 3rd party components (Aspose) are fairly pricey - its the
client licences that concern me - unless its the OEM option in which
case we are talking $1500

What would you do?

Andrew Badera

unread,
Jan 19, 2008, 5:53:09 AM1/19/08
to DotNetDe...@googlegroups.com
Provider model. Ship your main application with a default or dummy provider class (or set of classes), but can configure via config file to utilize a specific provider available locally on the target machine.

--ab



On 1/18/08, JaffaB <jaffa...@yahoo.co.uk> wrote:

Peter Groenewegen

unread,
Jan 20, 2008, 3:33:37 AM1/20/08
to DotNetDe...@googlegroups.com
You can put the logic (dll) behind a webservice, and let your clients call the service. You only have one machine with the dll.
 
--
Peter

On Jan 18, 2008 10:05 AM, JaffaB <jaffa...@yahoo.co.uk> wrote:

Charles A. Lopez

unread,
Jan 20, 2008, 9:27:09 AM1/20/08
to DotNetDe...@googlegroups.com
How big is your project in terms of expected number of lines of code?
 
Peter's solution seems feasible but has some caveats.
 
I'd assume worst case scenario and base my request for funding on that.
--
Charles A. Lopez
charle...@gmail.com

Start your career with Charles A. Lopez TECHNOLOGY - Send your resume to charle...@gmail.com

http://cs.nyu.edu/web/People/alumni.html

1-800-GOOG411 - Google's free 411 service
Reply all
Reply to author
Forward
0 new messages