Question

I have started using ServiceStack and OrmLite for the first time and have got myself in a bit of a pickle.

I have built 2 classes, 1 is to take the input parameters and 1 is to hold the response.

Here they are...

[DataContract]
public class EMEM
{
    public int EMCo { get; set; }
    public string Location { get; set; }
    public string Department{ get; set; }


    [DataMember]
    public string Equipment { get; set; }
    [DataMember]
    public string udDriver { get; set; }
    [DataMember]
    public string udDrillRigCrew { get; set; }
    [DataMember]
    public DateTime udDOTInspectDate { get; set; }
    [DataMember]
    public string udDOTInspectReq { get; set; }
    [DataMember]
    public DateTime udDOTInspectExpire { get; set; }
}

public class EMEMResponse
{
    public int EMCo { get; set; }
    public string Location { get; set; }
    public string Department{ get; set; }
    public string Equipment { get; set; }
    public string udDriver { get; set; }
    public string udDrillRigCrew { get; set; }
    public DateTime udDOTInspectDate { get; set; }
    public string udDOTInspectReq { get; set; }
    public DateTime udDOTInspectExpire { get; set; }
    public string extraField1 { get; set;}
    public string extraField2 { get; set;}
}

At the moment I have the following code calling the database:

public object Get(EMEM request)
    {
        var dbFactory = new OrmLiteConnectionFactory(Global.connString, SqlServerDialect.Provider);
        using (IDbConnection db = dbFactory.OpenDbConnection())
        {
            if (request.Equipment != null)
            {
                List<EMEM> results = db.Select<EMEM>(p => p.Where(ev => ev.Equipment == request.Equipment && ev.EMCo == 1));  // EMCo = 1 has been added as Kent only wants to see this company
                return results;
            }
            else
            {
                List<EMEM> results = db.Select<EMEM>(p => p.Where(ev => ev.EMCo == 1)); // EMCo = 1 has been added as Kent only wants to see this company
                return results;
            }
        }
    }

But my problem is that as you can see it's returning EMEM which doesn't contain extraField1 and extraField2

When I debug the "return result" the values or extraField1 and extraFeild2 are in the results but as they are not in EMEM they are not being sent in the response.

How can I get the result into EMEMResponse and return that instead of EMEM?

Is it something to do with AutoMapping? I just cannot seem to work it out.

Any help would be greatly appreciated.

Thanks

EDIT

I have tried the following but none of the values get copied across...

var test1 = new EMEMResponse { }.PopulateWith(results);
var test2 = results.ConvertTo<EMEMResponse>();
Was it helpful?

Solution

Here is a console application to convert from one typed collection to another (tested on ServiceStack v4.0.9)

using System;
using System.Collections.Generic;
using ServiceStack;


public class Employee
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class EmployeeModel
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Program
{
    public static void Main()
    {
        var results = new List<Employee> {
            new Employee { Id = "BG", FirstName = "Bill", LastName = "Gates" },
            new Employee { Id = "SJ", FirstName = "Steve", LastName = "Jobs" }
        };

        var results2 = results.ConvertAll(x => x.ConvertTo<EmployeeModel>());

        foreach (var result in results2)
        {
            // Will display EmployeeModel instance
            Console.WriteLine(string.Format("{0} {1} {2}", result.Id, result.FirstName, result.LastName));
        }
        Console.Read();
    }
}

OTHER TIPS

You will need to return the *Response type explicitly. Try something like this:

public object Get(EMEM request)
{
    var dbFactory = new OrmLiteConnectionFactory(Global.connString, SqlServerDialect.Provider);
    using (IDbConnection db = dbFactory.OpenDbConnection())
    {
        List<EMEM> results = null;
        List<EMEMResponse> response = new List<EMEMResponse>();
        if (request.Equipment != null)
        {
            results = db.Select<EMEM>(p => p.Where(ev => ev.Equipment == request.Equipment && ev.EMCo == 1));  // EMCo = 1 has been added as Kent only wants to see this company
        }
        else
        {
            results = db.Select<EMEM>(p => p.Where(ev => ev.EMCo == 1)); // EMCo = 1 has been added as Kent only wants to see this company
        }

        foreach (var item in results)
        {
            var test1 = new EMEMResponse().PopulateWith(item);
            test1.extraField1 = "extra";
            response.Add(test1);
        }

        return response;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top