Asked by:
Passing an inherited class into a method help

Question
-
User1034446946 posted
I have static class which accepts a List of an inherited class, when i build it to accept a single class it works,however i need ti accept multiple,and in this can I can't get it to work any suggestions?
Monday, March 18, 2019 2:22 AM
All replies
-
User303363814 posted
I can't get it to workCan you add some more detail? What have you tried? What was the error?There are a few different things that you might be trying to do and they have different solutions. It is easier to explain with a specific example. So, please, can we see some code of what you have tried?
Monday, March 18, 2019 3:10 AM -
User-1174608757 posted
Hi EnenDaveyBoy,
According to your description ,static class couldn't be instantiated and inherited. You should use it as a static object.So, if you have met the problem.Could you please share your codes?It will help us to solve your problem.
Best Regards
Wei
Monday, March 18, 2019 6:28 AM -
User1034446946 posted
public modelc :modelb {} public modelb : modela {} public modela { public int Id {get;set;}
}
public static class Helper
{ public static List<T> Map<T>(this List<T> entities, Guid primaryId, IList<modela> confirmedEntities) where T : someOtherModel { return entities; }
}this works
var i = new modelc(); entity.SomeNaviagtionEntity.Map(1, i);
but this doesn't
var i =new List<modelc>(); entity.SomeNavigationEntity.Map(1, i);
but i need the second one to work,any suggestions would be appriciated
I am trying to manually map many to many relationships in .netcore 2.1 ef, i am build a generic static class to map it, to the inherited class
Monday, March 18, 2019 10:49 AM -
User475983607 posted
There are many issues with this design. The main issue is you are building an extension method which requires a static class. An extension method is a way to add functionality to an existing type without making changes to the existing source code.
The other issues with the code shown is you cannot inherit from a static class and a static class by definition cannot have members.
A minor issue is you can replace List<T> just T.
Can you explain what problem "manually mapping many-to-many relationships" solves? The relationship is exposed through the navigation properties and for the most part simply known.
Monday, March 18, 2019 5:38 PM -
User1034446946 posted
There are many issues with this design. The main issue is you are building an extension method which requires a static class. An extension method is a way to add functionality to an existing type without making changes to the existing source code.
The other issues with the code shown is you cannot inherit from a static class and a static class by definition cannot have members.
A minor issue is you can replace List<T> just T.
Can you explain what problem "manually mapping many-to-many relationships" solves? The relationship is exposed through the navigation properties and for the most part simply known.
I don'tlike automapper,when models get complex, i spend all my timeon automapper telling it what not to do,and I don'tmind building the maps it's simple enough.
According to everything I have read and pluralsight, EF core at this time doesn't automatically map many to many in the same way as EF 6, so i need to build the navigation model,and I don't want to build a helper class for each time I have a many to many relationship that needs mapping.
Monday, March 18, 2019 7:26 PM -
User475983607 posted
Sorry I added additional formatting to my example to give a better example, I am not mapping inhertiance from a static class, the classes are all just model classes which inheriate fromeach other,the static class is just a helper toholdmy mapping.You changed the original code.
I don'tlike automapper,when models get complex, i spend all my timeon automapper telling it what not to do,and I don'tmind building the maps it's simple enough.You're not a fan of automapper. Are you building a similar API?
According to everything I have read and pluralsight, EF core at this time doesn't automatically map many to many in the same way as EF 6, so i need to build the navigation model,and I don't want to build a helper class for each time I have a many to many relationship that needs mapping.I use EF Core in a project with many-to-many relationships. The many-to-many works according to the docs. Is the problem you do not like adding the "join" entity?
Many-to-many relationships without an entity class to represent the join table are not yet supported. However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.
If so, it looks like EF Core 3.0 will have this feature but it does not stop you from creating a many-to-many now. With all that being said, are you build this feature too?
Monday, March 18, 2019 7:55 PM -
User1034446946 posted
I am just using simple static extension classes to map,super simple, i wish I was clever enough to build my own API for mapping.
I have the many many relationship built,I just need to convert my viewModels to entites and then link the many to many,I forgot I don't need the original entity, as you didn't with EF,so I have converted it to a simplelist of ids.
I heard that it will be EF core 3 forits added, but nothing is set in stone as yet.
Although I would stilllive the know,why I can add a collection of an inherinted class to a method.
Monday, March 18, 2019 8:27 PM -
User475983607 posted
EnenDaveyBoy
I am just using simple static extension classes to map,super simple, i wish I was clever enough to build my own API for mapping.
I have the many many relationship built,I just need to convert my viewModels to entites and then link the many to many,I forgot I don't need the original entity, as you didn't with EF,so I have converted it to a simplelist of ids.
I heard that it will be EF core 3 forits added, but nothing is set in stone as yet.
Although I would stilllive the know,why I can add a collection of an inherinted class to a method.
If I understand, the actual issue is populating an an EF entity from a ViewModel? Can you share the ViewModel and entities or a sample that reproduces this issue?
Maybe this tutorial will help.
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/complex-data-model?view=aspnetcore-2.2
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/update-related-data?view=aspnetcore-2.2
Monday, March 18, 2019 8:37 PM -
User303363814 posted
I'm not 100% sure what you are trying to do since the text you have shown is a long way from being a runnable sample (a runnable sample that anyone can use is a great help to people trying to solve your problem).
But … try defining Map like this
public static List<T> Map<T,U>( this List<T> entities, Guid primaryId, IList<U> confirmedEntities) where T : someOtherModel where U : modela { return entities; }
Thursday, March 21, 2019 11:27 PM