I want to make a class that I will use as a model in a strongly typed view in an application:

public class MaterialAssetDetails { public int Id {get;set;} public string ItemNumber {get;set;} public string Name {get;set;} public string MeasureUnit {get;set;} public decimal Amount {get;set;} public decimal Price {get;set;} public MaterialAssetDetails(int id, string itemNumber, string Name, string measureUnit, decimal amount, decimal price) { this.Id = id; this.ItemNumber=itemNumber; //и так далее } } 

The purpose of this : displaying detailed information about the tangible asset .

What I don't like: the number of parameters that need to be passed to the constructor (and these are not all the properties) for creating an object instance.

Tell me how do you do with similar classes?

PS: it only occurs to me to create an overloaded version of the constructor without parameters amount , price :

 public MaterialAssetDetails(int id, string itemNumber, string name, string measureUnit) :this(id, itemNumber, name, measureUnit, 0,0) { } 
  • pageObject a template do not consider? - Senior Pomidor
  • @SeniorAutomator can answer in slightly more detail (I have not heard of such a pattern / pattern), if not difficult with a specific example. thanks in advance - Bald
  • And if you just remove the constructor? .. - Pavel Mayorov
  • @PavelMayorov I currently have not defined a constructor, i.e. My code corresponds to the method proposed by MarkShevcenko, but I don’t like it that you have to remember the properties you need to fill out - Bald

2 answers 2

 public class MaterialAssetDetails { public int Id {get;set;} public string ItemNumber {get;set;} public string Name {get;set;} public string MeasureUnit {get;set;} public decimal Amount {get;set;} public decimal Price {get;set;} public MaterialAssetDetails(int id, string itemNumber) { this.Id = id; this.ItemNumber=itemNumber; //и так далее } public MaterialAssetDetails withName(string name){ this.Name = name; return this; } public MaterialAssetDetails withName(string MeasureUnit){ this.MeasureUnit = MeasureUnit; return this; } public MaterialAssetDetails withAmount(decimal Amount){ this.Amount = Amount; return this; } public MaterialAssetDetails withPrice(decimal Price){ this.Price = Price; return this; } static int Main(string[] args) { MaterialAssetDetails details = new MaterialAssetDetails(0, "0") .withAmount(0) .withName("name") .withPrice(0); } } 

wiki and another article

In general, this template is used for other purposes, but if you do not want many parameters to pass to the constructor

    Since you have all the fields not only getters, but also setters, you can completely abandon the constructor. Starting with C # version 4, you can initialize classes like this:

     public class MaterialAssetDetails { public int Id {get;set;} public string ItemNumber {get;set;} public string Name {get;set;} public string MeasureUnit {get;set;} public decimal Amount {get;set;} public decimal Price {get;set;} } . . . var details = new MaterialAssetDetails { Id = model.Id, ItemNumber = model.ItemNumber, Name = model.Name, MeasureUnit = model.MeasuteUnit, }; 

    In essence, this is syntactic sugar: the compiler first creates an empty instance of the MaterialAssetDetails class, and then assigns the specified values ​​to the specified properties.

    Alternative: use default values ​​in the constructor:

     public MaterialAssetDetails(int id, string itemNumber, string Name, string measureUnit, decimal amount = 0m, decimal price = 0m) { . . . } 

    In this case, you too can initialize only those fields that are necessary, filling the rest only if necessary.

    Finally, if the set of models and their associated representations is large enough, you can use something like the AutoMapper library, which allows you to copy properties with the same types and names.

    • I have a method that you have proposed at the moment, I don’t like it that when creating an object instance I have to remember which properties need to be filled in - Bald
    • You can combine, that is, fill in the required fields through the constructor, but not mandatory through initialization in curly brackets. However, I don’t know your project, so choose what method is better. - Mark Shevchenko