使用Linq to SQL自定义实现DomainService

时间:2011-08-31 20:02:32

标签: c# silverlight linq-to-sql wcf-ria-services

有人能指出一个示例或简要描述如何使用Linq to SQL 作为数据访问层创建WCF RIA Services DomainService的自定义实现,但是没有使用.dbml 文件(这是因为Linq to SQL模型是由自定义工具生成的,是大量切割的,并且是一个包含50多个表的相当大的数据库)和没有VS2010用于创建DomainService的向导(该向导依赖于.dbml文件可用)

这是我迄今为止尝试过的一个非常简单的外壳:

[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);
            }
        }
    }
}

到目前为止这似乎有效。有没有人看到我可能会遗漏这种方法的任何问题?如果有人已经尝试过这种方式并且发现了一些重大问题,我不想在这个错误的道路上走太远。

感谢大家的投入。

1 个答案:

答案 0 :(得分:2)

这样做是没有错的。

我基本上做了同样的事情。

您需要创建一个为每个查询返回IQueryable的属性,然后您将自动神奇地获取使用RIA服务的skip / take / where。

[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; }
}

另外作为旁注,你不需要删除方法的完整对象,这样的东西会起作用,并且会阻止客户端序列化整个对象,并在你不需要时将其发回。 / p>

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