静态类的模板 [英] Template for static class

查看:61
本文介绍了静态类的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在寻找一种方法来从我的POCO类导入数据时减少类似的代码。


在此例如,从CSV文件读取的类是AnimalCategory,用于存储该对象的DbSet是DbContext.DbSet< AnimalCatergory> throw属性AnimalCategories(这是DevArt自动生成的代码)。


所以,如果我有另一个对象(例如AnimalSubCategory),那么我已经完成了复制/粘贴这个function添加由AnimalSubCategory重命名AnimalCtegory。是的它有效,但我有6-8个类,所以复制这类代码不是很有用,不是
吗?


谢谢,


Vincent

 public static DBImportResult AnimalCategoriesById(DbContext ctx,string csvFile,char separator)
{
if(ctx == null)抛出新的ArgumentNullException(nameof(ctx));

DBImportResult result = new DBImportResult();

使用(TextReader reader = File.OpenText(csvFile))
{
var csv = new CsvReader(reader);
csv.Configuration.Delimiter = separator.ToString();
csv.Configuration.Encoding = Encoding.Default; ;
var records = csv.GetRecords< AnimalCategory>();

//方法2 - 检查每个项目以更新计数器
foreach(记录中的AnimalCategory rec)
{
//按ID列出所有物种。
List< AnimalCategory> searchQuery = ctx.AnimalCategories
.Where(b => b.Id == rec.Id)
.OrderBy(b => b.Id)
.ToList();

if(searchQuery.Count == 0)
{
//插入项目
ctx.AnimalCategories.Add(rec);
result.NewItems ++;
}
else
{
Debug.Assert(searchQuery.Count == 1,"警告 - 找到多于1个元素");

//更新项目字段
searchQuery.FirstOrDefault< AnimalCategory>()。CopyFrom(rec);
result.UpdatedItems ++;
}
result.TotalItems ++;
}
ctx.SaveChanges();
}

返回结果;
}

解决方案

所以,如果我有另一个对象(例如AnimalSubCategory),那么我已经完成了复制/粘贴这个功能,添加了AnimalSubCategory重命名的AnimalCtegory。是的它有效,但我有6-8个类,所以复制这种类型的
代码不是很有用吗?


https:// www .dotnetperls.com /部分的


Hi,

I'm looking a way to reduce similar code when importing datas from my POCO classes.

In this case, the class read from CSV file is AnimalCategory and the DbSet used to store this object is DbContext.DbSet<AnimalCatergory> throw property AnimalCategories (this is auto-generated code by DevArt).

So, if I have another object (AnimalSubCategory for example), for the moment, I've done Copy/Paste this function add rename AnimalCtegory by AnimalSubCategory. Yes it works but I have 6-8 classes so it's not very useful to duplicate this type of code isn't it ?

Thanks,

Vincent

public static DBImportResult AnimalCategoriesById(DbContext ctx, string csvFile, char separator)
        {
            if (ctx == null) throw new ArgumentNullException(nameof(ctx));

            DBImportResult result = new DBImportResult();

            using (TextReader reader = File.OpenText(csvFile))
            {
                var csv = new CsvReader(reader);
                csv.Configuration.Delimiter = separator.ToString();
                csv.Configuration.Encoding = Encoding.Default; ;
                var records = csv.GetRecords<AnimalCategory>();

                // Method 2 - Check each item to update counters
                foreach (AnimalCategory rec in records)
                {
                    // List all species by IDs.
                    List<AnimalCategory> searchQuery = ctx.AnimalCategories
                                    .Where(b => b.Id == rec.Id)
                                    .OrderBy(b => b.Id)
                                    .ToList();

                    if (searchQuery.Count == 0)
                    {
                        // Insert item
                        ctx.AnimalCategories.Add(rec);
                        result.NewItems++;
                    }
                    else
                    {
                        Debug.Assert(searchQuery.Count == 1, "warning - more than 1 element found");

                        // Update item field(s)
                        searchQuery.FirstOrDefault<AnimalCategory>().CopyFrom (rec);
                        result.UpdatedItems++;
                    }
                    result.TotalItems++;
                }
                ctx.SaveChanges();
            }

            return result;
        }

解决方案

So, if I have another object (AnimalSubCategory for example), for the moment, I've done Copy/Paste this function add rename AnimalCtegory by AnimalSubCategory. Yes it works but I have 6-8 classes so it's not very useful to duplicate this type of code isn't it ?

https://www.dotnetperls.com/partial


这篇关于静态类的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