C
C#ā€¢2y ago
joy

interface instead of mapping one by one

hi all, im trying to use an interface to initialize common properties, for example INote that contains all common properties, and multiple classes will inherit from this INote, im trying to achieve something like : initialize once and can be used for all objects that inherit this INote interface previously i was doing one by one mapping for each objects that have common properties and found out there are lots of duplication (some resource say better to do manual mapping that use a library like automapper) so i try to do it using interface, but somehow even if a class inherit this INote i still can't do it like this : Memo resource = QueryVariant(new NoteItem(), "memo"); LongNote longNote = QueryVariant(new NoteItem(), "longNote"); did i miss anything here?
public interface INote{
public long NodeId { get; set; }
public string NoteName { get; set; }
}

public class LongNote : INote {

public long NodeId { get; set; }
public string NoteName { get; set; }

public string barcodeID {get;set;}
public string profileURL {get;set;}
}

public class Memo: INote {

public long NodeId { get; set; }
public string NoteName { get; set; }

public string memoURL {get;set;}
public string memoUniqueID {get;set;}
}

Memo resource = QueryVariant(new NoteItem(), "memo"); //doesnt work

private async Task<INote> QueryVariant(NoteItem item, string type) {
INote note = type switch {
"memo" => new Memo { /*initialize properties here*/ };
// replicate for the other types
_ => // throw some exception here
};
// initialize common properties in note here, i.e:
note.id = item.Id.ToString():
note.noteName = item.name;
// etc
return note;
}
public interface INote{
public long NodeId { get; set; }
public string NoteName { get; set; }
}

public class LongNote : INote {

public long NodeId { get; set; }
public string NoteName { get; set; }

public string barcodeID {get;set;}
public string profileURL {get;set;}
}

public class Memo: INote {

public long NodeId { get; set; }
public string NoteName { get; set; }

public string memoURL {get;set;}
public string memoUniqueID {get;set;}
}

Memo resource = QueryVariant(new NoteItem(), "memo"); //doesnt work

private async Task<INote> QueryVariant(NoteItem item, string type) {
INote note = type switch {
"memo" => new Memo { /*initialize properties here*/ };
// replicate for the other types
_ => // throw some exception here
};
// initialize common properties in note here, i.e:
note.id = item.Id.ToString():
note.noteName = item.name;
// etc
return note;
}
6 Replies
Becquerel
Becquerelā€¢2y ago
I'm not sure why you're using an interface for this instead of inheritance
(some resource say better to do manual mapping that use a library like automapper)
doesn't necessarily mean they're right!
Memo resource = QueryVariant(new NoteItem(), "memo"); //doesnt work
this doesn't work because your QueryVariant method only takes one parameter. it doesn't take a string parameter that you're trying to pass in
joy
joyā€¢2y ago
I see.. so it's okay if i use automapper, is that right? (Actually I'm not sure of a best practice for this problem, hope you could give an advice as well ..) I was trying to map an object one by one previously (object with common properties, so there are redudancy everywhere..) Sry i mistyped the code, it should accept 2 parameters (edited the message), but i mean.. I can't seem to convert it that way
Becquerel
Becquerelā€¢2y ago
Automapper is a useful tool for this kind of situation when people warn against automapper it's usually because people try to use it for important business logic Memo resource = QueryVariant(new NoteItem(), "memo"); //doesnt work QueryVariant returns an INote all Memos are INotes, but not all INotes are Memos you are trying to assign an INote (less specific) to a Memo (more specific) assigning a Memo (more specific) to an INote (less specific) would work
joy
joyā€¢2y ago
I see.. okay understand.. I'll revert my changes to use inheritance instead of interface and use automapper for the current problem I see, no wonder it wont works.., i think there would be lot's of work on using interface for this issue Okay.. thank you for your explanation, noted it down.. šŸ™
Becquerel
Becquerelā€¢2y ago
no prob šŸ™‚
joy
joyā€¢2y ago
šŸ€
Want results from more Discord servers?
Add your server
More Posts
Update Database with EF core doesn't work and no error messagei am trying to create a database with ef core. Adding a migration works. Update database doesn't, widependency injection net frameworkhi all, I'm following a reference on how to use dependency injection in net framework https://stackSafe to ignore non-null value on exit contructor? (disabling warning)Can I safely add this as a ignore? I don't really know how I would fix this otherwiseHow should I start learninghi everybody I started learning c#, going through basics and want to ask those who in industry alreaCreating an instance of a class using DI [Answered]This is purely just out of curiosity. If you have a class which takes some arguments in its ctor andIs there any benefit to write a Property with a private variable ?Why: ```cs private int myVar; public int MyVar { get => myVar; set => myVar = value; } ``` may be beHow add analyzer for suggesting to add missing parameters or removing them?Soo I have analyzer which checks if parameter type is proper soo then I need to make codefix which j[ASP.NET] B-Logic Services, ValidationI am using anemic models, therefore I want to implement Business Logic Services. And the questions Optimizing some string manipulationI want to both substring an input string at the last occurrence of `'/'` and normalize it into *onlyRight way to use Interfaces in Large Scale Blazor Server Web ApplicationsHello there, iam currently working on a <see title> app for something iam not allowed to leak now ia