开发者

RIA Services extending code-generated entity model partial classes

I'm writing an application that has POCO Entity Framework data access on the server. When RIA services creates the model in Silverlight, it generates a (let's call it) "RIA Services" model. That is, it's not just a copy of my POCOs. These classes in the model, to begin with, are sealed, and for instance expose Lists as EntityCollections.

Since they are sealed, I cannot extend them to (say) provide extra functionality (that doesn't belong in the server), or implement an interface.

I can, however, extend the functionality of these classes by implementing the defined partial methods, and adding extra properties to the other partial class.

Is doing so generally discouraged. Is it some kind of code smell? Is there a good reason why开发者_Go百科 I shouldn't do this?


We use partial implementations frequently but I don't find it to be code smell. In fact, I think it is a lot better than wrapping them with more code. At that point, you might as well skip RIA and write your own WCF change tracker... but I digress.

If you want a rich domain model, then partial implementations are one great way to accomplish that. The default code generator creates partial methods for the most meaningful points in a WCF RIA entity's life cycle. By implementing these partials, you move away from the Anemic Domain Model.

As you stated above, you can implement an interface and create your own methods and properties. IOW you are not limited to implementing the partial stubs. If all else fails, modify the default code generator to your liking. I did to remove sealed from every entity to make testing easier.

UPDATE: Here is my code to remove sealed

[DomainServiceClientCodeGenerator(typeof (CustomClientCodeGenerator), "C#")]
public class CustomClientCodeGenerator : CSharpClientCodeGenerator
{
    private EntityGenerator _entityGenerator;

    protected override EntityGenerator EntityGenerator
    {
        get { return _entityGenerator ?? (_entityGenerator = new CustomEntityGenerator()); }
    }

    private class CustomEntityGenerator : CSharpEntityGenerator
    {
        public override string TransformText()
        {
            return base.TransformText().Replace("sealed ", string.Empty);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