简体   繁体   English

Xamarin SQLite.Net PCL - 保持域模型不受sqlite属性影响的好方法是什么?

[英]Xamarin SQLite.Net PCL — what is good option to keep domain model free of sqlite attributes?

I'm starting out with Xamarin and sqlite, specifically this package: 我开始使用Xamarin和sqlite,特别是这个包:

https://github.com/oysteinkrog/SQLite.Net-PCL https://github.com/oysteinkrog/SQLite.Net-PCL

There is no fluent api (IE like in Entity Framework), which is OK, but instead, does anyone have a pattern to keep the attributes, such as [PrimaryKey] out of the domain model so that my domain library doesn't need a reference to the sqlite libraries? 没有流畅的api(IE实体框架中的IE),这没关系,但是,有没有人有一个模式来保持属性,例如[PrimaryKey]超出域模型,这样我的域库就不需要了参考sqlite库? The only way I can see is to create separate classes in my sqlite repo library for each of my domain classes, and employ some kind of mapping scheme. 我能看到的唯一方法是在我的sqlite repo库中为每个域类创建单独的类,并采用某种映射方案。 But that seems a lot of work just to avoid an attribute. 但这似乎只是为了避免属性而做了很多工作。 In fact, in this case it's probably easier just to bang out the sql and do it that way instead of using SQLite.Net-PCL's built-in ORM. 事实上,在这种情况下,可能更容易就是敲出sql而不是使用SQLite.Net-PCL的内置ORM。

It's probably worth it in my case to just litter my domain with the attributes and create a dependency on the SQLite.Net-PCL library. 在我的情况下,使用属性丢弃我的域并在SQLite.Net-PCL库上创建依赖项可能是值得的。

Are there any other libraries that i can use with xamarin/PCL that might help? 我可以使用xamarin / PCL的任何其他库可能有帮助吗? Or is there a better technique? 还是有更好的技术?

If there really is no configuration-based approach like the fluent API you mention (I don't know of one), you have two options: 如果确实没有像你提到的流畅的API那样基于配置的方法(我不知道一个),你有两个选择:

Use a separate persistence model 使用单独的持久性模型

This is usually not as much effort as it initially appears, and it provides real separation of concerns. 这通常不像最初出现的那么多,它提供了真正的关注点分离。 By going down this path, you also have a natural location for schema migration logic and custom data mappings. 通过沿着这条路径前进,您还可以获得模式迁移逻辑和自定义数据映射的自然位置。

The ugly part of this is the mapping between the persistence and the domain model, which can typically be solved elegantly by using an object to object mapper. 丑陋的部分是持久性和域模型之间的映射,通常可以通过使用对象映射器来优雅地解决。 For C#, a very good solution is AutoMapper . 对于C#,一个非常好的解决方案是AutoMapper

Re-evaluate your decision to go with DDD 重新评估您决定使用DDD

This is a rather drastic change, but given the fact that you're going to build a mobile app, there is a chance that DDD is a bit heavy anyway. 这是一个相当剧烈的变化,但考虑到你要构建一个移动应用程序的事实,DDD有可能有点重。 If that's the case*, the door is open to just make the trade-off and have a unified persistence and "domain" model. 如果是这种情况*,那么大门是敞开的,只需进行权衡并拥有统一的持久性和“域”模型。

*: Take this decision seriously and consider all requirements. *: 认真对待这个决定并考虑所有要求。 Don't base your decision solely on the "implementation detail" problem that you're having right now. 不要仅仅根据您现在遇到的“实施细节”问题做出决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM