Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/606016067727683584
added 247 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I'm working on a client proxy for my WebAPI. I've looked at WebApiProxyWebApiProxy but I feel DTOs are unnecessary in my scenario.

I made an "ApiProxyBase"ApiProxyBase class that supports basic Get, Post, Put and Delete functionality using generics. Using this base class I've setup an implementation as follows:

public sealed class MyProxy : ApiProxyBase
{
    private static readonly string ConnectionString = "MyServiceUrl";
    private static readonly MyProxy Instance = new MyProxy(InitializeConnection());

    private MyProxy(string url) : base(url)
    {

    }

    private static string InitializeConnection()
    {
        var conn = ConfigurationManager.ConnectionStrings[ConnectionString];
        if (conn == null) throw new InvalidOperationException("MyProxy requires a connection string with the name '" + ConnectionString + "'");
        return conn.ConnectionString;
    }

    public static class Users
    {
        public static List<User> List()
        {
            return Instance.Get<List<User>>("users/list");
        }

        public static User Single(int userId)
        {
            return Instance.Get<User>("users/" + userId.ToString());
        }

        public static User Add(User user)
        {
            return Instance.Post("users/add", user);
        }

        public static User Update(User user)
        {
            return Instance.Put("users/update", user);
        }

        public static bool Delete(int userId)
        {
            return Instance.Delete("users/delete/" + userId.ToString());
        }
    }
}

}

I've read that public nested classes such as this are considered bad practice. To me it feels much cleaner and more readable than implementing each method directly in the proxy like this:

I plan to add more nested classes for each of the datatypes I need, so the semantics of MyProxy.[Type].AddMyProxy.[Type].Add, MyProxy.[Type].UpdateMyProxy.[Type].Update...etc. feels very smooth when compared to MyProxy.Add[Type]MyProxy.Add[Type], MyProxy.Update[Type]MyProxy.Update[Type].

I'm working on a client proxy for my WebAPI. I've looked at WebApiProxy but I feel DTOs are unnecessary in my scenario.

I made an "ApiProxyBase" class that supports basic Get, Post, Put and Delete functionality using generics. Using this base class I've setup an implementation as follows:

public sealed class MyProxy : ApiProxyBase
{
    private static readonly string ConnectionString = "MyServiceUrl";
    private static readonly MyProxy Instance = new MyProxy(InitializeConnection());

    private MyProxy(string url) : base(url)
    {

    }

    private static string InitializeConnection()
    {
        var conn = ConfigurationManager.ConnectionStrings[ConnectionString];
        if (conn == null) throw new InvalidOperationException("MyProxy requires a connection string with the name '" + ConnectionString + "'");
        return conn.ConnectionString;
    }

public static class Users
{
    public static List<User> List()
    {
        return Instance.Get<List<User>>("users/list");
    }

    public static User Single(int userId)
    {
        return Instance.Get<User>("users/" + userId.ToString());
    }

    public static User Add(User user)
    {
        return Instance.Post("users/add", user);
    }

    public static User Update(User user)
    {
        return Instance.Put("users/update", user);
    }

    public static bool Delete(int userId)
    {
        return Instance.Delete("users/delete/" + userId.ToString());
    }
}

}

I've read that public nested classes such as this are considered bad practice. To me it feels much cleaner and more readable than implementing each method directly in the proxy like this:

I plan to add more nested classes for each of the datatypes I need, so the semantics of MyProxy.[Type].Add, MyProxy.[Type].Update...etc. feels very smooth when compared to MyProxy.Add[Type], MyProxy.Update[Type].

I'm working on a client proxy for my WebAPI. I've looked at WebApiProxy but I feel DTOs are unnecessary in my scenario.

I made an ApiProxyBase class that supports basic Get, Post, Put and Delete functionality using generics. Using this base class I've setup an implementation as follows:

public sealed class MyProxy : ApiProxyBase
{
    private static readonly string ConnectionString = "MyServiceUrl";
    private static readonly MyProxy Instance = new MyProxy(InitializeConnection());

    private MyProxy(string url) : base(url)
    {

    }

