Best practices creating data models with same properties - c#

New to MVC and looking for best practice when multiple models have the same properties. For example I have several models that have the address fields (address, city, state and zipcode). Let's say I have a customer model and supplier model with these fields. What is the best way to create the model? First example I started with has:
public class Customer
{
public int Id { get; set; }
public string AccountNumber { get; set; }
public string Customer { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string MainPhone { get; set; }
public string fax { get; set; }
public string ContactPhone { get; set; }
}
public class Supplier
{
public int Id { get; set; }
public string AccountNumber { get; set; }
public string Supplier { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string MainPhone { get; set; }
public string fax { get; set; }
public string ContactPhone { get; set; }
}
or alternately:
public class Customer
{
public int Id { get; set; }
public string AccountNumber { get; set; }
public string Customer { get; set; }
public Address CustomerAddress { get; set; }
public Phone MainPhone { get; set; }
public Phone fax { get; set; }
public Phone ContactPhone { get; set; }
}
public class Supplier
{
public int Id { get; set; }
public string AccountNumber { get; set; }
public string Supplier { get; set; }
public Address SupplierAddress { get; set; }
public Phone MainPhone { get; set; }
public Phone fax { get; set; }
public Phone ContactPhone { get; set; }
}
public class Address
{
public string AddressLine1 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
public class Phone
{
public int AreaCode { get; set; }
public int NPA { get; set; }
public int Station { get; set; }
}
Any suggestions on what is considered best practice is very much appreciated

I don't know that this is any different for MVC than for anything else, but IMO, the second is far better. When you decide to add a work address and home address, just like you already have a few phone numbers, you won't have to come up with even more contrived names to differentiate. All the standard reasons for reusable code would still apply.

Related

AutoMapperMappingException: Missing type map configuration or unsupported mapping

I'm attempting to use AutoMapper for the first time. The GSMSite maps fine, but I get an error when trying to map the GSMUnit:
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Do I need to specify a relationship between GSMSite and GSMUnits?
Domain class:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Company { get; set; }
public string Telephone { get; set; }
public bool PasswordReset { get; set; } = false;
public virtual ICollection<GSMSite> GSMSites { get; set; }
}
public class GSMSite
{
public int Id { get; set; }
public string SiteName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Town { get; set; }
public string Postcode { get; set; }
public string ContactName { get; set; }
public string ContactNumber { get; set; }
public string ContactEmail { get; set; }
public string ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual ICollection<GSMUnit> GSMUnits { get; set; }
}
public class GSMUnit
{
public int Id { get; set; }
public string Model { get; set; }
public string Firmware { get; set; }
public string TelephoneNum { get; set; }
public int GSMSiteId { get; set; }
public virtual GSMSite GSMSite { get; set; }
}
Contract class:
public class GSMResponse
{
public int Id { get; set; }
public string Model { get; set; }
public string Firmware { get; set; }
public string TelephoneNum { get; set; }
}
public class SiteResponse
{
public int Id { get; set; }
public string SiteName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Town { get; set; }
public string Postcode { get; set; }
public string ContactName { get; set; }
public string ContactNumber { get; set; }
public string ContactEmail { get; set; }
}
Mapping:
public DomainToResponseProfile()
{
CreateMap<GSMSite, SiteResponse>();
CreateMap<GSMUnit, GSMResponse>();
}

EF core 2.0, OwnsOne in TPH model classes

I have problem when I try to migrate my model in EF Core 2.0.
public class Profile
{
[Key]
public Guid Id { get; set; }
public Guid UserId { get; set; }
public ExternalUser User { get; set; }
}
public class OrganizationCustomerProfile : Profile
{
public string CompanyName { get; set; }
public Address LegalAddress { get; set; }
public Address ActualAddress { get; set; }
public BusinessRequisites Requisites { get; set; }
public string President { get; set; }
public IEnumerable<ContactPerson> ContactPerson { get; set; }
}
public class PersonCustomerProfile : Profile
{
public FullName Person { get; set; }
public Address Address { get; set; }
public string PhoneNumber { get; set; }
}
public class ContactPerson
{
[Key]
public Guid Id { get; set; }
public FullName Person { get; set; }
public string Rank { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public Guid ProfileId { get; set; }
public Profile Profile { get; set; }
}
Here I want to add complex datatypes Address and BusinessRequisites, which are:
public class BusinessRequisites
{
public string OGRN { get; set; }
public string INN { get; set; }
public string KPPCode { get; set; }
public string SettlementAccount { get; set; }
public string RCBIC { get; set; }
public string CorrespondentAccount { get; set; }
public string BankName { get; set; }
}
public class Address
{
public string FullAddress { get; set; }
public float Latitude { get; set; }
public float Longtitude { get; set; }
}
Code which I use for TPH binding:
public DbSet<Profile> UserProfiles { get; set; }
public DbSet<ContactPerson> ContactPerson { get; set; }
public DbSet<OrganizationCustomerProfile> OrganizationCustomerProfile { get; set; }
...
modelBuilder.Entity<Profile>().HasKey(u => u.Id);
modelBuilder.Entity<OrganizationCustomerProfile>().OwnsOne(e => e.ActualAddress);
modelBuilder.Entity<OrganizationCustomerProfile>().OwnsOne(e => e.LegalAddress);
modelBuilder.Entity<OrganizationCustomerProfile>().OwnsOne(e => e.Requisites);
But when I try to make a migration, I get an error:
"Cannot use table 'UserProfiles' for entity type
'OrganizationCustomerProfile.ActualAddress#Address' since it has a
relationship to a derived entity type 'OrganizationCustomerProfile'.
Either point the relationship to the base type 'Profile' or map
'OrganizationCustomerProfile.ActualAddress#Address' to a different
table."
So, what the reason of this error? Is it not possible to create hierarchy inheritance in EF Core 2.0?
Thank you!
It seems like this isn't supported at the moment:
https://github.com/aspnet/EntityFrameworkCore/issues/9888

Convert in ObservableCollection (C# uwp)

I am writing an app for UWP.
I tried to use data binding according to this answer link.
Here are my classes:
public class Billing
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postcode { get; set; }
public string country { get; set; }
public string email { get; set; }
public string phone { get; set; }
}
public class Shipping
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postcode { get; set; }
public string country { get; set; }
}
public class RootObject
{
public int id { get; set; }
public int parent_id { get; set; }
public string status { get; set; }
public string order_key { get; set; }
public string currency { get; set; }
public string version { get; set; }
public bool prices_include_tax { get; set; }
public string date_created { get; set; }
public string date_modified { get; set; }
public int customer_id { get; set; }
public double discount_total { get; set; }
public double discount_tax { get; set; }
public double shipping_total { get; set; }
public double shipping_tax { get; set; }
public double cart_tax { get; set; }
public double total { get; set; }
public double total_tax { get; set; }
public Billing billing { get; set; }
public Shipping shipping { get; set; }
public string payment_method { get; set; }
public string payment_method_title { get; set; }
public string transaction_id { get; set; }
public string customer_ip_address { get; set; }
public string customer_user_agent { get; set; }
public string created_via { get; set; }
public string customer_note { get; set; }
public string date_completed { get; set; }
public string date_paid { get; set; }
public string cart_hash { get; set; }
public List<object> line_items { get; set; }
public List<object> tax_lines { get; set; }
public List<object> shipping_lines { get; set; }
public List<object> fee_lines { get; set; }
public List<object> coupon_lines { get; set; }
}
public ObservableCollection<RootObject> Orders { get; set; }
Here is the code:
List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(products);
foreach (RootObject root in rootObjectData)
{
string date = root.date_created;
string name = root.billing.first_name + root.billing.last_name ;
Orders = new ObservableCollection<RootObject> { new RootObject { date_created = date,billing = name } };
}
With billing = name I have this error: Cannot implicitly convert type 'string' to 'Milano.InWork.Billing'
How can I fix this error?
Maybe this is simple, but I don't find a solution.
Thanks for the help!!
With billing = name I have this error: Cannot implicitly convert type 'string' to 'Milano.InWork.Billing'
Of course this error will occur, your billing in the class RootObject has the data type Billing, which is another class you defined.
Just from your code I think you only want to show the first_name and last_name as string in your root.billing, then you can change the code public Billing billing { get; set; } in your RootObject class to public string billing { get; set; }.

How to setup a relational data model?

I am completely new to ASP.NET and was trying to cross-reference two different models with each other. I have 'Customer':
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
and 'Reservation':
public class Reservation
{
public int ReservationID { get; set; }
public string PetName { get; set; }
public DateTime Birthdate { get; set; }
public string Specie { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public Customer Customer { get; set; }
}
Now, each Reservation should belong to a Customer (as you can see in the bottom line of the Reservation model) and should therefore contain the CustomerID. On the other hand should each Customer contain references to each Reservation that was made by him.
How can I setup this relation?
Use an ICollection for a one to many relationship.
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public ICollection<Reservation> Reservations { get; set; }
}
Note I originally suggested a generic list, but there's a few subtle differences.

Instantiate one class variable with another

I have a the following classes
namespace MyClass.Models
{
public class Address : IIdentity
{
public string id { get; set; }
public DateTime updatedAt { get; set; }
public string user_id { get; set; }
public int building_number { get; set; }
public string street { get; set; }
public string area { get; set; }
public string county { get; set; }
public string country { get; set; }
public string postcode { get; set; }
public bool iscustomer { get; set; }
public bool islive { get; set; }
public string customer_id { get; set; }
public string store_id { get; set; }
public string phone_id { get; set; }
}
}
and
namespace MyClass.Models
{
public class User : IIdentity
{
public string id { get; set; }
public DateTime updatedAt { get; set; }
public string username { get; set; }
public string password { get; set; }
public bool isrestricted { get; set; }
public bool isbanned { get; set; }
public DateTime renewaldate { get; set; }
public string address_id { get; set; }
public string userlevel { get; set; }
}
}
What I am trying to do is create some test data to propagate through to my database so I can test my MVC5 website.
I am attempting to perform a look up on my instance of User within the Address class like so
user_id = context.User.FirstOrDefault(t=>t.address_id == id)
where id points to the id within the class I and trying instantiate.
VS2013 is saying that id does not exist in the current context which makes sense. Is there a way to do what I'm trying to achieve or am I better off setting the my _id from within the class itself and will that be okay when I run update-database from within the package manager console?
For example, the address class would become
namespace MyClass.Models
{
public class Address : IIdentity
{
public string id { get; set; }
public DateTime updatedAt { get; set; }
public string user_id { get {user_id = Users.FirstOrDefault(t=>t.address_id == id).id; } }
public int building_number { get; set; }
public string street { get; set; }
public string area { get; set; }
public string county { get; set; }
public string country { get; set; }
public string postcode { get; set; }
public bool iscustomer { get; set; }
public bool islive { get; set; }
public string customer_id { get {customer_id = Customers...;} }
public string store_id { get {store_id = Stores...;} }
public string phone_id { get {phone_id = Phones...;} }
}
}

Categories