/// <summary> /// Froms the class metadata. /// </summary> /// <param name="classMetadata">The class metadata.</param> /// <returns>MemberSuiteObject.</returns> public static MemberSuiteObject FromClassMetadata(ClassMetadata classMetadata) { var mso = new MemberSuiteObject(); mso.ClassType = classMetadata.Name; mso.ParentTypes = classMetadata.ParentTypes; foreach (var field in classMetadata.Fields) { if (field.Name == null) { //throw new ApplicationException("Null field name found - this should not happen."); continue; } if (mso.Fields.ContainsKey(field.Name)) { /*throw new ApplicationException(string.Format("Field '{0}' for object type '{1}' was specified more than once.", * field.Name, classMetadata.Name));*/ continue; } mso.Fields.Add(field.Name, getValidDefaultValue(field.DataType, field.DefaultValue)); } return(mso); }
public static MemberSuiteObject2 FromMemberSuiteObject(MemberSuiteObject src) { var obj2 = new MemberSuiteObject2(); obj2.ClassType = src.ClassType; obj2.ParentTypes = src.ParentTypes; foreach (var entry in src.Fields) { var value = entry.Value; var childList = value as IList <MemberSuiteObject>; // if we have a list of membersuite objects, we need to do something about it if (childList != null) { //{ // List<MemberSuiteObject2> childList2 = new List<MemberSuiteObject2>(); // foreach (var obj in childList) // childList2.Add(MemberSuiteObject2.FromMemberSuiteObject(obj)); // value = childList2; //} else if (value is MemberSuiteObject) // value = MemberSuiteObject2.FromMemberSuiteObject((MemberSuiteObject) value); continue; } obj2.Fields.Add(new NameValuePair(entry.Key, value)); } return(obj2); }
/// <summary> /// Converts to. /// </summary> /// <param name="mso">The mso.</param> /// <param name="t">The t.</param> /// <returns>MemberSuiteObject.</returns> /// <exception cref="System.Exception">no contructor found for + t.FullName</exception> public static MemberSuiteObject ConvertTo(this MemberSuiteObject mso, Type t) { if (mso == null) { return(null); } var ci = t.GetConstructor(new[] { typeof(MemberSuiteObject) }); if (ci == null) { throw new Exception("no contructor found for " + t.FullName); } var newMSO = (MemberSuiteObject)ci.Invoke(new object[] { mso }); // change out the lists foreach (var pi in t.GetProperties()) { if (typeof(IEnumerable).IsAssignableFrom(pi.PropertyType)) { var genericArgs = pi.PropertyType.GetGenericArguments(); if (genericArgs.Length > 0 && genericArgs[0].IsSubclassOf(typeof(MemberSuiteObject))) { // let's create a new list //!!! Important! Don't use Container in any shared code. Shared code is used by API and JES as well as Console and Portal. // Console and Portal are not configured to use Container! //IList newList = (IList)MemberSuite.Container.GetOrCreateInstance(typeof(List<>).MakeGenericType(genericArgs[0])); var newList = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(genericArgs[0])); var oldList = newMSO[pi.Name] as IEnumerable; if (oldList != null) { foreach (MemberSuiteObject msoOld in oldList) { newList.Add(msoOld.ConvertTo(genericArgs[0])); } } newMSO[pi.Name] = newList; // swap out the list } } } newMSO.ClassType = mso.ClassType; // make sure the old class type is kept if (mso.ParentTypes != null) { // copy over parent types newMSO.ParentTypes = new List <string>(); newMSO.ParentTypes.AddRange(mso.ParentTypes); } return(newMSO); }
/// <summary> /// Clones this instance. /// </summary> /// <returns>MemberSuiteObject.</returns> public MemberSuiteObject Clone() { var mso = new MemberSuiteObject { ClassType = ClassType, ParentTypes = ParentTypes }; mso.Fields = new MemberSuiteFieldsDictionary(); foreach (var item in Fields) { mso.Fields[item.Key] = item.Value; } return(mso); }
/// <summary> /// _absorbs the specified ms object to absorb. /// </summary> /// <param name="msObjectToAbsorb">The ms object to absorb.</param> /// <exception cref="SDKException">MemberSuite Object of type '{0}' cannot absorb an object of type '{1}'</exception> private void _absorb(MemberSuiteObject msObjectToAbsorb) { if (msObjectToAbsorb == null) { return; // nothing to absorb } if (ClassType != null && ClassType != msObjectToAbsorb.ClassType) { throw new SDKException("MemberSuite Object of type '{0}' cannot absorb an object of type '{1}'", ClassType, msObjectToAbsorb.ClassType); } ClassType = msObjectToAbsorb.ClassType; Fields = msObjectToAbsorb.Fields; }
/// <summary> /// Takes an object that is of a type derived from MS Object and converts it to a pure membersuite object /// </summary> /// <param name="memberSuiteObject">The member suite object.</param> /// <returns>MemberSuiteObject.</returns> public static MemberSuiteObject ConvertToMemberSuiteObject(MemberSuiteObject memberSuiteObject) { if (memberSuiteObject == null) { return(null); } //if (memberSuiteObject.GetType() == typeof(MemberSuiteObject)) return memberSuiteObject; // no conversion necessary var clone = memberSuiteObject.Clone(); var newValues = new Dictionary <string, object>(); // now go through and find any lists if (clone.Fields != null) { foreach (var entry in clone.Fields) { if (entry.Value == null || !typeof(IEnumerable).IsAssignableFrom(entry.Value.GetType())) { continue; } var genericArgs = entry.Value.GetType().GetGenericArguments(); if (genericArgs.Length == 0) { continue; } if (genericArgs[0].IsSubclassOf(typeof(MemberSuiteObject))) // we have to clone all of these { var objects = new List <MemberSuiteObject>(); foreach (var listEntry in (IEnumerable)entry.Value) { objects.Add(ConvertToMemberSuiteObject((MemberSuiteObject)listEntry)); } newValues[entry.Key] = objects; // set the new value } } } foreach (var newEntry in newValues) { clone.Fields[newEntry.Key] = newEntry.Value; } return(clone); }
/// <summary> /// Froms the data row. /// </summary> /// <param name="dataRow">The data row.</param> /// <returns>MemberSuiteObject.</returns> public static MemberSuiteObject FromDataRow(DataRow dataRow) { var result = new MemberSuiteObject(); foreach (DataColumn column in dataRow.Table.Columns) { if (column.DataType == typeof(Guid)) { result[column.ColumnName] = dataRow[column] == DBNull.Value ? null : dataRow[column].ToString(); continue; } result[column.ColumnName] = dataRow[column] == DBNull.Value ? null : dataRow[column]; } return(result); }
//public T To<T>() where T : msAggregate, new() //{ // T mso = new T(); // mso._absorb(this); // return mso; //} /// <summary> /// Initializes a new instance of the <see cref="MemberSuiteObject" /> class, /// using the fields in the absorbing object /// </summary> /// <param name="msObjectToAbsorb">The ms object to absorb.</param> public MemberSuiteObject(MemberSuiteObject msObjectToAbsorb) { _absorb(msObjectToAbsorb); }
/// <summary> /// Converts to. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mso">The mso.</param> /// <returns>``0.</returns> public static T ConvertTo <T>(this MemberSuiteObject mso) where T : msDomainObject { return((T)ConvertTo(mso, typeof(T))); }