For my EF (Entity Framework 4.1) I was using POCO templates to generate DTOs.

POCO are very simple objects but I did want to separate my MVC UI from Entity Framework dependency, so I”ve used a wrapper classes for my POCO classes from EF.

Something like this, where Medication would be a POCO templated class from EF and MedicationModel is a wrapper.

public class MedicationModel:Mediation{

………………..

}

Another approach could be a total encapsulation of Medication class, but… I did it this way.

Well so far so good, but the question was how to convert and/or assign data to a simple entity to persist it in DB?

My solution in this case was simple. I created an extension for my classes.

All of my extensions would implement one method on the common static extension class:

Snippet from the extension class.

       public static List<AppointmentForm> ToAppointmentFormList(this ICollection<AppointmentFormModel> forms) {
            List<AppointmentForm> list = new List<AppointmentForm>();
            foreach (var f in forms) {
                list.Add(f.ToAppointmentForm());
            }
            return list;
        }

        #region - Base function for Objects conversions -

        private static void To<F, T>(F from, ref T to) {
            ExtentionBase.To<F, T>(from, ref to);
        }

        #endregion - Base function for Objects conversions -
The base Generic method looks like this:
public static class ExtentionBase {
        #region - Base function for Objects conversions -

        public static void To<F, T>(F from, ref T to) {
            if (from != null) {
                Type fromType = from.GetType();
                Type toType = to.GetType();

                PropertyInfo[] fromPropertyInfo = fromType.GetProperties();
                PropertyInfo[] toPropertyInfo = toType.GetProperties();

                foreach (var prop in fromPropertyInfo) {
                    var targetProperty = toType.GetProperty(prop.Name);
                    if (targetProperty != null && targetProperty.CanWrite) {
                        try {
                            var value = prop.GetValue(from, null);
                            if (value != null && (value.GetType().BaseType.Name.ToString().Equals("ValueType") ||
                                                  value.GetType().BaseType.Name.ToString().Equals("Object") ||
                                                  value.GetType().BaseType.Name.ToString().Equals("RelatedEnd"))
                               ) {
                                targetProperty.SetValue(to, value, null);
                            }
                        } catch {
                            Console.Out.WriteLine("Error");
                        }
                    }
                }
            }
        }

        #endregion - Base function for Objects conversions -
    }
To get the source code goto: http://clires3.codeplex.com
To see the app demo goto http://clires.tateeda.com

I have run into issue with data serialization today.

I needed to update my very complex object by calling WCF services.

Here is the error I got.

There was an error while trying to serialize parameter http://tateeda.com/2011/11:request. The InnerException message was ‘Type ‘System.Collections.Generic.List`1[[Tateeda.DomainModel.SystemModel.System, Tateeda.DomainModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]’ with data contract name ‘ArrayOfMedication:http://schemas.datacontract.org/2004/07/Tateeda.DomainModel.SystemModel’ is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.’.  Please see InnerException for more details.

I got it to work, but there is a list to look for:

  1. Your class and all classes your class may based on have to have a [DataContract] and [DataMember]
  2. Add all classes in base classes to list of known types [KnownType(typeof(Person))]
  3. Make sure that you also decorate your Interface with ServiceKnownType
Here is the example Request class:
    [KnownType(typeof(MedicationSummary))]
    [KnownType(typeof(MedicationBase))]
    [KnownType(typeof(ComparableMedication))]
    [KnownType(typeof(Code))]
    [KnownType(typeof(Code.StatusCode))]
    [DataContract]
    public class MedicationRequest{
    [DataMember]
    public RequestDraft Medication{get;set;}
    }
Known types above are referenced in RequestDraft object.
Interface decoration Example:
    [ServiceKnownType(typeof(Code))]
    [ServiceKnownType(typeof(List<Code>))]
    public interface IMyService
    {
        [OperationContract]
        List<string> GetMedication(string[] codes);

        [OperationContract]
        AddSectionResponse AddSection(Section section);

}

I run today into specific need to return only selected columns from a table but also I had to join on more then one columns.
In my case UnitOfWork is an Interface of tables something like this:
IObjectSet<Form> Forms{get}

On my UnitOfWork Interface look into Open Source CLIRES-3 at http://tateeda.com or http://clires3.codeplex.com
Here what I got Entity Framework 4.0 and LINQ....
var list = (from fa in UnitOfWork.FormAnswers
                        join q in UnitOfWork.Questions
                        on new { fa.QuestionId, fa.AnswerId } equals new { q.QuestionId, q.AnserId }
                        where q.FormId == formId && fa.AppointmentFormId == appointementFormId
                        select new {
                            fa.Notes,
                            fa.FreeTextAnswer
                        }).ToList();

Steps on how to save your SharePoint password:

  1. Add the SharePoint site to the Local Intranet Zone in Internet Explorer. To do this, go to Tools > Internet Options > Security. In Local Intranet, click the Sites button and then Advanced.
  2. Add your SharePoint URL. Click Ok.
  3. Go to Start > Control Panel > User Accounts > Credential Manger > Generic Credentials > Add a Generic Credential
  4. Enter your site https://sharepoint.yoursite.com user name with domain mydomain\username 
  5. Enter your password and click OK.
  6. Log out
  7. Log in again
Works for me on Windows 7 and SharePoint 2010