Automapper from SOAP Web Service to View Model - c#

I want to use Automapper for mapping SOAP Web Service Response to a Model which will be used to return the result through a Web API.
Mostly of the attributes returned in the object by the web service are codes, we want to show in the response of our api the descriptions related to those codes.
For example:
The web service response with a list of:
<charge>
<type>ABC</type>
<qualifier>3</qualifier>
<periodCode>004</periodCode>
<code>STE</code>
</charge>
<charge> ... </charge>
Which will be encapsulated in a class like this:
class Charge {
string type { get; set; }
string qualifier { get; set; }
string periodCode { get; set; }
string code { get; set; }
decimal rate { get; set; }
}
Our model is:
public class RCharge {
public string Description { get; set; }
public bool? IncludedInRate { get; set; }
public decimal? AmountValue { get; set; }
public string Period { get; set; }
}
I have stored in a database the information related to the codes and descriptions, as all codes have their own description.
The problem is how to map from the code returned in the web service, to the description. I have this code, and I could make a call to the database in search of the code and get the description, but is it ok? I guess the ConstructUsing is executed for every item in the response, so make a query here would result in a bunch of requests to DB.
AutoMapper.Mapper.Initialize(config => {
config
.CreateMap<Charge, RCharge>()
.ConstructUsing(s => RChargeConstructor.Construct(s));
});
public class RChargeConstructor {
public static RCharge Construct(ResolutionContext context) {
if (context == null || context.IsSourceValueNull)
return null;
var src = (Charge)context.SourceValue;
return new RCharge() {
Description = src.type, // want description from DB
IncludedInRate = src.qualifier == "3",
AmountValue = src.rate,
Period = src.periodCode // want description from DB
};
}
}
Is there a good approach for doing this kind of mapping?

Related

Custom error objects for .Net Core 3 web api

I am currently developing a web api in .NET Core 3. I currently have the following model for my error response object:
public class ErrorRo
{
public string Message { get; set; }
public int StatusCode { get; set; }
public string Endpoint { get; set; }
public string Parameters { get; set; }
public string IpAddress { get; set; }
}
This is a mandated response I need to implement, management has pushed this. It allows more verbose error messages for people hitting our API so that they know what went wrong.
At the moment I am currently populating this object manually in the methods themselves. Is there a way where I can overwrite the response methods. I.e. can I override the BadRequest of IActionResult to automatically populate these fields?
Thanks!
You can use result filters for this purpose. Add a filter which repalces result before sending it back
Model
public class CustomErroModel
{
public string Message { get; set; }
public int StatusCode { get; set; }
public string Endpoint { get; set; }
public string Parameters { get; set; }
public string IpAddress { get; set; }
}
Filter
public class BadRequestCustomErrorFilterAttribute : ResultFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext context)
{
//todo: check for BadRequestObjectResult if anything is returned for bad request
if (context.Result is BadRequestResult)
{
var result = new CustomErroModel
{
StatusCode = 200, //you status code
Endpoint = context.HttpContext.Request.GetDisplayUrl(),
Message = "some message",
IpAddress = context.HttpContext.Connection.RemoteIpAddress.ToString(), //find better implementation in case of proxy
//this returns only parameters that controller expects but not those are not defined in model
Parameters = string.Join(", ", context.ModelState.Select(v => $"{v.Key}={v.Value.AttemptedValue}"))
};
context.Result = new OkObjectResult(result); // or any other ObjectResult
}
}
}
Then apply filter per action or globally
[BadRequestCustomErrorFilter]
public IActionResult SomeAction(SomeModel model)
or
services
.AddMvc(options =>
{
options.Filters.Add<BadRequestCustomErrorFilterAttribute>();
//...
}
Well it depends on the scenario, but one possible approach could be to use a middleware using a similar strategy like the one described in this question, so that you complete the response with extra information.

C# / MVC Validate model data and call external API based on the results

I have created an WebAPI web app and I would like to validate the data when POST and based on the results to call an external API.
The data will be saved in the database as it is, apart from the validation results.
Validation will be done only for calling the external API.
I have created the logic for posting to the external API but I'm not quite sure how it will be the optimal way to validate the data.
My model includes 10 classes like the below Class1 with multiple properties and I've created a controller for each of them.
The properties can have the true/false values but as strings.
public class Class1
{
public ICollection<Class1Data> Data { get; set; }
}
public class Class1Data
{
public int Id { get; set; }
public string Prop1{ get; set; }
public string Prop2 { get; set; }
..
public string Prop10 { get; set; }
}
WebAPI contoller for POST:
[ResponseType(typeof(Class1))]
public async Task<IHttpActionResult> PostClass1(Class1 class1)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Class1.Add(class1);
await db.SaveChangesAsync();
return CreatedAtRoute("DefaultApi", new { id = Class1.Id }, class1);
}
I've manage somehow to validate one property and POST to external API but not quite sure how I can do that for all my model classes ( I have around 10, 20 props each ).
var notValid = Class1.Data.Where(x => x.Prop1 == "False");
if (notValid != null)
{
foreach ( var fault in notValid )
{
// Call external API using fault.Prop1 / fault.Prop5 / ..
}
}
How could I achieve this?
I hope that my question makes any sense to you.
The simplest way is to use Data Annotations:
Examples:
[StringLength(100)]
public string AccountKey { get; set; }
[Required]
[StringLength(100)]
public string FirstName { get; set; }
Or if you need custom validations you can define them as Custom Validation Attributes and use them like below:
[Required]
[CountryCode]
[StringLength(3)]
public string CountryCode { get; set; }
In this sample [CountryCode] is a Custom validation which you can implement like this:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
public class CountryCodeAttribute : RegularExpressionAttribute
{
public CountryCodeAttribute() :
base("^[A-z]{2,3}([-]{1}[A-z]{2,})?([-]?[A-z]{2})?$")
{
ErrorMessage = "Invalid country code.";
}
}
You will need to import this namespace for this kind of validation:
System.ComponentModel.DataAnnotations

How to return specific set of data from a class

I have a similar structure to the one below
Base class
public class BaseClass
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public Guid Guid { get; set; }
public string Hometown { get; set; }
}
Derived Class
public class DerivedClass : BaseClass
{
public List<DerivedClassDataItem> Data { get; set; }
}
Data class
public class DerivedClassDataItem
{
public string Datum1 { get; set; }
public string Datum2 { get; set; }
public string Datum3 { get; set; }
public string Datum4 { get; set; }
public int Datum5 { get; set; }
public DateTime Datum6 { get; set; }
}
What is the best practice to return specific set of info from the DerivedClass?
a potential set could be:
Name, Address, Guid and then a Data list that only contains Datum1 and Datum4
I could see anonymousTypes, Tuples or another set of class(es), all to be valid approaches.
My concern about creating new set of classs for the set returned is that the class(s) structure will be similar to the structure of the three mentioned above except it will have fewer selected members, which to me, does not sound ideal. (duplicate code and structure)
Using anonymousTypes was my initial solution to tackle this, something like
List<DerivedClass> list = new List<DerivedClass>();
var mySet = list.Select(d => new
{
Name = d.Name,
Address = d.Address,
.
.
.
.
.
Data = d.Data.Select(item => new
{
Datum1 = item.Datum1,
Datum4 = item.Datum4
})
});
but again, that was a headache for us to track through httpResponse and through out API calls.
Should I go with Tuple?
Any insights as to what is the best practice for doing this?
Edit
I am using this set of data to be a response returned by a API/GET call. I will send the set back using HttpRespose and then the framework will transform that into json
this is an actual method we have now
private void populateReturnFile()
{
var returnFileAnonymous = new
{
Vendor = this.Vendor,
OrganizationName = this.OrganizationName,
User = this.User,
Platform = this.Platform,
DictionaryType = this.DictionaryType,
UseCaseId = this.UseCaseId,
Data = this.Data.Select(d => new
{
MigrationTermId = d.MigrationTermId,
ImoLexicalCode = d.ImoLexicalCode
})
};
this.returnFile = returnFileAnonymous;
}
Then my GET will return the retunFile (this is a very simple method, i have remove irrelevant code)
[HttpGet]
public HttpResponseMessage Get(Guid migrationFileId)
{
ProblemList problemList = ProblemList.GetProblemList(migrationFileId);
return Request.CreateResponse(HttpStatusCode.OK, problemList.ReturnFile, new JsonMediaTypeFormatter());
}
If API calls is where you are using these classes, then I personally like to keep it simple and avoid complex inheritance hierarchy. Remember, simple code is good code.
I would make a separate class for each api request/response call. For very simple api calls (ajax requests for example) I like to use anonymous types, but for controllers that only handle API calls I like to create separate classes, organized in a nice folder structure.
Everyone has their "style" but as long as you strive for simplicity your code will be maintainable.

Access properties of extending class in a base class query from DB with Entity Framework TPH pattern

