Servicestack Ormlite Automapping

时光毁灭记忆、已成空白 提交于 2019-12-13 03:56:05

问题


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>();

回答1:


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();
    }
}



回答2:


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;
    }
}


来源:https://stackoverflow.com/questions/21497290/servicestack-ormlite-automapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!