0

I've built a reusable Class Library to encapsulate my Authentication logic. I want to be able to reuse the compiled *.dll across multiple projects.

What I've got works. But, something about how I'm making the reference, or how my Class Library is structured isn't quite right. And I need your help to figure out what I'm doing-wrong/not-understanding...

I've got a Class Library (Authentication.dll) which is structured like this:

namespace AUTHENTICATION
{
    public static class authentication
    {
        public static Boolean Authenticate(long UserID, long AppID) {...}

        //...More Static Methods...//    
    }
}

In my dependent project I've added a reference to Authentication.dll, and I've added a using directive...

using AUTHENTICATION;

With this structure I can call my Authenticate method, from my dependent project, like so...

authentication.Authenticate(1,1)

I'd like to be able to not have to include that "authentication." before all calls to methods from this Class Library. Is that possible? If so, what changes do I need to make to my Class Library, or how I'm implementing it in my dependent project?

2
  • Is there any reason you want this?
    – Arran
    Aug 30, 2012 at 14:01
  • Arran... I'm a minimalist when it comes to formatting my code. I'm just trying to consolidate as much as possible. That is, if it's possible to consolidate further.
    – s15199d
    Aug 30, 2012 at 14:16

7 Answers 7

3

In C# a function cannot exist without a class. So you always need to define something for it, being a class for a static method or an object for an object method.

3

The only option to achieve that would be to declare a base class in the Authentication assembly from which you inherit in the dependent projects.

You could expose Authenticate as a protected method (or public works too), and call it without specifying the class name.

public class MyClassInDependentProject : authentication
{
    public void DoSomething(int userId, long appId)
    {
        var success = Authenticate(userId, appId);
        …
    }
}

That said, you'll quickly find this to be a bad design. It conflates a cross-cutting concern with all sorts of other classes, and those classes are now precluded from inheriting from any other class.

Composition is a core principle of object-oriented programming, and we have the idiom "Favor composition over inheritance." This simply means that we break down complexity into manageable chunks (classes, which become instantiated as objects), and then compose those objects together to handle complex processing. So, you have encapsulated some aspect of authentication in your class, and you provide that to other classes compositionally so they can use it for authentication. Thinking about it as an object with which you can do something helps, conceptually.

As an analogy, think about needing to drill a hole in the top of your desk. You bring a drill (object) into your office (class). At that point, it wouldn't make sense to simply say "On," because "On" could be handled by your fan, your lamp, your PC, etc. (other objects in your class). You need to specify, "Drill On."

2
4
  • What's the value of the namespace? I can drop the namespace, and remove the need for the using directive in my dependent projects.
    – s15199d
    Aug 30, 2012 at 14:20
  • btw my class is title cased in my actual code. I paraphrased my code for the thread. But, I appreciate the references to the best practices for naming conventions.
    – s15199d
    Aug 30, 2012 at 14:22
  • Namespaces avoid naming collisions. That's especially important for class libraries. They are also used to be able to group classes more logically. I can for instance type YourClassLib.Authentication.SqlServer. to see all classes in that namespace.
    – jgauffin
    Aug 30, 2012 at 14:29
  • Right...simple answer to a simple question...Thanks!
    – s15199d
    Aug 30, 2012 at 14:32
1

You are obliged to use Class in order to invoke your method, just

When is static class just NameClass.Method

When is not static, you must create instance, ClassName ob = new ClassName(); ob.Method();

1

The format of a call like this is class.method, and you really can't escape using the "class" moniker even with the "using" designation. Something has to "host" the function.

1

I don't think what you are asking for is possible without using the base class method Jay mentioned. If all you want is to simplify the syntax whenever you call Authenticate() however, this silly solution (adding an extra method in each class that needs to do authentication) may be just what you want:

private static void DoAuth(long UserID, long AppID){
    authentication.Authenticate(UserID, AppID)
}

If the ID's are always the same within some context, you could also overload it:

private static void DoAuth(){
    DoAuth(1,1)
}

Yes, this does mean you have to add more code wherever you want to do the authentication (that's why it's silly! ;) ). It does also however, also reduce this:

authentication.Authenticate(1,1);

...into this:

DoAuth();

I leave the cost / benefit analysis of this up to you..

0

I know I am some 3 years late but here goes nothing.

To keep your code cleaner and more readable you should create a new namespace for all the re-usable code that you want to have. Then in that namespace have the Authentication Class and Authenticate Function.

To use this you can easily set a using on your namespace and use the function as you are doing like

Authentication.Authenticate()

But to use

Authenticate()

by itself you can always do using MyNamespace.Authentication; and in your code use Authenticate Function directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.