How to assign a class to the whole and not just attributes separately?

Asked

Viewed 97 times

1

I have a class constructor method that when called makes a query in another database class that returns me the same constructor method class(only with the assigned data), example:

public Invoice(int id)
{
   CallDB db = new CallDB();
   Invoice invoice = db.ReturnInvoice(id);
   this.Filial = invoice.Filial;
   this.UserProfileModel = invoice.UserProfileModel;
   this.DataEmissao = invoice.DataEmissao;
   this.DataVencimento = invoice.DataVencimento;
   this.ID = invoice.ID;
}

I wouldn’t want to be using the this "300 thousand" times, it would be possible to do something like this?

public Invoice(int id)
{
   CallDB db = new CallDB();
   Invoice invoice = db.ReturnInvoice(id);
   this.Invoice = invoice ; //estou atribuindo a classe como um todo ao invés de cada atributo separadamente
}

2 answers

2


There’s no way in normal ways.

And it looks like there’s a lot of things wrong there, starting with this class that doesn’t seem to be what she describes. It’s even hard to steer in the right direction because you’re on all the wrong grounds.

Of course you can do the ReturnInvoice() already return the mounted object, but then you will be doing the same thing you already do in the original constructor, only in another method. And worse, in the wrong method that should only deal with the database.

There is a solution with reflection, but it is not usually recommended. It seems to me to be just to reduce typing and is not a good use for this. And if you need this maybe C# isn’t the right language for this solution.

With reflection I could read the structure of objects and make a loop that takes the dice return database and assign in class members Invoice.

In some cases it may facilitate reflection if you use certain conventions, but it makes it difficult to make the system that cannot escape from them, does not compensate.

You can also use a Dictionary or a ExpandoObject, but again, then use a dynamic language.

In C# I prefer a solution of scaffolding (in C# 9 this is even easier) or do manually.

A restructuring of the general architecture can give a better solution.

  • As the other colleague said, the organization is wrong right... I will give a studied in their quoted answers (which by the way are always FODAS) and I will apply the correct, thank you.

1

In this scenario, you would have to call the object that way:

Invoice obj = new Invoice(1);

correct ?

I suggest you create a static method that returns the object:

public class Invoice
{
   public Invoice()
   {
     //...construtor padrão
   }

   public static Invoice SelectById(int id)
   {
       CallDB db = new CallDB();
       return db.ReturnInvoice(id);
   }
}

And then I’d make the call:

Invoice obj = Invoice.SelectById(1);

however... I suggest to review the structure of your code, because within the class you are having access to the data layer, this is generating a high coupling and low cohesion which is not certain and will make the code more complex and difficult to maintain.

  • Rovann it is wrong that I have this access to data within the class?

  • I use MVC, I would do this data access inside the controller?

  • yes, always in the controller

Browser other questions tagged

You are not signed in. Login or sign up in order to post.