As in clean architecture, we have to define the Entities in Domain layer and Models in Data layer. Now the issue that i am facing is in converting the entities into models when we are passing that as a request object in repositories.
here is the diagram which depicts the relationship amongst the entities (in brown) and models (in green).
Now, what is the simplest way to convert the entities to model in dart because implementing a mapper and then copy one field from another field seems a very tedious job and when there are nested objects in class (i.e. UserProfile data in below diagram) takes lots of time. so is there any library that exists or a better approach that could seamlessly convert entities to model.
abstract class Mapper<E, D> {
D mapFromEntity(E type);
E mapToEntity(D type);
}
You easily can convert from model to entity, if your model extends from your entity. Because, you do not need a mapper for this case actually. You pass fields to super() while initialisation.
class UserEntity {
final String id;
final String name;
final String surname;
final String? avatarImage;
UserEntity({required this.id, required this.name, required this.surname, this.avatarImage});
}
class UserModel extends UserEntity {
UserModel({
required String id,
required String name,
required String surname,
String? avatarImage,
}) : super(name: name, id: id, surname: surname, avatarImage: avatarImage);
Map<String, dynamic> toMap() {
return {
'id': this.id,
'name': this.name,
'surname': this.surname,
'avatarImage': this.avatarImage,
};
}
factory UserModel.fromMap(Map<String, dynamic> map) {
return UserModel(
id: map['id'] as String,
name: map['name'] as String,
surname: map['surname'] as String,
avatarImage: map['avatarImage'] as String,
);
}
}
final UserEntity user=UserModel.fromJson(someMap);
P.s. You can see also Converter<S,T> pre-made abstract class for mapping
Related
in my model a Author has many Books.
Is it possible to get the amount of books per author a transient property?
This is what I tried but it says:
NoSuchMethodError: The getter 'length' was called on null.
My inspiration came from the docs.
class Author extends ManagedObject<_Author> implements _Author {
#Serialize(input: false, output: true)
int get numBooks => books.length;
}
class _Author {
#primaryKey
int id;
#Column(unique: true)
String name;
ManagedSet<Book> books;
}
class Book extends ManagedObject<_Book> implements _Book {}
class _Book {
#primaryKey
int id;
#Relate(#books)
Author author;
}
I am using Aqueduct 3.2.1.
I think it isn't possible cause book is a related field.
You can use .join() in controller in order to get all the books.
In Mongoose my enum is is defined like this:
personType: {
type:String,
enum: ['Contact','Donor','Resident'],
},
Is there a simple way to make a similar definition in the Breeze metadata?
You can define an enum in a class library:
public enum PersonType
{
Contact = 0,
Donor = 1,
Resident= 2
}
Then, In the conroller:
[HttpGet]
public object Lookups()
{
// Some lookup data go here
var PersonType = Enum.GetValues(typeof(PersonType));
return new {
// Some lookup objects
PersonType }
}
Hence, you can get the PersonType as an object along with the lookups from the client side.
I have a blocking issue on my serverside filtering and sorting of data.
My Kendo Grid sends requests to my C# backend.
This is what happens:
I fetch all Employee objects (I use Data Access)
An employee has a Person object, which contains person info like FirstName, LastName etc.
And it also has a GroupName
So my scheme is:
schema: {
model: {
id: "ID",
fields: {
FirstName: { type: "string", from: "Person.FirstName" },
LastName: { type: "string", from: "Person.LastName" },
GroupName: { type: "string" },
}
}
},
When I use a filter or sorting on the firstname or lastname, it wil try to filter the employee on attribute "Person.FirstName".
But the person is an addition to my DataAccess object, so it is not in the database.
When I use a filter or sorting on GroupName, it will also try to just filter on the groupname. But the groupname is filled in the backend and comes from EmployeeGroup.Name
So I am not able to filter serverside on any of these rows.
I would really love to know if there is any possibility to filter on custom attributes or childattributes.
Thanks in advance!
This nuget package could help you : https://www.nuget.org/packages/KendoGridBinderEx
But what you need to do is create a ViewModel object which contains all properties you want, so
class EmployeeVMO
{
public string FirstName {get;set;}
public string LastName {get;set;}
public string GroupName {get;set;}
}
You need AutoMapper to map this ViewModel object to you real database object. When these mappings are correctly applied, KendoGridBinderEx can be used which will automatically map Model to ViewModel (and back)
For some code example check this source file.
See this live example.
Im using MVC 4 my ActionController recives the following Json:
{
"MainId": 1,
"Actions": [
{
"Attribute1ClassA": 1,
"Attribute2ClassA": 2
},
{
"Attribute1ClassB": 3,
"Attribute2ClassB": 4
},
{
"Attribute1ClassC": 5
}
]
}
and the Controller:
[HttpPost]
public ActionResult Commit(ActionsSummaryViewModel summary)
{
//DO stuff
}
and declaration for classes:
public ActionsSummaryViewModel
{
public int MainId {get;set;}
public IList<MainClass> {get;set;}
}
public class MainClass
{
}
public class ClassA : MainClass
{
public int Attribute1ClassA {get;set;}
public string Attribute2ClassA {get;set;}
}
public class ClassB : MainClass
{
public int Attribute1ClassB {get;set;}
public string Attribute2ClassB {get;set;}
}
public class ClassC : MainClass
{
public int Attribute1ClassC {get;set;}
}
So now, how can i manage the deserialization for the MainClass when the action controller receive the JSON ? because when i call the action the list items are null.
if part of the solution is Json.NET, how i can implement for MVC 4 controllers?
Thanks for your help.
You need a property or set of properties from which you can determine which type the class is to use this method. Using JSON.NET, I deserialize the incoming JSON as a dynamic object, then check the common property, determine the type, and deserialize the value again this type using my model type:
// I'm assuming here you've already got your raw JSON stored in 'value'
// In my implementation I'm using the Web API so I use a media formatter,
// but the same principle could be applied to a model binder or however
// else you want to read the value.
dynamic result = JsonConvert.DeserializeObject(value);
switch ((string)result.type)
{
case "typeone":
return JsonConvert.DeserializeObject<ModelOne>(value);
// ...
default: return null;
}
There's a little bit of extra overhead here because you're deserializing twice, but it's worth it in most cases to me because it's easy to understand what's going on and add new types as needed.
You could parse JSON into dynamic object instead using Json.NET:
using Newtonsoft.Json.Linq:
dynamic data = JObject.Parse("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");
string name = data.Name;
string address = data.Address.City;
I've a model class below:
class Person
{
string FirstName {get;set}
string ABNNumber {get;set}
}
This object gets sent to UI via JsonResult. The problem is I don't want to sent ABNNumber to UI due to security risk.
Is there anyway (any attribute) which I can use to achive above?
Thanks
Use attribute [ScriptIgnore]. It will help:
class Person
{
string FirstName {get;set}
[ScriptIgnore]
string ABNNumber {get;set}
}