示例#1
0
        public async Task <ContactGetByIdResponse> GetContactByIdAsync(ContactGetByIdRequest req)
        {
            using (var conn = new SqlConnection(_connectionSettings.DefaultConnection))
            {
                var    result    = new ContactGetByIdResponse();
                string storeproc = string.Format("[dbo].[usp{0}GetById_New]", req.ContactType);
                await conn.OpenAsync();

                var dynParm = new
                {
                    ID = req.Id
                };
                var rawResult = await conn.QueryAsync <ContactDTO>(storeproc, dynParm, null, null, CommandType.StoredProcedure);

                if (rawResult != null)
                {
                    result.Data             = rawResult.FirstOrDefault();
                    result.Data.ContactType = req.ContactType;
                }

                return(result);
            }
        }
        public async Task <ContactGetByIdResponse> GetContactByIdAsync(int contactId)
        {
            if (contactId <= 0)
            {
                throw new ArgumentNullException(nameof(contactId));
            }

            var retVal = new ContactGetByIdResponse
            {
                Success = false,
                Message = ""
            };

            try
            {
                // using a stored procedure here is not normally needed, but is also not harmful.
                var resultAsync = await _connection.QueryMultipleAsync("usp_Contact_GetById",
                                                                       new
                {
                    Id = contactId
                }
                                                                       , commandType : CommandType.StoredProcedure);

                var result = resultAsync.ReadSingleOrDefault <Domain.DomainModels.Contact.Contact>();

                retVal.Success = true;
                retVal.Contact = result;
            }
            catch (Exception ex)
            {
                retVal.Success = false;
                retVal.Message = ex.Message;
            }

            return(retVal);
        }