开发者

How could i define anonymous type?

开发者 https://www.devze.com 2023-04-04 10:54 出处:网络
I have anonymous type variable which i am populating like that(i have two variables jsonDataCache and jsonData but i want to have just one, jsonData for example) :

I have anonymous type variable which i am populating like that(i have two variables jsonDataCache and jsonData but i want to have just one, jsonData for example) :

 var jsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,

                rows = (from b in myData
                select new
                        {
                        //do things
                        }).ToArray()
             };
return Json(jsonData, JsonRequestBehavior.AllowGet);

But if i want to define this variable first, before i do populate that, basically i want to do something like that:

var jsonData = null;   

//check if jsonData in cache and if it is return Json(jsonData, JsonRequestBehavior.AllowGet);

jsonData = new
                {
                    total = totalPages,
                    page = page,
                    records = totalRecords,

                    rows = (from b in myData
                    select new
                            {
                            //do things
                            }).ToArray()
                 };
//put jsonData in cache by key

return Json(jsonData, JsonRequestBehavior.AllowGet);

How could i do that?

The reason i want to do that because i want to introduce the cache, so i need to define the variable first ant than check that in cache and if it is not i will do the thing above. Currently i did that with two variable开发者_JAVA技巧s, bot i want to use just one.

Here is how i currently done that:

public virtual JsonResult GetSomething(int id, int type)
{
   string keyPrefix = GetKeyPrefix(id, type);

   var jsonDataCache = CacheManager.Get<object>(keyPrefix);   

    if(jsonDataCache != null)
      return Json(jsonDataCache, JsonRequestBehavior.AllowGet);

    var myData = GetFromDataase();

    var jsonData = new
                    {
                        total = totalPages,
                        page = page,
                        records = totalRecords,

                        rows = (from b in myData
                        select new
                                {
                                //do things
                                }).ToArray()
                     };

    CacheManager.Set<object>(keyPrefix, jsonData);

    return Json(jsonData, JsonRequestBehavior.AllowGet);
}

UPDATE: After all your help I think it should be something like that, hopefully it is right way to go:

public virtual JsonResult GetSomething(int id, int type)
{
   string keyPrefix = GetKeyPrefix(id, type);

   var jsonData = CacheManager.Get<object>(keyPrefix);   

    if(jsonData != null)
      return Json(jsonData , JsonRequestBehavior.AllowGet);

    var myData = GetFromDataase();

    jsonData = new
                    {
                        total = totalPages,
                        page = page,
                        records = totalRecords,

                        rows = (from b in myData
                        select new
                                {
                                //do things
                                }).ToArray()
                     };

    CacheManager.Set<object>(keyPrefix, jsonData);

    return Json(jsonData, JsonRequestBehavior.AllowGet);
}               


Well, you can create it "by example" - with the same property names and types, in the same order, just with dummy values. For example:

var jsonData = new { total = 0, page = 0, records = 0,
                     rows = new[] { new { dummy = "" } };

It wasn't clear what you wanted the nested anonymous type to do, so I just created one with a string property called dummy.

Note that I'd try to avoid doing this sort of thing normally... as Marc says, if you can explain the bigger picture, it will help us to help you more.

EDIT: If it's for caching, how were you expecting to create the declare the cache in a statically-typed way? What is your Json method declared to accept? If it's declared to accept object, you don't need a strongly-typed variable, do you? If it's generic, that's fine - but it'll come back to how you manage to cast whatever's coming out of the cache appropriately...


You can't do that (and initialize to null), except for some really nasty and ugly "by example" tactics with generic methods. Variables to anonymous types can only be declared alongside initialisation (except as a generic type parameter).

It does, however, seem unlikely that you really need it here. Indeed,

object jsonData;

would suffice, since you never use the value. Beyond that, I'd argue that it is time to introduce a DTO class for the purpose instead.


It's unclear why you'd want to do this but here's one way:

You can create your object as type object or dynamic

object foo = null;

foo = new {
 bar = "bar"
};

Update:

Given your update I think there's a much more appropriate way to do this.

Personally I use the System.Web.Caching.Cache and have an extension method which checks the cache for your item (based on the cache key) or executes your method to retrieve the data.

public static T GetOrStore<T>(this Cache cache, string key, Func<T> generator)
        {
            var result = cache.Get(key);
            if(result == null)
            {
                result = generator();
                cache.Insert(key,result);
            }

            return (T) result;
        }

Which you can use like:

var jsonData = Cache.GetOrStore("jsonData", Foo);

where:

private static string Foo()
{
   return new {[...]}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号