Club the UI model and DB model in C# - 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 :)

Related

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;}
}

Reusing models in Web Api

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; }
}

Combining Entities into ViewModels and using Entity Framework

Most tutorials don't really cover this at all. They just say link your entity to a controller and you're done.
In my business model I have Customers and I have Customer Contacts 1 Customer to >1 Customer Contacts. How do I create a view model for these that will allow them to be edited/created/whatever from the same view?
public class Customer
{
public Customer()
{
this.CustomerContacts = new List<CustomerContact>();
this.Systems = new List<System>();
this.CreatedByCustomerTickets = new List<Ticket>();
this.CustomerTickets = new List<Ticket>();
}
public long CustomerID { get; set; }
public Nullable<bool> BusinessCustomer { get; set; }
public string CustomerName { get; set; }
public string CustomerNotes { get; set; }
public virtual ICollection<CustomerContact> CustomerContacts { get; set; }
public virtual ICollection<System> Systems { get; set; }
public virtual ICollection<Ticket> CreatedByCustomerTickets { get; set; }
public virtual ICollection<Ticket> CustomerTickets { get; set; }
}
public class CustomerContact
{
public long CustomerContactID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Phone { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public Nullable<int> Zip { get; set; }
public Nullable<long> CustomerID { get; set; }
public string Email { get; set; }
public bool PromotionalEmails { get; set; }
public virtual Customer Customer { get; set; }
}
Well I'd start with this
public class CustomerViewModel
{
public Customer Customer {get; set;}
public CustomerContact CustomerContact {get; set;}
}
and work from there.
If you don't need all the properties from the domain objects, you may consider something more like:
public class CustomerViewModel
{
public long CustomerID { get; set; }
public ICollection<CustomerContact> CustomerContacts { get; set; }
}
It's really up to you to construct your view models in a way that will meet the needs of your specific project.

Automapper linker table

I'm trying to map two entities connected via linker table.
OrganizationVM = Organization -|---< AddressLinker >---|- Address
one to many many to one
My goal is to get Organization ViewModel with State property from Address entity.
string state = OrganizationVM.State
Does anyone know how do I do it?
Thanks!
public class OrganizationVM : BaseViewModel
{
[Display(Name = "Parent ID")]
[Required]
public int? ParentID { get; set; }
public string ParentInternalName { get; set; }
public string State { get; set; }
public string StateAbr { get; set; }
public string City { get; set; }
}
public class AddressLinker
{
public int ID {get; set;}
public int OrganizationID { get; set; }
public int AddressID { get; set; }
}
public class Address
{
public int ID {get; set;}
public string State { get; set; }
public string Code { get; set; }
public string City { get; set; }
}
public class Organization
{
public int ID {get; set;}
public int? ParentID { get; set; }
public string ParentInternalName { get; set; }
public bool StructuralNode { get; set; }
public string InternalName { get; set; }
public string ExternalShortName { get; set; }
public string ExternalFullName { get; set; }
public string State { get; set; }
public string Code { get; set; }
public string City { get; set; }
}

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