Skip to content

Instantly share code, notes, and snippets.

@mikeedwards83
Created January 18, 2012 21:52
Show Gist options
  • Select an option

  • Save mikeedwards83/1636013 to your computer and use it in GitHub Desktop.

Select an option

Save mikeedwards83/1636013 to your computer and use it in GitHub Desktop.
Bad code for constructor injection
//from SitecoreClassConfig
internal delegate object Instantiator0();
internal delegate object Instantiator1(object arg1);
internal delegate object Instantiator2(object arg1, object arg2);
internal Dictionary<string, Delegate> CreateObjectMethods { get; set; }
//From SitecoreService.cs
public object CreateClass (bool isLazy, bool inferType, Type type, Item item, params object [] conParams){
if (item == null) return null;
if(conParams == null) conParams = new object[]{};
SitecoreClassConfig config=null;
if (!inferType)
{
//this retrieves the class based on return type
config = InstanceContext.GetSitecoreClass(type);
}
else
{
//this retrieves the class by inferring the type from the template ID
//if ths return type can not be found then the system will try to create a type
//base on the return type
config = InstanceContext.GetSitecoreClass(item.TemplateID.Guid, type);
if (config == null) config = InstanceContext.GetSitecoreClass(type);
}
if (isLazy || type.IsInterface)
{
return ProxyGenerator.CreateProxy(config, this, item, inferType);
}
else
{
if (item == null) return null;
//get the class information
//The conKey is used to dermine which method should be used to construct the class
StringBuilder conKey = new StringBuilder();
if (conParams.Any())
conParams.ForEach(x => conKey.Append(x.GetType().Name));
else
conKey.Append("$EmptyTypes");
string finalConKey = conKey.ToString();
if (!config.CreateObjectMethods.ContainsKey(finalConKey) || config.CreateObjectMethods[finalConKey] == null)
{
//the list of parameters to pass to the construtor
Type[] typeList = conParams.Any() ? conParams.Select(x => x.GetType()).ToArray() : null ;
Type objType = config.Type;
Type [] dynTypes = null;
switch (conParams.Count())
{
case 0:
dynTypes = null;
break;
case 1:
dynTypes = new[] { typeof(object) };
break;
case 2:
dynTypes = new[] { typeof(object), typeof(object) };
break;
}
var dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + objType.Name, objType, typeList, objType);
ILGenerator ilGen = dynMethod.GetILGenerator();
ilGen.Emit(OpCodes.Newobj, objType.GetConstructor(typeList == null ? Type.EmptyTypes : typeList));
for (int i = 0; i < conParams.Count(); i++)
{
ilGen.Emit(OpCodes.Ldarg, i);
}
ilGen.Emit(OpCodes.Ret);
Type degType = null;
switch (conParams.Count())
{
case 0:
degType = typeof( SitecoreClassConfig.Instantiator0);
break;
case 1:
degType = typeof(SitecoreClassConfig.Instantiator0);
break;
case 2:
degType = typeof(SitecoreClassConfig.Instantiator0);
break;
}
config.CreateObjectMethods[finalConKey] = dynMethod.CreateDelegate(degType);
}
object t = config.CreateObjectMethods[finalConKey].DynamicInvoke(conParams);
ReadFromItem(t, item, config);
return t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment