Automapper find best mapping [closed] - c#

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'm using AutoMapper with Entity Framework. I have a hierarchy of entities:
Person
Student
Worker
Each business object has a map to an entity in the database. To convert the business objects into entities I'm using AutoMapper v6.2.2
I wonder if there is a better way to find the "best" mapping or do I really need to have something like this in code:
public PersonEntity MapPerson(Person person)
{
switch (person.Type)
{
case PersonType.Unknown:
return Mapper.Map<PersonEntity>(person);
case PersonType.Student:
return Mapper.Map<StudentEntity>(person);
case PersonType.Worker:
return Mapper.Map<WorkerEntity>(person);
default:
throw new ArgumentOutOfRangeException();
}
}
The good thing is, I already have a "Type" enum for discriminator and things like that but it still feels wrong. Maybe you can help.

You can do this...
var mapped = Mapper.Map(person, person.GetType(), typeof(PersonEntity));
Please refer to the AutoMapper docs for more details.
http://docs.automapper.org/en/stable/Mapping-inheritance.html

AutoMapper is capable of mapping inheritance. The mapping should look like this:
class PersonMapperProfile : AutoMapper.Profile
{
public PersonMapperProfile()
{
this.CreateMap<Student, StudentEntity>();
this.CreateMap<Worker, WorkerEntity>();
this.CreateMap<Person, PersonEntity>()
.Include<Student, StudentEntity>()
.Include<Worker, WorkerEntity>();
}
}
Now when you map a Person to PersonEntity, AutoMapper will create the correct base type or sub types.

Related

Object oriented c# [closed]

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 have a class called Promotion (model class). I want a promotion to have a type called Category where Category is its own model class I'm assuming where I would define all the types of categories such as fast food foot wear jewelry and so on. I'm not quite sure of how to go about this though so for example, my class called category is already a set thing but my class promotion is something where before I create it I need to set it with a category with the viable category options. thank you!
why don't you create a property called Categories of type Category in the Promotion?
enum Algorithms
{
FCFS,
SJF,
PRIORITY,
RR
}
class Process
{
public Algorithm algorithms{get;}
}
For the detailed difference please read the answer from this SO answer

Class Name: Append DTO or Entity [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 7 years ago.
Improve this question
Is there any preference on either appending DTO or Entity to a class name?
Is there any standard around this?
1 Class is used by ORM (EntityFramework) and the other class is used for serialization.
The reason for this is so that there is no duplication of all fields as the EntityFramework is a wrapper around the DTO class(most but not all properties).
The DTO class is in a shared library, and decoupled from EF.
E.g. Which of these is the most common/standard approach?
// 1.
MyNamespace.Entities.MyClass
MyNamespace.Models .MyClassDto
// 2.
MyNamespace.Entities.MyClassEntity
MyNamespace.Models .MyClass
// 3.
MyNamespace.Entities.MyClassEntity
MyNamespace.Models .MyClassDto
In my personal experience your third example is the only implementation I have worked with and it is the one I would argue for because the intent of the object you are working with will always be clear whereas with the other two it only becomes clear when looking at both objects together.
That being said as long as your team comes to an agreement on which to use any would work.
In my opinion, you typically don't want to put implementation details into class names for similar reasons to why you don't want to use Hungarian Notation.
If there's a bit of code that needs to work with both types and differentiate between them, another option is including aliased using statements like this:
using entities = MyNamespace.Entities;
using dto = MyNamespace.Models;
//in code
var myClassEntity = new entities.MyClass();
var myClassDto = new dto.MyClass();
//work with both
My assumption is that the code that needs to work with both types is limited to an isolated library, and that client code typically works with one, not both types.

Is it a bad design to have a structure where one class contains list of another class and vice versa [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 7 years ago.
Improve this question
I was working on design of User Role Management system. In this system a user can have many roles and a role can have many users. It is one to many relationship in between both user and role. I was thinking of a design to establish this relationship as:
Class User
{
List<Role>
}
Class Role
{
List<User>
}
My question is, Is this a bad design.If yes what should be the recommended way to establish relationship.
Regards
I don't understand any previous accusations of a bad design. This is a typical design to map many-to-many relationships. In particular if you use Entity Framework, you get relationships like this all the time.
You can even have a self-reference, why not?
class Person
{
List<Person> Friends;
}
It is not that bad or uncommon to do this in the context of child and parent objects.
Class User
{
List<Role> Children;
}
Class Role
{
List<User> Parents;
}
This way you always know the linking from parents to childs.
An example would be the XMLNode Class in the .NET Framework.
It has ChildNodes and ParentNode properties.
Yes, I would consider this bad design because now you have two lists to maintain, assuming this even compiles with cross referencing.
You are much better to either create a joining object that something else holds a list of (A class that has both user and a role).
Or just pick the one that makes the most sense and then apply some querying on that when need to find all users with a specific role. I.E
myListOfUsers.Where(user => user.Roles.Contains(something));

Get Property's value by using entity framework [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 8 years ago.
Improve this question
I try to get column property's value by using entity framework in c#.
My Code:
foreach (var columns in _context.Properties)
{
var SelectedColumnValue = columns.GetType().GetProperties().GetValue(0)GetType();
}
How can i get value of propert(fieldname/column name) in entity framework ?
Any help will be appreciated.
Thanks
I don't know where to start. If _context is of type DbContext, Properties wouldn't have worked unless you have a table named Properties. So _context is probably typeof(DbContext). Then again, Type class does not have a property named Properties but it has the GetProperties() method.
In that case, each columns would be an instance of PropertyInfo. Then you could have used columns.Name to get the name of that property. Those names would have nothing to do with columns, but they would probably be pluralized names of tables.
tl;dr
I'm lost.

how to name a class that generate other 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 9 years ago.
Improve this question
I have several classes that all implementing an interface:
interface ISample
{
}
class A:ISample
{
}
class B:ISample
{
}
class C:ISample
{
}
and another class that create them based on some situation for example:
class CreateISample
{
ISample Create(string situation )
{
switch(situation )
{
case "create A":
return new A();
case "Create B":
return new B();
case "Create C":
return new C();
}
}
}
What is the best name for this class?
Create ISample is not good, as then I have:
CreateISample.Create("Create A");
which has two Create as part of name. Also CreateISample may do some other things (for example hold some constant values that relates to all instances, or hold a list of created instances and so on). Then CreateISample is not a good name.
Is there any standard for this? I remember that I read a book about design patterns and they suggested a suitable name for this factory pattern.
you are describing the factory pattern http://en.wikipedia.org/wiki/Factory_method_pattern
generally the convention we use is SampleFactory
But don't overthink it. It's really hard to make names like this from samples. for example it is obvious to most people that a square inherrits from shape. so
ShapeFactory.Create("Square");
would make a lot of sense. so look at your problem to see what kind of thing ISample really is. If i makes sense from a business/problem side other programmers who understand the problem can figure it out.

Categories