使用Linq to SQL的DomainService自定义实现

本文关键字:DomainService 自定义 实现 SQL Linq to 使用 | 更新日期: 2023-09-27 18:07:06

谁能给我举个例子或简要描述一下如何使用Linq to SQL作为数据访问层创建WCF RIA Services DomainService的自定义实现,但不使用。dbml文件(这是因为Linq to SQL模型是由自定义工具生成的,是高度自定义的)。并且是一个相当大的数据库,有50多个表)和,没有VS2010向导创建DomainService(向导依赖于可用的。dbml文件)

这是我目前为止尝试的一个非常简单的shell:

[EnableClientAccess()]
public class SubscriptionService : DomainService
{
    [Query(IsDefault = true)]
    public IQueryable<Subscription> GetSubscriptionList()
    {
        SubscriptionDataContext dc = new SubscriptionDataContext();
        var subs = dc.Subscription.Where(x => x.Status == STATUS.Active)
            .Select(x => 
            new Subscription
            {
                ID = x.ID,
                Name = x.Name
            }).ToList();
        return subs.AsQueryable();
    }
    public void InsertSubscription(Subscription sub)
    {
        if (!sub.ID.IsEmpty())
        {
            SubscriptionDataContext dc = new SubscriptionDataContext();
            Subscription tmpSub = dc.GetByID<Subscription>(sub.ID);
            if (tmpSub != null)
            {
                tmpSub.Name = sub.Name;
                dc.Save(tmpSub);
            }
            else
            {
                tmpSub = new Subscription();
                tmpSub.Name = sub.Name;
                dc.Save(tmpSub);
            }
        }
    }
    public void UpdateSubscription(Subscription sub)
    {
        if (!sub.ID.IsEmpty())
        {
            SubscriptionDataContext dc = new SubscriptionDataContext();
            Subscription tmpSub = dc.GetByID<Subscription>(sub.ID);
            if (tmpSub != null)
            {
                tmpSub.Name = sub.Name;
                dc.Save(tmpSub);
            }
        }
    }
    public void DeleteSubscription(Subscription sub)
    {
        if (!sub.ID.IsEmpty())
        {
            SubscriptionDataContext dc = new SubscriptionDataContext();
            Subscription tmpSub = dc.GetByID<Subscription>(sub.ID);
            if (tmpSub != null)
            {
                dc.Delete(tmpSub);
            }
        }
    }
}
到目前为止,这似乎是有效的。有没有人发现了我可能遗漏的问题?如果有人已经尝试过这种方法并发现了一些主要问题,我不想在错误的道路上走得太远。

感谢大家的参与。

使用Linq to SQL的DomainService自定义实现

这样做没有错。

我基本上也做过同样的事情。

你将需要创建一个属性,为每个查询返回一个IQueryable,你将自动神奇地获得跳过/拿走/在哪里的RIA服务的东西。

[EnableClientAccess()]
public class SubscriptionService : DomainService
{
    [Query(IsDefault = true)]
    public IQueryable<Subscription> GetSubscriptionList()
    {
        using(var dc = new SubscriptionDataContext())
             return from x in dc.Subscription
                    where x.Status == STATUS.Active
                    select new Subscription { ID = x.ID, Name = x.Name };
        // make sure you don't call .ToList().AsQueryable() 
        // as you will basically load everything into memory, 
        // which you don't want to do if the client is going to be using 
        // any of the skip/take/where features of RIA Services.  
        // If you don't want to allow this, 
        // simply return an IEnumerable<Subscription>
    }
 }

我假设Subscription是DTO而不是L2S类,因为您正在显式地实例化它。只需确保您的dto具有正确的属性。例如

public class Subscription
{
    [Key]
    // you must have a key attribute on one or more properties...
    public int ID { get; set; }
}

如果DTO中有子元素,使用IncludeAssociation属性:

public class User
{
    [Key]
    public int Id { get; set; }
    [Include]
    [Association("User_Subscriptions", "Id","UserId")]
    // 'Id' is this classes's Id property, and 'UserId' is on Subscription
    // 'User_Subscriptions' must be unique within your domain service,
    // or you will get some odd errors when the client tries to deserialize
    // the object graph.
    public IEnumerable<Subscription> Subscriptions { get; set; }
}

顺便说一句,你的delete方法不需要完整的对象,类似这样的东西可以工作,并且可以防止客户端序列化整个对象并在你不需要的时候将其发回。

public void DeleteSubscription(int id)
{
    using(var dc = new SubscriptionDataContext())
    {
        var sub = dc.GetById<Subscription>(id);
        if( sub != null ) dc.Delete(sub);
    }
}