0

I need some help. I have a model class named Transaction and I want to calculate JumlahTransaksi with PersenTopUp, where should I put the function to calculate that? In the Model or in the Controller?

public class Transaksi
{
    public Transaksi()
    {
        Members = new Member();
        Groups = new Group();
        Profits = new Profit();
        Level = new Level();
        JenisTransaksi = new JenisTransaksi();
    }

    [BsonId]
    public string Id { get; set; }

    public Member Members { get; set; }

    public Group Groups { get; set; }

    public int NoTransaksi { get; set; }

    [BsonRepresentation(MongoDB.Bson.BsonType.Double)]
    public double BatasTransaksi { get; set; }

    [BsonRepresentation(BsonType.String)]
    public JenisTransaksi JenisTransaksi { get; set; }

    [BsonRepresentation(BsonType.Double)]
    public double PersenTopUp
    {
        get;
        set;
    }
    [BsonRepresentation(BsonType.Double)]
    public double PersenRef
    {
        get;
        set;
    }

    [BsonRepresentation(BsonType.Double)]
    public double JumlahTransaksi { get; set; }

    [BsonDateTimeOptions(Kind = DateTimeKind.Local, DateOnly = true)]
    public DateTime TanggalTransaksi { get; set; }

    [BsonDateTimeOptions(Kind = DateTimeKind.Local, DateOnly = true)]
    public DateTime TglJatuhTempo
    {
        get
        {
            return (this.TanggalTransaksi.AddYears(1));
        }
    }

    // Setiap member memiliki list profit 
    public Profit Profits { get; set; }

    [BsonRepresentation(BsonType.String)]
    public Level Level { get; set; }

    [BsonRepresentation(BsonType.String)]
    public StatusTransaksi StatusTransaksi;

    [BsonRepresentation(MongoDB.Bson.BsonType.String)]
    public string Keterangan { get; set; }

    [BsonDateTimeOptions(Kind = DateTimeKind.Local, DateOnly = true)]
    public DateTime TerakhirDiubah { get; set; }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
  • Your question is opinion based, have a look at [Rich vs Anemic domain model](https://stackoverflow.com/questions/23314330/rich-vs-anemic-domain-model). DDD favors rich domain models (which means behavior should be in the model), see [Martin Fowler's post](https://www.martinfowler.com/bliki/AnemicDomainModel.html) on the topic. – Hooman Bahreini Mar 24 '19 at 23:37
  • Possible duplicate of [Rich vs Anemic Domain Model](https://stackoverflow.com/questions/23314330/rich-vs-anemic-domain-model) – Hooman Bahreini Mar 24 '19 at 23:38

1 Answers1

0

You can define in your model a function to calculate JumlahTransaksi with PersenTopUp.

public double MakeSum()
{
   return PersenTopUp + JumlahTransaksi;
}
  • and how i call it in controller ?? or using razor ?? – Edward Davinci Apr 10 '16 at 17:35
  • I think in the controller you will make a new instance of Transaksi class. Then you can save the result of MakeSum function in a variable and then to pass this result to the ViewBag object. You have the ViewBag object in the Model, so you can use it :). – Vasilut Lucian Apr 10 '16 at 18:21
  • oke thanks a loot lucian , can it implemented using repository pattern ?? – Edward Davinci Apr 10 '16 at 18:26