    private static string InitializeConnection()
    {
        var conn = ConfigurationManager.ConnectionStrings[ConnectionString];
        if (conn == null) throw new InvalidOperationException("MyProxy requires a connection string with the name '" + ConnectionString + "'");
        return conn.ConnectionString;
    }

    public static class Users
    {
        public static List<User> List()
        {
            return Instance.Get<List<User>>("users/list");
        }

        public static User Single(int userId)
        {
            return Instance.Get<User>("users/" + userId.ToString());
        }

        public static User Add(User user)
        {
            return Instance.Post("users/add", user);
        }

        public static User Update(User user)
        {
            return Instance.Put("users/update", user);
        }

        public static bool Delete(int userId)
        {
            return Instance.Delete("users/delete/" + userId.ToString());
        }
    }
}

I've read that public nested classes such as this are considered bad practice. To me it feels much cleaner and more readable than implementing each method directly in the proxy like this:

I plan to add more nested classes for each of the datatypes I need, so the semantics of MyProxy.[Type].Add, MyProxy.[Type].Update...etc. feels very smooth when compared to MyProxy.Add[Type], MyProxy.Update[Type].

edited tags
Source Link
Chad
  • 196
  • 4

I'm working on a client proxy for my WebAPI. I've looked at WebApiProxy but I feel DTOs are unnecessary in my scenario.

I made an "ApiProxyBase" class that supports basic Get, Post, Put and Delete functionality using generics. Using this base class I've setup an implementation as follows:

public sealed class MyProxy : ApiProxyBase
{
    private static readonly string ConnectionString = "MyServiceUrl";
    private static readonly MyProxy Instance = new MyProxy(InitializeConnection());

    private MyProxy(string url) : base(url)
    {

    }

    private static string InitializeConnection()
    {
        var conn = ConfigurationManager.ConnectionStrings[ConnectionString];
        if (conn == null) throw new InvalidOperationException("MyProxy requires a connection string with the name '" + ConnectionString + "'");
        return conn.ConnectionString;
    }

public static class Users
{
    public static List<User> List()
    {
        return Instance.Get<List<User>>("users/list");
    }

    public static User Single(int userId)
    {
        return Instance.Get<User>("users/" + userId.ToString());
    }

    public static User Add(User user)
    {
        return Instance.Post("users/add", user);
    }

    public static User Update(User user)
    {
        return Instance.Put("users/update", user);
    }

    public static bool Delete(int userId)
    {
        return Instance.Delete("users/delete/" + userId.ToString());
    }
}

}

I've read that public nested classes such as this are considered bad practice. To me it feels much cleaner and more readable than implementing each method directly in the proxy like this:

public static List<User> ListUsers()
{
    return Instance.Get<List<User>>("users/list");
}

I plan to add more nested classes for each of the datatypes I need, so the semantics of MyProxy.[Type].Add, MyProxy.[Type].Update...etc. feels very smooth when compared to MyProxy.Add[Type], MyProxy.Update[Type].

Are there any good reasons not to go with this approach?

I'm working on a client proxy for my WebAPI. I've looked at WebApiProxy but I feel DTOs are unnecessary in my scenario.

I made an "ApiProxyBase" class that supports basic Get, Post, Put and Delete functionality using generics. Using this base class I've setup an implementation as follows:

public sealed class MyProxy : ApiProxyBase
{
    private static readonly string ConnectionString = "MyServiceUrl";
    private static readonly MyProxy Instance = new MyProxy(InitializeConnection());

    private static string InitializeConnection()
    {
        var conn = ConfigurationManager.ConnectionStrings[ConnectionString];
        if (conn == null) throw new InvalidOperationException("MyProxy requires a connection string with the name '" + ConnectionString + "'");
        return conn.ConnectionString;
    }

public static class Users
{
    public static List<User> List()
    {
        return Instance.Get<List<User>>("users/list");
    }

    public static User Single(int userId)
    {
        return Instance.Get<User>("users/" + userId.ToString());
    }

    public static User Add(User user)
    {
        return Instance.Post("users/add", user);
    }

    public static User Update(User user)
    {
        return Instance.Put("users/update", user);
    }

    public static bool Delete(int userId)
    {
        return Instance.Delete("users/delete/" + userId.ToString());
    }
}

}

