将 Linq 与现有对象结合使用

发布于 2024-11-06 01:29:39 字数 1131 浏览 0 评论 0原文

我的系统有这种模式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏雨一样淡蓝的深情 2024-11-13 01:29:39

我不太确定您当前的设置正在谈论什么,但我认为您是在问如何重用 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属性来实现“代码优先”解决方案,下面是一个示例类:

[Table(Name = "Countries")]    
public class Country
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int CountryId { get; set; }

    [Column]
    public string iso2 { get; set; }

    [Column]
    public string iso3 { get; set; }

    [Column]
    public string name_en { get; set; }
}

要使用此对象:

var context = new DataContext(ConnectionString);
var data = context.GetTable<Country>().Where(c => c.CountryId == 1);

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:

[Table(Name = "Countries")]    
public class Country
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int CountryId { get; set; }

    [Column]
    public string iso2 { get; set; }

    [Column]
    public string iso3 { get; set; }

    [Column]
    public string name_en { get; set; }
}

To work with this object:

var context = new DataContext(ConnectionString);
var data = context.GetTable<Country>().Where(c => c.CountryId == 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文