Reusing models in Web Api - c#

I have a c# class as below
public class CreateStudent
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
and I need another class with following properties
public class EditStudent
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public DateTime Date_of_Birth { get; set; }
}
I have repeated properties except that one field (Date_of_Birth) is added in the EditStudent Model.
Is there an option to reuse some of the properties from previous CreateStudent model
I am going to handle these data as Json objects in my front end angularjs based application

You could do this with a null-able property.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public DateTime? Date_of_Birth { get; set; }
}
This way you only have one Student model that can accommodate both use-cases.

you should be using inheritance feature here.
public class CreateStudent
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
public class EditStudent : CreateStudent
{
public DateTime Date_of_Birth { get; set; }
}

Related

Club the UI model and DB model in C#

I'm having a client, and it sending the following Signature to the Library
Client UI Signature :
namespace Library.Model
{
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public string streetName { get; set; }
public string City { get; set; }
public string State { get; set; }
}
}
Library DB Structure:
namespace Library.Data
{
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public int AddressId { get; set; }
public Address AddressInfo { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string streetName { get; set; }
public string City { get; set; }
public string State { get; set; }
}
}
Here I'm doing the mapping process from Client UI model to DB Structured model. How could I use the DB structured model as like Client model instead of the Client model.
Kindly assist me how efficiently we can share the DB Structured model in Client?
Note: But the Client the Signature should be
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
public string streetName { get; set; }
public string City { get; set; }
public string State { get; set; }
}
Kindly refer Update a class property based on another Property of a calss properties value in the Setter - I need the solution similar to this.
i think you can use PersonViewModel to make this and it will be like you mention
public class PersonViewModel
{
public int PersonId { get; set; }
public string Name { get; set; }
public string streetName { get; set; }
public string City { get; set; }
public string State { get; set; }
}
Make join and file this object
PersonViewModel persons = new PersonViewModel ();
I wish it will help you :)

How do you link two models ASP.NET CORE

currently I have this two models:
Contact.cs
public class Contact
{
public int ConctactId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string City { get; set; }
}
PhoneNumber.cs
public class PhoneNumber
{
public int PhoneNumberId { get; set; }
public string Number { get; set; }
public string Description { get; set; }
public PhoneNumberTypeEnum EnumType { get; set; }
}
My question is, what is a correct way to alter these two so I can have multiple instances of PhoneNumber linked to one Contact? Also, later I would like to display all contacts in View with corresponding phone numbers.
Change your models as following
public class Contact
{
public int ConctactId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string City { get; set; }
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
}
public class PhoneNumber
{
public int PhoneNumberId { get; set; }
public string Number { get; set; }
public string Description { get; set; }
public PhoneNumberTypeEnum EnumType { get; set; }
public int ContactId {get; set;}
public virtual Contact Contact{get; set;}
}

DbSet Ordering by non primitive field

I'm trying to implement an sorting table in MVC with Razor but i'm having such a pain to make the sorting work with some fields which are non primitive. My class model is as follows
public class Turma
{
public int ID { get; set; }
public string Ano { get; set; }
[ForeignKey("Professor")]
public int Professor_ID { get; set; }
public virtual Professor Professor { get; set; }
[ForeignKey("Disciplina")]
public int Disciplina_ID { get; set; }
public virtual Disciplina Disciplina { get; set; }
}
public class Professor
{
public int ID { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string Telefone { get; set; }
}
public class Disciplina
{
public int ID { get; set; }
public string Nome { get; set; }
}
I'm tring to, with an DbSet, sorting it like this:
turmas = turmas.OrderByDescending(s => s.Disciplina.Nome);
Is that even possible?

Entity framework CF model structure

I've been working on this for a while now and i still haven't come up with a good idea.
I have the fallowing 3 types Project, User and Result. A user and a project can have multiple results. My problem now is that a result can be of multiple types (12 right now and that can change). I have no idea how i'm supposed to structure this the only things that this results have in common is a type and an Id and i want a table for each of the different 12 types.
So what i have until now is this.
Project
public class Project :ITypedEntity<ProjectType>
{
public int Id { get; set; }
public string Name { get; set; }
public string Nationality { get; set; }
public virtual ProjectType Type { get; set; }
public string TitleEn { get; set; }
public string TitleRo { get; set; }
public string Acronym { get; set; }
public string ContractNo { get; set; }
public virtual ActivityField ActivityField {get;set;}
public string SummaryEn { get; set; }
public string SumarryRo { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public decimal Value { get; set; }
public bool IsPartner { get; set; }
public ICollection<User> Team{get;set;}
public string Website {get;set;}
public ICollection<INamedEntity> Results;
}
Result
public class Result:ITypedEntity<ResultType>
{
public int Id { get; set; }
public string Name{get;set;}
public ResultType Type { get; set; }
}
User
public class User:Person
{
public int CNP { get; set; }
public virtual Faculty Faculty { get; set; }
public bool IsUSAMV {get;set;}
public virtual ICollection<INamedEntity> Results {get;set;}
public virtual ICollection<Project> Projects {get;set;}
}
I don't think this helps but i'll paste them anyway
public interface INamedEntity:IEntity
{
string Name{get;set;}
}
public interface ITypedEntity<TType>:INamedEntity{
TType Type { get; set; }
}
public interface IEntity
{
int Id { get; set; }
}
Update 1
Some results types
public class BookChapter:INamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Book Book {get;set;}
public int PagesInChapter {get;set;}
public string Pagination {get;set;}
}
public class Book:INamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Person> Authors { get; set; }
public int Year { get; set; }
public Publisher Publisher {get;set;}
public int NumberOfPages { get; set; }
public string Pagination { get; set; }
public int ISBN { get; set; }
}
public class Patent:ITypedEntity<PatentType>
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> Authors { get; set; }
public virtual Person Holder{get;set;}
public string Number{get;set;}
public virtual PatentType Type { get; set; }
}
Sorry if the question is not clear, i'll update it with any other information you need.
Thanks.

EF4.1 Code First Question

I want to create my first application using the EF 4.1 Code First Model. I want to model a magazine subscription but just want to check that my POCO classes are fit for purpose.
The following are my classes. Am I missing anything?
Should Customer be a member of Subscription or should it be just that List be a member of Customer?
Thanks.
public class Subscription
{
public int SubscriptionID { get; set; }
public string CardNumber { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public decimal NetPrice { get; set; }
public decimal Tax { get; set; }
public decimal Discount { get; set; }
public string PromotionalCode { get; set; }
public Customer Customer{ get; set; }
}
public class Customer {
public int CustomerID { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public PostalAddress PostalAddress { get; set; }
public string EmailAddress { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public DateTime DateOfBirth { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string SecurityQuestion { get; set; }
public string SecurityAnswer { get; set; }
public bool Valid { get; set; }
public IList<Subscription> Subscriptions { get; set; }
public bool MailMarketing { get; set; }
public bool PartnerMailMarketing { get; set; }
}
public class PostalAddress
{
public int ID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string City { get; set; }
public string Postcode { get; set; }
public string Region { get; set; }
public string Country { get; set; }
}
If a Customer have X suscriptions, every suscription should have the customerID so you need that in your Suscription class (public int CustomerID {get; set;}).
OTOH, I think that you have to put that Customer reference as virtual and the suscription one (I don't know why).
Maybe Im wrong but that works for me.
Anyway, what's your problem?
Your models look correct. You don't necessarily need the Customer property in the Subscription class, but it does help if you want to retrieve a specific Subscription and then want to find the customer that is tied to that subscription. In that instance you can then do var customer = mySub.Customer instead of querying for a customer with the specific subscription id.

Categories

Resources