C# builder pattern

C# builder pattern sample


class Program
{
static void Main(string[] args)
{
string[] partlist1 = { "a", "b", "c" };
string[] partlist2 = { "1", "2", "3" };

Builder builder = new ConcreateClass1();
Director.Construct(builder, partlist1);
Product product1 = builder.GetProduct();
Console.WriteLine(product1.ToString());

builder = new ConcreateClass2();
Director.Construct(builder, partlist2);
Product product2 = builder.GetProduct();
Console.WriteLine(product2.ToString());

Console.ReadLine();
}
}

class Director
{
public static void Construct(Builder builder, string[] parts)
{
parts.ToList().ForEach(z => {
builder.BuildPart(z);
});
}
}

public abstract class Builder
{
public abstract void BuildPart(string partName);
public abstract Product GetProduct();
}

public class ConcreateClass1 : Builder
{

private Product part = new Product();

public override void BuildPart(string partName)
{
part.Add(partName);
}

public override Product GetProduct()
{
return part;
}
}

public class ConcreateClass2 : Builder
{

private Product part = new Product();

public override void BuildPart(string partName)
{
part.Add(partName);
}

public override Product GetProduct()
{
return part;
}
}

public class Product
{
private List<string> _parts = new List<string>();

public void Add(string part)
{
this._parts.Add(part);
}

public override string ToString()
{
string parts = string.Empty;
_parts.ForEach(z =>
{
parts += " " + z;
});

return String.Format("Part list {0}", parts);
}
}

Advertisements

#builder-pattern, #csharp-pattern

How to create early bound class for crm?

For this use to CrmSvcUtil.exe. Go to sdk folder (D:\Libraries\MS Dynamics CRM SDK\SDK\Bin) my version 7.1.1.xx

Create a sample bat file, create.bat. Open text editor and put this.

For AD Crm

CrmSvcUtil.exe /url:https://url/organiztionname/XRMServices/2011/Organization.svc    /out:CrmObjects.cs /username:yourusername /password:yourpassword /domain:yourdomain    /namespace:CrmEntityClasses /serviceContextName:CrmEntityContext

For IFD Crm

CrmSvcUtil.exe /url:https://url/XRMServices/2011/Organization.svc     /out:CrmObjects.cs /username:domain\yourusername /password:yourpassword

Run the create.bat file, after then created CrmObjects.cs

Copy to your project and create the context object


CrmEntityClasses.CrmEntityContext  context = new CrmEntityClasses.CrmEntityContext(service)

“service” object must be inherit IOrganizationService !

Lets try to simple code


var list = from z in context.ContactSet select z;

#crm-entity-class, #crmutilsvc, #dynamics-crm, #early-bound, #entity-class

Where is the email attachments ?

If you need email attachments this sample may be helping for you.

Email attachments in the ActivityMimeAttachment entity. Other attachment in the Annotation entity (for default!)

Firstly get email


Entity emailEntity = service.Retrieve("email", new Guid(emailGuidId), new ColumnSet(true));

Here this


QueryExpression qExpression = new QueryExpression();

qExpression.ColumnSet = new ColumnSet(true);
qExpression.EntityName = ActivityMimeAttachment.EntityLogicalName;
qExpression.Criteria.AddFilter(new FilterExpression(LogicalOperator.And));
qExpression.Criteria.AddCondition(new ConditionExpression("ObjectId", ConditionOperator.Equal, emailEntity.Id.ToString()));

var attachments = service.RetrieveMultiple(qExpression);

#attachments, #email, #email-attachments

Value parameter ,reference parameter and output parameter differences

Reference parameters must be assigned required the outside method !

Output parameters non required assign but the method inside must be assign !

Here this ->


class Program
{
static void Main(string[] args)
{
MyClass param1;
int param2 = 50;

//Write1(param1, param2);
//Write2(ref param1, ref param2);
//Write3(out param1, out param2);

Console.WriteLine("(default) param 1 : {0} - param 2 : {1}", param1.Val, param2);

Console.ReadLine();
}

public static void Write1(MyClass param1, int param2)
{
param1.Val += 10;
param2 += 10;
Console.WriteLine("(value) param 1 : {0} - param 2 : {1}", param1.Val, param2);
}

// ref parameters must be assigned required
public static void Write2(ref MyClass param1, ref int param2)
{
param1.Val += 10;
param2 += 10;
Console.WriteLine("(reference) param 1 : {0} - param 2 : {1}", param1.Val, param2);
}

// out parameter must be assigned in method parateter
public static void Write3(out MyClass param1, out int param2)
{
param1 = new MyClass();
param2 = 50;

param1.Val += 10;
param2 += 10;
Console.WriteLine("(reference) param 1 : {0} - param 2 : {1}", param1.Val, param2);
}
}

public class MyClass
{
public int Val { get; set; }

public MyClass()
{
this.Val = 50;
}
}

#csharp, #output-parameter, #reference-parameter, #value-parameter

Custom json parser (Newtonsoft)

If you need custom json parser, this sample maybe helping for you,


using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CustomJsonParser
{
class Program
{
static void Main(string[] args)
{
JsonConverter[] converters = { new MyConverter() };
string jsonData = "sampe json data";
Container container = JsonConvert.DeserializeObject<Container>(jsonData, new JsonSerializerSettings() { Converters = converters });
}
}

public class Container
{
public List<Room> RoomList { get; set; }
public List<Home> HomeSectionList { get; set; }
}

public interface Home
{ }

public class Door : Home {
public String DoorName { get; set; }
}

public class Windows : Home {
public String WindowName { get; set; }
}

public class Room {
public String RoomName { get; set; }
}

public class MyConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Home));
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);

if (FindClassName(jo["__class"].Value<string>(), "Room"))
return jo.ToObject<Windows>(serializer);

if (FindClassName(jo["__class"].Value<string>(), "Door"))
return jo.ToObject<Door>(serializer);

if (FindClassName(jo["__class"].Value<string>(), "Window"))
return jo.ToObject<Windows>(serializer);

return null;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public bool FindClassName(string jObjectString, string className)
{
bool existClassName = jObjectString.Split(':').First().Equals(className);

return existClassName;
}
}

}

#csharp-deserialize, #custom-deserialize, #interface-deserialize, #json, #json-deserialize, #newtonsoft

Xrm page ui controls is null or not an object

Xrm.Page.ui.control functions only run this actions (onload, onchange, onsave and onkeypress(2016 feature) ) This functions call in action. If it still does not work, try this methods and check fields.


Xrm.Page.getAttribute("fieldName") or Xrm.Page.getAttribute("fieldName").getValue()

#control-is-null, #crm, #dynamics-crm, #getattribute, #getvalue, #xrm, #xrm-page