c# mapping class to another class [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am interested in best design pattern on below issue. Say I have 2 classes with different number of properties. I need to map instance of source class to target class' instance. Mapping source property may not be simply as just equal. There may conditional check and etc. As simplest way would be writing method and accept source class'object as parameter. Then manipulate properties and initialize target class' object. However it is not good, as there will be duplicate code and logic. Because there are will be many type of source classes. So, I will be forced writing code fach convertion. Something comes to my mimd generic methods? Thanks for your time.

Automapper worked fine for our team.

Related

C# 9.0 create a record from a class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I would like to initialize a record from a class. For example, I read in a bunch of objects from a ReST call. These objects should be immutable, so the record type would fit the bill nicely, but I want to write a bunch of code to convert them to records.
Suggestions?
I'm assuming you don't have write access to the API that creates the classes? Because, honestly, it would probably better just to rewrite that.
There are libraries available such as AutoMapper (https://github.com/AutoMapper/AutoMapper).
Beyond that, there are no casts you can use, and unless the classes are highly regular, you need to write custom conversions for each class.

Using common entity models from another microservice [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
We have to microservices.
How can we use an entity model from one microservice to another microservice without needing to maintain codes on both end?
The goal is to take the jsonData from a microservice and map it to entity model that exist in another microservice.
What is the best practice here?
You will need the assembly that contains the types you want to serialize/deserialize jsons. I think it is ok because when you have one service, you expect it to run autonomously, so, if you provide additional fields it should work (because it will not be affected by the deserialization). Now missing fields, the service will thrown exceptions and it is expected as part of the business.
One option, but not recommended (in my opinion), is to deserialize your json into dynamic and you will be able to navigate on the result as you want. I am not sure about the performance of this.

Clean Code - naming related classes [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I've read Clean Code by R.C. Martin and I'm trying to adopt his suggestions about clean code as broadly as possible.
But I'm not sure how to name related classes.
Let's say I have a class named TreeDirectoryList.
I want to cut implementation of this class into many smaller classes.
Let's say I'll create a class named ParentIndexStack.
ParentIndexStack will implement functionality very dependent on TreeDirectoryList, so it's very not probable that this implementation of ParentIndexStack will be useful with any other class in the future.
But the name of ParentIndexStack is very generic, it's possible, that I'll need another class with the same name, within the same project.
So I thought I'll name ParentIndexStack more precise, like TDLParentIndexStack (prefix TDL is from TreeDirectoryList).
Would it be correct ?
I'll end with many classes starting with TDLxxxxx.
One option is to put that set of classes in their own namespace. Then you can have simple, concise names that still communicate the full meaning of the class through the namespace context.

C# method naming best practices [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a coworker that keeps renaming object and method in my code. Add S, removing S etc..
I would like to know what is the best way in your opinion.
Suppose I have a class Client.
This Client can set two reminders (wakeupReminder and leaveReminder).
Each of these reminders have different settings.
So I created a Class called WakeUpReminderSettings. He told me that I should rename it to WakeUpReminderSetting because it is not a collection.
I also created a method that return all reminders settings. I named it GetClientRemindersSettings.
Again, he renamed it to GetClientReminderSettings. He's argument: Only the last word should be pluralized..
I would like to have your thoughts on this.
I think you're right in both cases. A WeakUpReminderSettings can be an aggregate of different settings without being a collection, and if your method return settings for multiple reminders, it makes sense to pluralize reminder even if it's not the last work.
Then again, naming conventions are really something subjective, if your coworker is not above you in the hierarchy, I'd tell him to stop messing with your work for minor changes like this.

Should a class also be responsible for storing itself's data to db? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Let's say I have a class named Rectangle and it has some attributes like: color, width, height etc. So this class will for sure describe this object but I also want to save this object to database and later read and create object from db.
My question is should this class also have methods like "SaveRectangle", "GetOneRectangle ", "GetAllRectangles", "EditRectangle" that handles the SQL operations or is there a other good practice?
I would suggest you check out Martin Fowler's "Patterns of Enterprise Architecture".
There are several different patterns for data persistence. The pattern you describe is "Active Record". It can definitely make things easier in the short term but I have found that it often leads to issues when working with many objects.
I typically choose to use a combination of the "Data Mapper" and "Table Data Gateway" patterns that separates storage/retrieval concerns from the objects themselves. That allows me to handle both separately and, possibly, more efficiently.

Categories

Resources