I've read that public nested classes such as this are considered bad practice. To me it feels much cleaner and more readable than implementing each method directly in the proxy like this:

public static List<User> ListUsers()
{
    return Instance.Get<List<User>>("users/list");
}

I plan to add more nested classes for each of the datatypes I need, so the semantics of MyProxy.[Type].Add, MyProxy.[Type].Update...etc. feels very smooth when compared to MyProxy.Add[Type], MyProxy.Update[Type].

Are there any good reasons not to go with this approach?

I'm working on a client proxy for my WebAPI. I've looked at WebApiProxy but I feel DTOs are unnecessary in my scenario.

I made an "ApiProxyBase" class that supports basic Get, Post, Put and Delete functionality using generics. Using this base class I've setup an implementation as follows:

public sealed class MyProxy : ApiProxyBase
{
    private static readonly string ConnectionString = "MyServiceUrl";
    private static readonly MyProxy Instance = new MyProxy(InitializeConnection());

    private MyProxy(string url) : base(url)
    {

    }

    private static string InitializeConnection()
    {
        var conn = ConfigurationManager.ConnectionStrings[ConnectionString];
        if (conn == null) throw new InvalidOperationException("MyProxy requires a connection string with the name '" + ConnectionString + "'");
        return conn.ConnectionString;
    }

public static class Users
{
    public static List<User> List()
    {
        return Instance.Get<List<User>>("users/list");
    }

    public static User Single(int userId)
    {
        return Instance.Get<User>("users/" + userId.ToString());
    }

    public static User Add(User user)
    {
        return Instance.Post("users/add", user);
    }

    public static User Update(User user)
    {
        return Instance.Put("users/update", user);
    }

    public static bool Delete(int userId)
    {
        return Instance.Delete("users/delete/" + userId.ToString());
    }
}

}

I've read that public nested classes such as this are considered bad practice. To me it feels much cleaner and more readable than implementing each method directly in the proxy like this:

public static List<User> ListUsers()
{
    return Instance.Get<List<User>>("users/list");
}

I plan to add more nested classes for each of the datatypes I need, so the semantics of MyProxy.[Type].Add, MyProxy.[Type].Update...etc. feels very smooth when compared to MyProxy.Add[Type], MyProxy.Update[Type].

Are there any good reasons not to go with this approach?

Source Link
Chad
  • 196
  • 4

WebAPI Client Proxy

I'm working on a client proxy for my WebAPI. I've looked at WebApiProxy but I feel DTOs are unnecessary in my scenario.

I made an "ApiProxyBase" class that supports basic Get, Post, Put and Delete functionality using generics. Using this base class I've setup an implementation as follows:

public sealed class MyProxy : ApiProxyBase
{
    private static readonly string ConnectionString = "MyServiceUrl";
    private static readonly MyProxy Instance = new MyProxy(InitializeConnection());

    private static string InitializeConnection()
    {
        var conn = ConfigurationManager.ConnectionStrings[ConnectionString];
        if (conn == null) throw new InvalidOperationException("MyProxy requires a connection string with the name '" + ConnectionString + "'");
        return conn.ConnectionString;
    }

public static class Users
{
    public static List<User> List()
    {
        return Instance.Get<List<User>>("users/list");
    }

    public static User Single(int userId)
    {
        return Instance.Get<User>("users/" + userId.ToString());
    }

    public static User Add(User user)
    {
        return Instance.Post("users/add", user);
    }

    public static User Update(User user)
    {
        return Instance.Put("users/update", user);
    }

    public static bool Delete(int userId)
    {
        return Instance.Delete("users/delete/" + userId.ToString());
    }
}

}

I've read that public nested classes such as this are considered bad practice. To me it feels much cleaner and more readable than implementing each method directly in the proxy like this:

public static List<User> ListUsers()
{
    return Instance.Get<List<User>>("users/list");
}

I plan to add more nested classes for each of the datatypes I need, so the semantics of MyProxy.[Type].Add, MyProxy.[Type].Update...etc. feels very smooth when compared to MyProxy.Add[Type], MyProxy.Update[Type].

Are there any good reasons not to go with this approach?