C#泛型简单应用

时间:2021-11-04 19:49:59

  最近老板要在app里开展金融模块了,产品一下就丢丢丢二三十个表单下来,怎么办,赶紧写代码,有20多个表单要提交呢,得建20多个表。等等,好像这些表单很相似,公司信息,个人信息,可是还有部分不同信息怎么办?可不可以存json数据,以前有模块仍json进一个字段,让app,web自己解析,这样耦合性太高了,除了前任第一次开发方便,现任维护起来非常痛苦,那就用mongo吧,一个集合,也不用建20多个表了,但是提交接口怎么办?二三十个接口想想就吐血,那就一个接口传个object对象,有没有什么办法能传只有我想要的对象,貌似泛型可以,那就用一下吧。

  下面是本次案例代码:

 private MongoServer mongoServer = new MongoServer();
private string collName = "Financial";
/// <summary>
/// 保存表单
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="form"></param>
/// <param name="userId">用户Id</param>
/// <param name="type">表单类型</param>
public void SaveBankingForm<T>(T form, int userId, int type) where T : Financial
{
var fromJson = JsonConvert.SerializeObject(form);
//获取类型
var financialType = XmlHelper.GetType("Financial", "FinancialType", type.ToString(), "id");
//拼接json字符串
var json = "{\"userId\":" + + ",\"typeId\":" + financialType.Id + ",\"typeName\":\"" + financialType.Name + "\"" +
",\"Financial\":" + fromJson + "}";
mongoServer.Insert(collName, json);
}
public void Test()
{
//假设有三个表单,有共同的CompanyInfo属性,部分相同的Person属性,其他各自的属性
//*********投资表单*********
TouZi touZi = new TouZi();
var company = new CompanyInfo() { CompanyName = "公司1", Address = "江陵路" };
var personTz = new PersonTz() { Name = "白玉京", Mobile = "" };
touZi.Company = company;
touZi.Person = personTz;
SaveBankingForm(touZi, , ); //*********融资表单*********
RrongZi rongZi = new RrongZi();
company = new CompanyInfo() { CompanyName = "公司2", Address = "市民中心" };
var personRz = new PersonRz() { Name = "白玉京1", Mobile = "", Amount = "10000.00" };
rongZi.Company = company;
rongZi.Person = personRz;
SaveBankingForm(rongZi, , ); //*********并购表单*********
BingGou bingGou = new BingGou();
company = new CompanyInfo() { CompanyName = "陌陌", Address = "市民中心" };
var personBG = new PersonBG() { Name = "白玉京", Mobile = "", Amount = "30000.00",BGCompany="探探" };
bingGou.Company = company;
bingGou.Person = personBG;
SaveBankingForm(bingGou, , );
}
    public class PersonTz : PersonInfo
{
}
public class PersonRz : PersonInfo
{
/// <summary>
/// 融资金额
/// </summary>
public string Amount { get; set; }
}
public class PersonBG : PersonInfo
{
/// <summary>
/// 收购金额
/// </summary>
public string Amount { get; set; }
/// <summary>
/// 并购单位
/// </summary>
public string BGCompany { get; set; }
}
/// <summary>
/// 投资表单
/// </summary>
public class TouZi : Financial
{
/// <summary>
/// 企业信息
/// </summary>
public CompanyInfo Company { get; set; }
/// <summary>
/// 投资表单个人信息
/// </summary>
public PersonTz Person { get; set; }
}
/// <summary>
/// 融资表单
/// </summary>
public class RrongZi : Financial
{
/// <summary>
/// 企业信息
/// </summary>
public CompanyInfo Company { get; set; }
/// <summary>
/// 融资表单个人信息
/// </summary>
public PersonRz Person { get; set; }
}
/// <summary>
/// 并购表单
/// </summary>
public class BingGou : Financial
{
/// <summary>
/// 企业信息
/// </summary>
public CompanyInfo Company { get; set; }
/// <summary>
/// 并购表单个人信息
/// </summary>
public PersonBG Person { get; set; }
}

  定义接口只接受Financial的类型,Financial是一个空的父类,所有继承Financial的子类都可用泛型接口SaveBankingForm保存。

  比如现实生活中,我们定义一个Animal类,FlyAnimal(飞行类),LandAnimal(爬行类),WaterAnimal(水生类)都继承了Animal,再定义比如:Fish继承WaterAnimal,Tiger继承LandAnimal,所有Fish,Tiger都可以用类型是Animal的泛型方法,Tiger不能用FlyAnimal的泛型方法。tree也不能用Animal的泛型方法。

  当然每个子类都有自己的具体属性和方法,这边又会涉及到抽象工厂模式了。

  最后再贴下Test()方法运行结果。C#泛型简单应用