In my project i have one viewmodel per domainmodel and reuse this viewmodel in more then one view.
Example of one viewmodel
public class ProductViewModel
{
public int ProductId { get; set; }
public int ProductGroupId { get; set; }
public bool IsLinkedToErp { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "Standard")]
public bool IsDefault { get; set; }
[MaxLength(50)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Artnr")]
public string ArtNo { get; set; }
[MaxLength(255)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Beskrivning")]
public string Description { get; set; }
[MaxLength(255)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Specifikation")]
public string Specification { get; set; }
[MaxLength(5)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Enhet")]
public string Unit { get; set; }
[MaxLength(4)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Konto")]
public string Account { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "Netto")]
public decimal NetPrice { get; set; }
public string ChUser { get; set; }
public DateTime ChTime { get; set; }
public string GetUpdatedDate
{
get { return String.Format("{0:d}", ChTime); }
}
public string GetNetPrice
{
get { return String.Format("{0:0.00}", NetPrice); }
}
}
All my views in ProductController reuse this ViewModel.
For List, Add and Edit. But in list i only use a few of all properties.
Then i using Automapper to map to Domain and vice versa.
Now I'm wondering if anyone has experience of this. Do you think I will have problems using this solution as the project grows?
I would consider splitting out the ListViewModel and the Add/Edit view models if only because you are potentially populating and sending more data than you really need to your view.
By leveraging from inheritance, you could quickly make two classes that contain only the data that you need for your view. Based on your example, I would consider the following:
public class ProductListViewModel{
public IEnumerable<ProductListModel> products {get;set;}
public string SomeOtherPotentialVariableForProductList {get;set;}
}
public class ProductListModel{
public int ProductId { get; set; }
public int ProductGroupId { get; set; }
[MaxLength(255)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Beskrivning")]
public string Description { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "Netto")]
public decimal NetPrice { get; set; }
}
public class ProductViewModel : ProductListModel
{
public bool IsLinkedToErp { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "Standard")]
public bool IsDefault { get; set; }
[MaxLength(50)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Artnr")]
public string ArtNo { get; set; }
[MaxLength(255)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Specifikation")]
public string Specification { get; set; }
[MaxLength(5)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Enhet")]
public string Unit { get; set; }
[MaxLength(4)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Konto")]
public string Account { get; set; }
public string ChUser { get; set; }
public DateTime ChTime { get; set; }
public string GetUpdatedDate
{
get { return String.Format("{0:d}", ChTime); }
}
This accomplishes a few things.
You are only sending the data that is required for the view.
If you find yourself needing more information for the list view that may not be directly related to a product, you can quickly add that property.
By inheriting from the ProductListModel, you are not repeating yourself within your code.
Related
Currently i have the following structure of my classes:
public class StoreElement
{
[Display(Name="ID")]
public int StoreElementId { get; set; }
[Display(Name = "RegalID")]
public string StoreElementCode { get; set; }
[Display(Name = "Regal")]
public string Storage { get; set; }
[Display(Name="Ebene")]
public string Level { get; set; }
[Display(Name = "Fach")]
public string Shelf { get; set; }
[Display(Name = "ESL Tag")]
public string ESLTagId { get; set; }
[Display(Name ="ESL Layout Template")]
public string ESLLayoutValue { get; set; }
[Display(Name = "Eingelagertes Material")]
public List<Material> Materials { get; set; }
}
And this class:
public class Material
{
[Display(Name = "ID")]
public int MaterialId { get; set; }
[Display(Name = "Materialnummer")]
public int? MaterialNumber { get; set; }
[Display(Name = "Auftragsnummer")]
public int? OrderNumber { get; set; }
[Display(Name = "Eingelagert")]
public bool? IsStored { get; set; }
[Display(Name = "Einlagerdatum")]
public DateTime StoredAt { get; set; }
[Display(Name = "Auslagerdatum")]
public DateTime OutsourcedAt { get; set; }
[Display(Name = "Liefertermin")]
public DateTime DeliveryDate { get; set; }
[Display(Name = "Priorisiertes Material")]
public bool PriorityMaterial { get; set; }
public int? StoreElementId { get; set; }
public StoreElement StoreElement { get; set; }
}
A StoreElement can hold a List of Materials. The class Materials contains a property which is a date. Now i would like to order the Storage-Location List by the subproperty date of a list of materials.
I tried something in linq like that:
var myOrderdStorageLocationsByDeliveryDateOfMaterialsSublist = this.MyDatabaseContext.StorageLocations.Include(x=>x.Materials).OrderBy(x=>x.Materials.OrderBy(y=>y.DeliveryDate)).ToList();
But this throws an exception that says "Failed to compare two elements in the array"
Obviously, you need some value from Materials. And OrderBy won't return it.
I would recommend you to get a particular value for sorting StoreElements based on what logic you need: Min, Max or Average for example.
var myOrderdStorageLocationsByDeliveryDateOfMaterialsSublist = this.MyDatabaseContext.StorageLocations
.Include(x=>x.Materials)
.OrderBy(x=>x.Materials.Min(y=>y.DeliveryDate))
.ToList();
I have used this
public List<InspectionReport> GetInspectionReportDetails(int InspectionReportID)
{
var List= this.Database.SqlQuery<InspectionReport>("GetInspectionReportDetails", InspectionReportID).ToList();
return List;
}
in a class that is inherited from DbContext
I am calling this function in a controller but List is empty. Database.SqlQuery returns 0 items even the procedure is returning data for the parameter i am providing to the function but not this.
Update:
alter PROCEDURE GetInspectionReportDetails
#InspectionReportID int=0
AS
BEGIN
Select ir.InspectionReportID
,ir.VelosiProjectNo
,ir.VelosiReportNo
,ir.Reference
,ir.PoNo
,ir.InspectionDate
,ir.IssueDate
,ir.InspectionPhase
,ir.InServiceInspection
,ir.NewInduction
,ir.HydrostaticTest
,ir.DimensionalCheck
,ir.ThicknessCheck
,ir.Patrom
,ir.Gvs
,ir.FinalOgraInspection
,ir.OmcClientRequirement
,ir.TankLorryRegistrationNo
,ir.TruckTractorManufacturerName
,ir.ClientName
,ir.Capacity
,ir.Omc
,ir.EngineNo
,ir.TankLorryDimension
,ir.ChassisNo
,ir.InspectionPlace
,ir.TankLorryEnginePower
,ir.CarriageName
,ir.Brakes
,ir.IsSatisfactory
,ir.Remarks
,ir.Rev
,ir.Description
,ir.Status
,u1.UserName as PeparedBy
,u2.UserName as CheckedBy
,u3.UserName as ApprovedBy
,u4.UserName as IssuedBy
From InspectionReport ir
Inner Join dbo.[User] u1
ON u1.UserID= ir.PeparedBy
Inner Join dbo.[User] u2
ON u2.UserID= ir.CheckedBy
Inner Join dbo.[User] u3
ON u3.UserID= ir.ApprovedBy
Inner Join dbo.[User] u4
ON u4.UserID= ir.IssuedBy
where ir.InspectionReportID= #InspectionReportID
This is the stored procedure that is called by the function.
This the class inpsectionreport;
namespace VAILCertificates.DAL.Entities
{
public class InspectionReport
{
//General Details
public int InspectionReportID { get; set; }
[Required]
[Display (Name= "Velosi Project No")]
public string VelosiProjectNo { get; set; }
[Display(Name = "Velosi Report No")]
public string VelosiReportNo { get; set; }
[Required]
public string Reference { get; set; }
[Required]
[Display(Name = "PO No")]
public string PoNo { get; set; }
[Required]
[Display(Name = "Inspection Date")]
public DateTime InspectionDate { get; set; }
[Required]
[Display(Name = "Issue Date")]
public DateTime IssueDate { get; set; }
//Inspection Phase
[Required]
[Display(Name = "Inspection Phase")]
public byte InspectionPhase { get; set; } //0= Before, 1= During, 2= Final
//Types Of Inspection
[Display(Name = "In Service Inspection")]
public bool InServiceInspection { get; set; }
[Display(Name = "New Induction")]
public bool NewInduction { get; set; }
[Display(Name = "Hydorstatic Test")]
public bool HydrostaticTest { get; set; }
[Display(Name = "Dimensional Check")]
public bool DimensionalCheck { get; set; }
[Display(Name = "Thickness Check")]
public bool ThicknessCheck { get; set; }
[Display(Name = "PATROM")]
public bool Patrom { get; set; }
[Display(Name = "GVS")]
public bool Gvs { get; set; }
[Display(Name = "Final OGRA Inspection")]
public bool FinalOgraInspection { get; set; }
[Display(Name = "OMC/Client Requirement")]
public bool OmcClientRequirement { get; set; }
//Details Of Tank Lorry
[Required]
[Display(Name = "Tank Lorry Registration Number")]
public string TankLorryRegistrationNo { get; set; }
[Required]
[Display(Name = "Truck Tractor Manufacturer Name")]
public string TruckTractorManufacturerName { get; set; }
[Required]
[Display(Name = "Client Name")]
public string ClientName { get; set; }
[Required]
[Display(Name = "Capacity")]
public string Capacity { get; set; }
[Required]
[Display(Name = "OMC")]
public string Omc { get; set; }
[Required]
[Display(Name = "Engine No")]
public string EngineNo { get; set; }
[Required]
[Display(Name = "Tank Lorry Dimension")]
public string TankLorryDimension { get; set; }
[Required]
[Display(Name = "Chassis Number")]
public string ChassisNo { get; set; }
[Required]
[Display(Name = "Inspection Place")]
public string InspectionPlace { get; set; }
[Required]
[Display(Name = "Tank Lorry (Engine Power)")]
public string TankLorryEnginePower { get; set; }
[Required]
[Display(Name = "Carriage Name")]
public string CarriageName { get; set; }
[Required]
public string Brakes { get; set; }
//ResultsofInspection
[Required]
[Display(Name = "Is Satisfactory?")]
public bool IsSatisfactory { get; set; }
//Remarks
[Required]
public string Remarks { get; set; }
//ApprovalSection
public string Rev { get; set; }
[Required]
public string Description { get; set; }
[Required]
public int PeparedBy { get; set; }
public int CheckedBy { get; set; }
public int ApprovedBy { get; set; }
public int IssuedBy { get; set; }
//ReportStatus
public byte Status { get; set; } //0= Prepared 1= CheckedBy 2= ApprovedBy
}
}
You can also construct a DbParameter and supply it as a parameter value to your stored procedure:
var InspectionReportID= new SqlParameter("#InspectionReportID", 0);
var list = this.Database.SqlQuery<InspectionReport>("EXECUTE dbo.GetInspectionReportDetails #InspectionReportID",InspectionReportID).ToList();
It is important to parameterize any user input to protect against a SQL injection attack. You can include parameter placeholders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter.
Try changing the the sql statement, I feel the error is how you call the sql statement.
var list = this.Database.SqlQuery<InspectionReport>($"EXEC GetInspectionReportDetails {InspectionReportID}").ToList();
Try using this code:
public List<InspectionReport> GetInspectionReportDetails(int InspectionReportID)
{
var List= this.Database.SqlQuery<InspectionReport>("GetInspectionReportDetails #InspectionReportID", InspectionReportID).ToList();
return List;
}
I have a ViewModel with several properties (VM for multi-step wizard):
public class CallViewModel
{
[Key]
[Column(Order = 0)]
[HiddenInput(DisplayValue = false)]
public System.Guid CallID { get; set; }
[Required, Display(Name = "Call Date")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MM yyyy} г.")]
public System.DateTime CallDate { get; set; }
[Required, Display(Name = "Contract Number")]
public string Number { get; set; }
[Display(Name = "Date of Sampling"), DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MM yyyy} г.")]
public System.DateTime SampleActDate { get; set; }
[Display(Name = "Date of the Contract"), DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MM yyyy} г.")]
public System.DateTime ContractDate { get; set; }
[Required, Display(Name = "Cost Efficiency, %")]
public decimal CostEfficiency { get; set; }
[Required, Display(Name = "Document Type")]
public string CallName { get; set; }
//Representative
[Required, Display(Name = "Second Name")]
public string RepFamilyName { get; set; }
[Required, Display(Name = "First Name")]
public string RepFirstName { get; set; }
[Required, Display(Name = "Middle Name")]
public string RepMidName { get; set; }
[Required, Display(Name = "Position")]
public string RepPosition { get; set; }
[Required, Display(Name = "Document")]
public string DocType { get; set; }
[Required, Display(Name = "Phone (###) ###-##-##")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:(###) ###-##-##}")]
public string RepPhoneNumber { get; set; }
//Customer
[Required, Display(Name = "Judicial Status")]
public string JudicialStatus { get; set; }
[Required, Display(Name = "Customer Name")]
public string CustomerName { get; set; }
[Required, Display(Name = "Address")]
public string CustomerAddress { get; set; }
[Required, Display(Name = "TaxID")]
public string TaxID { get; set; }
[Display(Name = "BIC")]
public string BIC { get; set; }
[Required, Display(Name = "PHone Number (###) ###-##-##")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:(###) ###-##-##}")]
public string CustomerPhoneNumber { get; set; }
[Required, Display(Name = "OKPO")]
public int OKPO { get; set; }
[Required, Display(Name = "Account)]
public string CustomerBankAccount { get; set; }
[Required, Display(Name = "Bank Branch")]
public string BankBranch { get; set; }
[Required, Display(Name = "Bank Address")]
public string BranchAddress { get; set; }
[Display(Name = "Bank Code")]
public Nullable<int> BankCode { get; set; }
[Display(Name = "Cell Phone Number")]
public string MPhoneNumber { get; set; }
//Person
[Required, Display(Name = "Expert")]
//public string Exp { get; set; }
public Guid ExpID { get; set; }
[Required, Display(Name = "Affidavit Number")]
public int AffidavitNum { get; set; }
[Required, Display(Name = "Affidavit Date"),
DataType(DataType.Date)]
public System.DateTime AffidavitDate { get; set; }
public List<ItemClass> ItemsList { get { return _items; } }
private List<ItemClass> _items = new List<ItemClass>();
public class ItemClass
{ //Item
public Guid ItemID { get; set; }
[Required, Display(Name = "Item SubType")]
public Guid ItemSubtype { get; set; }
[Required, Display(Name = "Item Name")]
public string ItemName { get; set; }
[Required, Display(Name = "Producer")]
public string ItemProducer { get; set; }
[Required, Display(Name = "Quantity")]
public int ItemQty { get; set; }
[Display(Name = "Additionals")]
public string Additional { get; set; }
[Required, Display(Name = "Program Name")]
public string ProgramNameShort { get; set; }
[Required, Display(Name = "Calc Date")]
public string calcDate { get; set; }
[Required, Display(Name = "Calc Number")]
public string calcNum { get; set; }
}
}
I also have several entities with 1:n relationships, like
public partial class Call
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Call()
{
this.CallDetails = new HashSet<CallDetail>();
}
public System.Guid CallID { get; set; }
public System.DateTime CallDate { get; set; }
public System.Guid CustomerID { get; set; }
public string DocNumber { get; set; }
public int AffidavitNum { get; set; }
public System.DateTime AffidavitDate { get; set; }
public System.DateTime ContractDate { get; set; }
public int CallStatus { get; set; }
public string DocType { get; set; }
public Nullable<decimal> Cost_Efficiency { get; set; }
public System.DateTime SampleActDate { get; set; }
public string CallName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<CallDetail> CallDetails { get; set; }
public virtual Customer Customer { get; set; }
}
and
public partial class CallDetail
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public CallDetail()
{
this.Orders = new HashSet<Order>();
}
public System.Guid CallID { get; set; }
public System.Guid ItemID { get; set; }
public int ItemQty { get; set; }
public decimal ItemTestCost { get; set; }
public System.Guid ProgramID { get; set; }
public virtual Call Call { get; set; }
public virtual Item Item { get; set; }
public virtual Program Program { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Order> Orders { get; set; }
}
to name a few.
In [HttpPost] public ActionResult Create (CallViewModel callViewModel) method I should map the ViewModel to those entities. I know that Automapper is among the best ways to perform it, but still I need to understand the principles of correct mapping of VM and related entities (Automapper examples are welcome as well :) ), and especially how to deal with navigation properties (I'm mostly worried about ID properties). Could you please show the best (or template) practice to perform it? Please, be as detailed as possible.
Thanks in advance.
Take this example:
public class ModelClass
{
public Guid Id { get; set; }
public ChildModel ChildModel { get; set; }
}
public class ViewModelClass
{
public Guid Id { get; set; }
public ChildModel ChildModel { get; set; } = new ChildModel();
}
public class ChildModel
{
public Guid Id { get; set; }
}
If you want to map your viewmodel to your model you can do:
[HttpPost]
public ActionResult Create (CallViewModel callViewModel) {
var model = new ModelClass;
model.Id = callViewModel.Id;
model.ChildModel = callViewModel.ChildModel;
_context.Add(model);
_context.SaveChanges();
return RedirectToAction("Index");
}
For the use of automapper I suggest you read the AutoMapper docs :)
EF creates the Item class, I then create a partial Item class with a metadataType, ItemMD.
In certain cases, I want to use the UIHint defined in ItemMD, but in others, I want to override the UIHint to use another editor. So I try creating a derived class that inherits ItemMD....but I don't think this is how you are supposed to do it. It compiles, but I get inconsistent behaviour....when I have 5 fields in my Razor form, 4 are using the base ItemMD UIHint and 1 is using the derived class UIHint. Not sure why the inconsistency.
[MetadataType(typeof(ItemMD))]
public partial class Item : AuditStamps, IEntity, IAuditStamps
{
}
public partial class ItemMD
{
public int Id { get; set; }
[Display(Name = "Company Id")]
public int CompanyId { get; set; }
public string Description { get; set; }
[Display(Name = "Short Description")]
public string ShortDescription { get; set; }
[Display(Name = "Type")]
[UIHint("ItemAtrributesComboBox")]
public virtual string Attribute1 { get; set; }
[Display(Name = "Color")]
[UIHint("ItemAtrributesComboBox")]
public virtual string Attribute2 { get; set; }
[Display(Name = "Finish")]
[UIHint("ItemAtrributesComboBox")]
public virtual string Attribute3 { get; set; }
[Display(Name = "Texture")]
[UIHint("ItemAtrributesComboBox")]
public virtual string Attribute4 { get; set; }
[Display(Name = "Gauge")]
[UIHint("ItemAtrributesComboBox")]
public virtual string Attribute5 { get; set; }
public class ItemSearchFiltersViewModel : OTIS.domain.InventoryMgmt.Item.ItemMD
{
[Display(Name = "Type:")]
[UIHint("ItemAttributesDDL")]
public override string Attribute1 { get; set; }
[Display(Name = "Color:")]
[UIHint("ItemAttributesDDL")]
public override string Attribute2 { get; set; }
[Display(Name = "Finish:")]
[UIHint("ItemAttributesDDL")]
public override string Attribute3 { get; set; }
[Display(Name = "Texture:")]
[UIHint("ItemAttributesDDL")]
public override string Attribute4 { get; set; }
[Display(Name = "Gauge:")]
[UIHint("ItemAttributesDDL")]
public override string Attribute5 { get; set; }
}
I have a class called Status, and I also have a class called ApplicantPositionHistory which has an OldStatus and a NewStatus.
However the table is being generated like this:
I would expect that the table has a newstatusid and oldstatusid, which should be foreign keys, but it generated those 2 columns duplicated.
public class ApplicationPositionHistory
{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
public int ApplicationPositionHistoryID { get; set; }
public ApplicantPosition applicantPosition { get; set; }
[Column("oldStatusID")]
public int oldStatusID { get; set; }
[Column("newStatusID")]
public int newStatusID { get; set; }
public Status oldStatus { get; set; }
public Status newStatus { get; set; }
[StringLength(500, MinimumLength = 3, ErrorMessage = "Comments should not be longer than 500 characters.")]
[Display(Name = "Comments")]
public string comments { get; set; }
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
[Display(Name = "Date")]
public DateTime dateModified { get; set; }
}
public class Status
{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
public int StatusID { get; set; }
[StringLength(40, MinimumLength = 3, ErrorMessage = "Status should not be longer than 20 characters.")]
[Display(Name = "Status")]
public string status { get; set; }
}
I think the problem is your non standard naming convention which results in problem when applying default mapping conventions so your FK columns are not paired with navigation properties and EF creates new ones.
Try this to manually pair navigation properties with your FK properties:
[ForeignKey("oldStatusID")]
public Status oldStatus { get; set; }
[ForeignKey("newStatusID")]
public Status newStatus { get; set; }