Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Overriding somes classes available in System namespace

I've a project that need to be compiled in Compact .NET Framework 3.5 and .NET Framework 3.5 (2 projects in fact, just compiling options that changes between both).

Problem is, some classes are missing in the CF .NET so I created it manually (and implemented all members of the class available in .NET

One example : The FtpWebRequest / FtpWebResponse classes.

It's bad to write something like this (if yes, why?) :

#if CFNET35 // Only if we are in Compact Framework 3.5 mode
namespace System.Net
{
    public class FtpWebRequest : WebRequest
    {
        // ...
    }

    public class FtpWebResponse : WebResponse
    {
        // ...
    }
}
#endif

I'm sure that in CF.NET35 these methods will never be available, so can I write it?

I would write that in order to avoid a name collision when using the my library in projects.

It allows me in other projects always using System.Net; whitout asking me which framework I use...

Thanks !


EDIT

Few months later, I'd to assess the strategy I used.

As said, I override System(.Net) namespace by doing conditional compilation, so I've two DLL's (one for CF.NET, one for .NET)

That includes too that all my applications using this DLL are in double (each a time a CF.NET app and one .NET app that includes the corresponding library).

So, was a bad idea, I've a lot of project in double and that's unnecessary in the way that a .NET app can directly include and use a CF.NET library.

Moreover, something I haven't take care in consideration is if that a .NET app include a CF.NET library with an overriden System namespace, initialization of it will fail because of a class name collision...

So, EPIC FAIL, providing a generic interface is the best way to manage this case.

like image 268
Arnaud F. Avatar asked Dec 12 '22 15:12

Arnaud F.


1 Answers

I would probably avoid using the System namespaces for writing custom code. I've seen open source libraries that try to do that, and it usually results in a headache.

You might be better off creating an interface that is shared between the full and compact framework, then implement the interface in the full and CF that provide the functionality you need using built-in System classes, or classes you write yourself.

This might seem like overkill, but you'll be safer in the future if something in System.Net changes. Your calling code should just reference the interface, and you can plug in either implementation depending on what platform you're on.

// Shared interface
public interface IFtpUtil
{
    SomeFileObject GetFile(SomeArgument a);
    void PutFile(SomeFileObject f, SomeArgument a);
}

// Full framework implementation
public class FullFtpUtil : IFtpUtil
{
    public ... GetFile(...)
    {
        // Use System.Net classes from full framework
    }

    public ... PutFile(...)
    {
        // Use System.Net classes from full framework
    }
}

// Compact framework implementation
public class CompactFtpUtil : IFtpUtil
{
    public ... GetFile(...)
    {
        // Use your own FTP classes
    }

    public ... PutFile(...)
    {
        // Use your own FTP classes
    }
}
like image 82
Andy White Avatar answered Dec 28 '22 07:12

Andy White