覆盖C#中对象的默认返回

时间:2019-01-22 10:55:32

标签: c# .net asp.net-core .net-core

关于对象的返回,我想确保在if返回之前就执行了操作。有没有办法重写对象的默认返回值来执行该操作?

例如

//In the constructor I set a query start time
var items = new ListReturnDTO<Product>();

    ....

//I want to set a query end time, but without the need to set the variable in code as it could be forgotten. 
return items;

编辑:

      //I set the QueryStartTime in the constructor
      var items = new ListReturnDTO<Product>();

            items.TotalItem = 11;

            ........

            items.data.Add(new Product { SKU = "000006", Description = "this is the test product 7, a rare breed of product", Price = 65.00, QuantityLeft = 3, Title = "Test Product 7", IsPreview = false });
            items.data.Add(new Product { SKU = "000007", Description = "this is the test product 8, a rare breed of product", Price = 7.00, QuantityLeft = 30, Title = "Test Product 8", IsPreview = false });

            //force a delay to replicate talking to external source
            Thread.Sleep(2000);

            //Currently i set QueryEndTime Here
            items.QueryEndTime = DateTime.UtcNow.TimeOfDay;

            //But i want it to be done automatically on the return (like how I can set QueryStartTime in the constructor, is there an equivalent/an override for the return)
            return Task.FromResult(items);

2 个答案:

答案 0 :(得分:3)

根据我对问题的理解

  

在方法末尾自动调用一些代码。或者更多   特别是如果方法的返回类型不为空,则执行一些   行动。因此,在给定的示例中,它应该更新QueryEndTime。

这个概念好像是aspect oriented programming。您可能想尝试的一种库可能是Postsharp。其他的也很少。

Postsharp具有method decorator,可以在方法执行之前和之后注入行为。上一个链接的示例代码

##teamcity[setParameter name='ddd' value='fff']

现在来看您的示例代码,如果是一次的话,请查看此answer

答案 1 :(得分:0)

因此,您有多个执行某些查询并填充数据的方法或类。您想将查询的开始和结束时间记录在返回对象中,该对象始终为ListReturnDTO<T>类型,其中T是要查询的实体类型。

您的问题缺少上下文,但是假设您使用诸如存储库模式之类的东西,则可以使您的基本存储库看起来像这样,并使用可公共调用的方法,该方法又调用必须由派生类实现的抽象方法,将实际工作分流到更专业的类型:

public abstract class BaseRepository<TEntity>
{
    public async Task<ListReturnDTO<TEntity>> QueryDataAsync()
    {
        var items = new ListReturnDTO<TEntity>();

        items.QueryStartTime = DateTime.UtcNow;

        await QueryAndPopulateDataAsync(items);

        items.QueryEndTime = DateTime.UtcNow;

        return items;
    }

    protected abstract Task QueryAndPopulateDataAsync(ListReturnDTO<TEntity> container);
}

这里有一个始终称为QueryDataAsync()的方法,该方法分配您始终希望分配的属性。

现在要实现实际的存储库,您可以继承基础存储库并在QueryAndPopulateDataAsync()中进行实际查询:

public class ProductRepository : BaseRepository<Product>
{
    protected override async Task QueryAndPopulateDataAsync(ListReturnDTO<TEntity> container)
    {
        container.TotalItem = 11;

        ........

        container.data.Add(new Product { SKU = "000006", Description = "this is the test product 7, a rare breed of product", Price = 65.00, QuantityLeft = 3, Title = "Test Product 7", IsPreview = false });
        container.data.Add(new Product { SKU = "000007", Description = "this is the test product 8, a rare breed of product", Price = 7.00, QuantityLeft = 30, Title = "Test Product 8", IsPreview = false });

        //force a delay to replicate talking to external source
        Thread.Sleep(2000);
    }
}

并这样称呼它:

var repo = new ProductRepository();
var data = await repo.QueryDataAsync();

并且data将分配其QueryStartTimeQueryEndTimedata属性。