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();
That would have worked in Java. In C#, you need to pass L to OL’s constructor:
By the way, your constructor won’t compile, you have two
OLs there, and areturn.By the way 2, why are you using so many dynamics?