如何使用ADO.NET和存储过程在数据库中保存复杂的C#对象?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用ADO.NET和存储过程在数据库中保存复杂的C#对象?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2606字,纯文字阅读大概需要4分钟。
内容图文
![如何使用ADO.NET和存储过程在数据库中保存复杂的C#对象?](/upload/InfoBanner/zyjiaocheng/907/03410738cbf046c09adb0d332f76386a.jpg)
我有一个复杂的C#对象,需要在数据库中的多个表(大约12个)中持久化:
class Foo
{
public Guid FooId { get; set; }
public Bar[] Bars { get; set; }
}
class Bar
{
public Guid BarId { get; set; }
public Guid FooId { get; set; }
public Baz[] Bazs { get; set; }
}
class Baz
{
public Guid BazId { get; set; }
public Guid BarId { get; set; }
// And so on ...
}
问题:
>将此复杂对象(Foo实例)作为输入发送到存储过程的最佳方法是什么?
>在存储过程中,如何将相应的Foo,Bar和Baz表与其父ID相关联(例如Bar需要其父FooId)?
解决方法:
以下是您可能想要考虑建模的大致轮廓.这绝不是一个完整的骨架(因为你可能想稍微使用范围),但应该给出一个大致的想法.我在几个项目中使用过类似的模型.
如果它们都遵循相同类型的数据模型,则可以抽象这些类.
class Foo
{
public Guid FooId { get; private set; } // Only allow the object to define its Id.
public List<Bar> Bars { get; set; } // Use a dynamic storage object.
private bool Deleted { get; set; } // Monitors if the object has been deleted in the DB.
public Foo()
{
this.FooId = Guid.Empty;
this.Deleted = false;
Bars = new List<Bar>();
}
public Foo(Guid fooId)
{
this.Deleted = false;
this.FooId = fooId;
LoadFromDatabase();
Bars = new List<Bar>();
LoadBarsFromDatabase();
}
private void LoadFromDatabase()
{
// Read from DB, set local variables...
}
private void LoadBarsFromDatabase()
{
// Psuedocode to load all Bars.
foreach (var barData in barIdsLinkedToFooTable.Rows)
{
Bars.Add(new Bar(this, barData["barId"]));
}
}
public void SaveToDatabase()
{
// If FooId is Guid.Empty, the record is new.
// You may also want to consider the Deleted property value.
// Either generate the new Guid first or let SQL do it for you in your SP.
// Save Foo to DB with stored proc.
// Save Bars.
foreach (var bar in Bars)
{
bar.SaveToDatabase();
}
}
public void Delete()
{
// Delete via a stored proc.
// Might be a good idea to set SQL to cascade the delete,
// this way all the children will be deleted automatically.
this.Deleted = true;
}
}
class Bar
{
public Guid BarId { get; private set; } // Only allow the local object to generate Id.
public Foo ParentFoo { get; set; } // Store a reference to the parent.
public List<Baz> Bazs { get; set; } // Use a dynamic storage object.
private bool Deleted { get; set; } // Monitors if the object has been deleted in the DB.
public Bar(Foo parentFoo)
{
// New Bar object.
this.BarId = Guid.Empty;
this.ParentFoo = parentFoo;
this.Bazs = new List<Baz>();
this.Deleted = false;
}
public Bar(Foo parentFoo, Guid barId)
{
// Create from existing object.
this.Deleted = false;
this.BarId = barId;
this.ParentFoo = parentFoo;
LoadFromDatabase();
Bazs = new List<Baz>();
LoadBazsFromDatabase();
}
/* DB methods for load, save, and delete similar to above. */
}
内容总结
以上是互联网集市为您收集整理的如何使用ADO.NET和存储过程在数据库中保存复杂的C#对象?全部内容,希望文章能够帮你解决如何使用ADO.NET和存储过程在数据库中保存复杂的C#对象?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。