Maintaining IoC and Deserializing JSON.NET

Hi Guys n Gals,

I have recently started looking at my code in terms of design patterns and improvements to make my code more extensible. Obviously the long term goal is to improve the overall cleanliness, maintainability and extensibility.

With that in mind, I am trying to write a piece of login code. I have attached the files and it is a fairly simple piece of code in itself. It is against a public API, so I do not control the API at all.

In my attached example file, I have done two sets of workups. The first is the "easy" way, where I simply create the objects inside the returnlogin function. It's very quick, very easy, not thought out and most importantly (for this example anyway) relies on concrete classes.

So you can see that in the first class "Login1" that I rely on:

MyAPI.CallAPI myCall = new CallAPI(); and;
When I am Deserializing - MyAPI.Responses.LogonResponse logonResponse = JsonConvert.DeserializeObject<MyAPI.Responses.LogonResponse>(response);

In the second workup, the Response is now an interface, and the dependency is injected through the constructor.

The API Call itself has now also itself been injected into the Method via an ICallAPI interface.

And herein my question lies.

The Deserialize method looks like this JsonConvert.DeserializeObject<T>(string). In the first example it is simple, because I am using the concrete classes. However now I am trying to not use the concrete classes. So I have got around this by using <T> and passing its type into the CallAPI Method. This way the method itself doesn't "truly" rely on the concrete class as it is passed through as an agrugment : T thisT

So putting it out there to all the EE pro's - what would be the best way to achieve the deserializing WITHOUT relying on any concerete implementations?

This question is not only about finding the right solution to this particular question, but also a question (for me personally) regarding the best practices involved here. Is the second proposed solution too convoluted, too stupid a solution? Is it simply impractical in real world terms? Any thoughts and suggestions would be much appreciated.

EELoginExample.txt
chudmarekAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chudmarekAuthor Commented:
Just as a bit of an update, I reworked the second sample a little more, as I realised What if I changed Serializers, I am relying specifically on JsonConvert. So in addition to the above, would appreciate a comment or two on the following attached code:

EELoginExample2.txt
0
ste5anSenior DeveloperCommented:
hmm, why should it make sense to inject the response in the constructer and also executing an API call, which overwrites it later?

The only pattern I would think is useful in the Login case is the strategy pattern. You're login class defines the necessary steps to perform, but the actual calls are deferred to a strategy. Here the part to abstract is the entire REST API Call stuff,  e.g.

namespace ConsoleCS
{
    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;

    class Program
    {
        static void Main(string[] args)
        {
            Login login;            
            login = new Login(new CallApiStrategy());
            login.ValidateCredentials("Homer", "D'onut!");

            Console.WriteLine("User: {0}, LoginSuccess: {1}", login.UserName, login.Success);

            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
        
    abstract class LoginStrategy
    {
        public abstract bool ValidateCredentials(string userName, string password);
    }
    
    class CallApiStrategy : LoginStrategy
    {
        public override bool ValidateCredentials(string userName, string password)
        {
            // here you implement all the concreate REST API stuff.
            return false;
        }
    }

    class Login
    {
        private LoginStrategy loginStrategy;
        private string userName;
        private bool success;

        public string UserName { get { return this.userName;  } }
        public bool Success { get { return this.success; } }

        public Login(LoginStrategy strategy)
        {
            this.loginStrategy = strategy;
        }

        public void ValidateCredentials(string userName, string password)
        {
            this.userName = userName;
            this.success = this.loginStrategy.ValidateCredentials(userName, password);            
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
Why are you using dynamic keyword here?

 public string Serialize(dynamic d)
        {
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(d);
            return json;
        }

Open in new window

0
ste5anSenior DeveloperCommented:
This is a question about design (architural) patterns. So I'm not sure whether this is the correct answer..
0
chudmarekAuthor Commented:
Hi Guys,

Sorry for the confusion but I actually selected the wrong answer when finalising this solution. I was meant to award this solution to "ste5an" - Please remedy this. Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.