将 Linq 与现有对象结合使用
我的系统有这种模式 DAO->Objects->facade->View
所以我有一个 DAO 来查询数据库,并实例化对象,这个对象只有属性(只是一个容器/实体),我想在中使用 LINQ DAO 部分,但我不知道如何传递我的对象,因为 LINQ 为每个表生成 1 个对象。
namespace ykpObjects.Objects
{
public class Customer
{
public string name { get; set; }
public Cidade()
{
cidadeID = 0;
}
}
}
namespace ykpData.Components.MSSQL
{
public class CustomerDC : DataComponentCM, ICustomerDC
{
Customer ICustomerDC.RecuperaPorID(int CustomerID)
{
Customer Customer = new Customer();
using (MDDataContext omd = new MDDataContext(base.PreencherConexao()))
{
sp_mkp_Customer_SelectByIDResult result = omd.sp_mkp_Customer_SelectByID(CustomerID).SingleOrDefault();
if (result == null)
return null;
Customer.name = result.name;
return Customer;
}
}
}
}
我使用 DAO 来调用存储过程,所以我得到存储过程结果并实例化一个 Customer 对象,并将其传递给控件,现在我想更改为 linq,但我不想更改所有对象结构以最小化影响。
有什么建议吗?
My system has this pattern DAO->Objects->facade->View
so i have a DAO to query database, and instantiate objects, this objects has only attributes (just a container / entity), i want to use LINQ in DAO part, but i dont realize how to pass my objects becouse LINQ generate 1-per table.
namespace ykpObjects.Objects
{
public class Customer
{
public string name { get; set; }
public Cidade()
{
cidadeID = 0;
}
}
}
namespace ykpData.Components.MSSQL
{
public class CustomerDC : DataComponentCM, ICustomerDC
{
Customer ICustomerDC.RecuperaPorID(int CustomerID)
{
Customer Customer = new Customer();
using (MDDataContext omd = new MDDataContext(base.PreencherConexao()))
{
sp_mkp_Customer_SelectByIDResult result = omd.sp_mkp_Customer_SelectByID(CustomerID).SingleOrDefault();
if (result == null)
return null;
Customer.name = result.name;
return Customer;
}
}
}
}
I use DAO to call sprocs, so i get sproc results and instanciate a object of Customer for exemple, and pass this to control, now i want to change to linq but i dont wanna change all object structure to minimalize the impact.
Any advice ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定您当前的设置正在谈论什么,但我认为您是在问如何重用 Linq to SQL 当前拥有的对象,而不是从 dbml 文件生成新对象。我说得对吗?
如果是这样,您有几个选择。您可以使用属性来装饰现有对象,以便可以使用 L2S 填充它们,也可以创建映射文件。
这里有一些信息: http://www.sidarok.com/web/blog/content/2008/10/14/achiving-poco-s-in-linq-to-sql.html
我使用 Linq to SQL属性来实现“代码优先”解决方案,下面是一个示例类:
要使用此对象:
I'm not exactly sure what you are talking about with your current setup, but I think you're asking how to re-use the objects you currently have with Linq to SQL, rather than generating new ones from a dbml file. Am I right?
If so, you have a few options. You can use attributes to decorate your existing objects so that you can populate them with L2S, or you can create mapping files.
Some info here: http://www.sidarok.com/web/blog/content/2008/10/14/achieving-poco-s-in-linq-to-sql.html
I use Linq to SQL with attributes to achieve a "code first" solution, here's an example class:
To work with this object: