开发者

Can I use an ORM (as Subsonic) to work with an API?

开发者 https://www.devze.com 2022-12-29 05:08 出处:网络
I´m new to ORMs and I have a new project I´ll do in .Net MVC. In the Model Layer I´ll create my classes: Videos and I´ll work with APIs as a DataLayer (BrightCove & YouTube APIs).

I´m new to ORMs and I have a new project I´ll do in .Net MVC.

In the Model Layer I´ll create my classes: Videos and I´ll work with APIs as a DataLayer (BrightCove & YouTube APIs).

So, I dont have a Relatio开发者_运维问答nal Database as a Data Layer. Is it possible to work with an ORM (as Subsonic)?

Thanks!


Pretty sure this isn't do-able. Or at least it's not the intention of an ORM.

If you're dealing with an api you'd be making a call and getting back a result yes? or you'd make a call and then get values from properties?

Either way there really isn't anything for the ORm to map from. It has no idea on calling api's etc. They are given objects that they can interpret using reflection (i assume) etc.

So I think you'll need to create an object yourself from the API, in which case you probably don't need an ORm in this case. Unless you create your API model in a layer of some sort and then use an ORM to translate to a model used by the application. This would be a better solution because if the API model changes the changes to your code would be minimal.


There is a .NET API for Youtube that provides classes for videos as well as data access methods to get data from Youtube. I think unless you have a good reason to roll your own, you might consider using their .NET API and directly using or extending the classes they provide. If you want to abstract out the data access, you could create your own Video class with just the properties you need and implement your own video repository which uses Youtube's API to hydrate instances of your class.


In the SubSonic code (just download it from github) there is a neat method in the SqlQuery class that is called "ExcecuteTypedList()".

It is just a small method that uses reflection to map a result from a DataReader to a generic class. It's simple but brilliant.

You could look at the code and modify it for yourself to fit your needs.

It basically does the following if you call ExecuteTypedList()

  • create an instance of your class
  • for the parameter "Test" it tries to load the property "Test of the class instance
  • Sets the value
0

精彩评论

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