Probably the questin title is not self-explanationary.
I have an ASP.NET MVC5 project with Entity Framework 6. I use code first and I've implemented a TPH pattern for an entity.
There's a base Request entity (I've removed most fields, it's just an example).
public class Request
{
public int Id { get; set; }
public string Title { get; set; }
}
also there's some models with exclusive properties that extend it:
public class RequestQuestion : Request
{
public string Question { get; set; }
public string Answer { get; set; }
}
public class RequestForWork : Request
{
public string WorkName { get; set; }
}
Each of them is added to the EntityContext:
public DbSet<Request> Requests { get; set; }
public DbSet<RequestQuestion> RequestQuestions { get; set; }
public DbSet<RequestForWork> RequestForWorks { get; set; }
When I create some of the requests I add them like this:
var db = new EntityContext();
var requestQuestion = new RequestQuestion{ some initialization };
this.db.Requests.Add(requestQuestion);
this.db.SaveChanges();
And here comes the question. When I query requests of the user
var requests = this.db.Students.Find(userId).Requests.ToList();
in debug I can access the properties of the extending class for every request through the base. So, is there a way to somehow get a type of class that is extending the selected entity and to access it's properties?
Currently to build a list of all requests and fill some viewmodel with data I need to seperately select every type of request and fill a global list from these seperate selects.
You need to cast the base type to its subtype and test for null
foreach (r in requests)
{
var rq = r as RequestQuestion;
if(rq != null)
{
string rq = rq.Question
}
var rfw = r as RequestForWork;
if(rfw != null)
{
string wn = rfw.WorkName;
}
}

Reuse index transformer expressions fails on Id

I'm looking to be able to reuse some of the transform expressions from indexes so I can perform identical transformations in my service layer when the document is already available.
For example, whether it's by a query or by transforming an existing document at the service layer, I want to produce a ViewModel object with this shape:
public class ClientBrief
{
public int Id { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
// ellided
}
From this document model:
public class Client
{
public int Id { get; private set; }
public CompleteName Name { get; private set; }
public Dictionary<EmailAddressKey, EmailAddress> Emails { get; private set; }
// ellided
}
public class CompleteName
{
public string Title { get; set; }
public string GivenName { get; set; }
public string MiddleName { get; set; }
public string Initials { get; set; }
public string Surname { get; set; }
public string Suffix { get; set; }
public string FullName { get; set; }
}
public enum EmailAddressKey
{
EmailAddress1,
EmailAddress2,
EmailAddress3
}
public class EmailAddress
{
public string Address { get; set; }
public string Name { get; set; }
public string RoutingType { get; set; }
}
I have an expression to transform a full Client document to a ClientBrief view model:
static Expression<Func<IClientSideDatabase, Client, ClientBrief>> ClientBrief = (db, client) =>
new ClientBrief
{
Id = client.Id,
FullName = client.Name.FullName,
Email = client.Emails.Select(x => x.Value.Address).FirstOrDefault()
// ellided
};
This expression is then manipulated using an expression visitor so it can be used as the TransformResults property of an index (Client_Search) which, once it has been generated at application startup, has the following definition in Raven Studio:
Map:
docs.Clients.Select(client => new {
Query = new object[] {
client.Name.FullName,
client.Emails.SelectMany(x => x.Value.Address.Split(new char[] {
'#'
})) // ellided
}
})
(The Query field is analysed.)
Transform:
results.Select(result => new {
result = result,
client = Database.Load(result.Id.ToString())
}).Select(this0 => new {
Id = this0.client.__document_id,
FullName = this0.client.Name.FullName,
Email = DynamicEnumerable.FirstOrDefault(this0.client.Emails.Select(x => x.Value.Address))
})
However, the transformation expression used to create the index can then also be used in the service layer locally when I already have a Client document:
var brief = ClientBrief.Compile().Invoke(null, client);
It allows me to only have to have one piece of code that understands the mapping from Client to ClientBrief, whether that code is running in the database or the client app. It all seems to work ok, except the query results all have an Id of 0.
How can I get the Id property (integer) properly populated in the query?
I've read a number of similar questions here but none of the suggested answers seem to work. (Changing the Ids to strings from integers is not an option.)
I have a hard time following your sample fully, Really the best way to dig in to this would be with a failing self-contained unit test.
Nonetheless, let's see if I can pull out the important bits.
In the transform, you have two areas where you are working with the id:
...
client = Database.Load(result.Id.ToString())
...
Id = this0.client.__document_id,
...
The result.Id in the first line and the Id = in the second line are expected to be integers.
The Database.Load() expects a string document key and that is also what you see in __document_id.
The confusion comes from Raven's documentation, code, and examples all use the terms id and key interchangeably, but this is only true when you use string identifiers. When you use non-string identifiers, such as ints or guids, the id may be 123, but the document key is still clients/123.
So try changing your transform so it translates:
...
client = Database.Load("clients/" + result.Id)
...
Id = int.Parse(this0.client.__document_id.Split("/")[1]),
...
... or whatever the c# equivalent linq form would be.

Categories