0

sorry for my stupidity but I'm looking for a way to construct an object from a class that connects to a service and also contains another class with methods for the service.

The problem I'm facing is trying to figure out a way to only have to connect to the service once. I refuse to use global variables and the likes.

I'm finding it difficult understanding the object concepts in C# as my programming background mainly comes from JavaScript.

Any help is most appreciated.

namespace Tests.Classes
{
    public class L
    {
        dynamic uri;
        dynamic service;
        dynamic credentials;
        dynamic proxy;

        public L L()
        {
            this.uri = new Uri("bladie bladie blah");
            this.credentials = new ClientCredentials();
            this.credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
            this.proxy = new OrganizationServiceProxy(this.uri, null, this.credentials, null);
            this.service = (IOrganizationService)this.proxy;
            return this;
        }

        public class OL
        {
            public OL OL(dynamic a)
            {
                this.service = parent.service; // <- doesn't compile
                return this;
            }
        }
    }
}

To make it clear how it's called:

var l = new L();
l.OL("haha");

Maybe my code isn't clear enough. This will keep the categorization fanatics at bay :P.

namespace Tests.Classes
{
    public class L
    {
        Uri uri;
        IOrganizationService service;
        ClientCredentials credentials;
        OrganizationServiceProxy proxy;

        public L L()
        {
            this.uri = new Uri("hBladie Bladie Blah");
            this.credentials = new ClientCredentials();
            this.credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
            this.proxy = new OrganizationServiceProxy(this.uri, null, this.credentials, null);
            this.service = (IOrganizationService)this.proxy;

            return this;
        }

        public class OL
        {
            Entity entity = new Entity();
            IOrganizationService service = null;

            public OL OL(dynamic a)
            {
                if (a is Entity)
                {
                    this.entity = a;
                }
                if (a is string)
                {
                    this.entity = new Entity(a);
                }

                return this;
            }

            public OL attr(dynamic key, dynamic value)
            {
                this.entity[key] = value;
                return this;
            }

            public Boolean save()
            {
                this.parent.service.create(this.entity); // parent does not exist
            }
        }
    }
}

I hate messy programming, I love jQuery style.

Here's how the code has to be used:

new L().OL("haha").attr("Hello", "world").save();

or

var l = new L();
l.OL("HAHA").attr("foo", "bar").save();
l.OL("pff").attr("boppa", "deebop").save();
6
  • First, replace dynamic with var. You don't need dynamic.
    – Jason
    Mar 16, 2012 at 9:07
  • It's not clear what you want parent to be, or why all your variables are declared using dynamic...
    – Jon Skeet
    Mar 16, 2012 at 9:08
  • doesn't answer my question but okay
    – Chris
    Mar 16, 2012 at 9:08
  • 2
    IMHO you need an introductory text to C#, which is widely out of scope for a question here.
    – Jon
    Mar 16, 2012 at 9:09
  • What do you mean not clear? It's supposed to refer to L.service
    – Chris
    Mar 16, 2012 at 9:09

3 Answers 3

1

That would have worked in Java. In C#, you need to pass L to OL's constructor:

public OL(dynamic O, L parent)
{
   this.service = parent.service;
}

By the way, your constructor won't compile, you have two OLs there, and a return.

By the way 2, why are you using so many dynamics?

2
  • Was pure example.. Dynamics because they always work :D (I come from a JavaScript background where everything is dynamic)
    – Chris
    Mar 16, 2012 at 9:15
  • 1
    "because they always work". No, they always compile. Make a typo or some other mistake and they won't work. :) Mar 16, 2012 at 9:37
0

Do somethign like this:

public class OL
{
    dynamic olService = nnull;
    public OL OL(dynamic a)
    {
          this.olService = parent.service; // <- doesn't compile
          //return this; //remove this !!!!
     }
}

EDIT

Just note: try to not use dynamic untill you really need that. Use strong types in C# language as much as it possible.

0

If you want a single service to exist for all instances of your classes, declare it as static. e.g.:

static IOrganizationService service;

Then in your nested class, you can refer to it by L.service.

2
  • How can I change its value when it is static?
    – Chris
    Mar 16, 2012 at 9:38
  • @Chris, I mentioned in my answer that you can refer to it by L.service. e.g.: L.service = ...;
    – Saeb Amini
    Mar 16, 2012 at 10:35

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.