Comments (9)
why exactly would you like to do that? what can be gained from doing that?
if the reasoning is good enough we can make inheritance from stream. but keep in mind that we cannot change the current public API of the library as it's already being used in many projects. I personally have about 6 large projects.
so there can be no breaking changes. also all the tests should pass without changing the tests.
let me know your thoughts. I'm open for improvements.
thanks!
from mongodb.entities.
First of all, it'll provide more usual interface for working with binary files. Secondly, end user of your library should not add one more additional variable (Stream). Thirdly, we provide to end user synchronous and asynchronous way for operate objects.
We can mark current DownloadAsync and UploadAsync method by ObsoleteAttribute and delete those methods after next major release.
from mongodb.entities.
i feel like inheriting from Stream would make the public API more verbose than it is right now.
can you tell me which methods/properties of Stream class you're planning to override and what the public api would look like afterwards? can you show examples for uploading/downloading after proposed change?
also i strongly believe we shouldn't provide sync methods for IO bound work like this and should always be async.
PS:
end user of your library should not add one more additional variable (Stream)
95% of the time, the target stream is not created by user (at least in my projects) and comes from asp.net/webapi Response.Body
or IFormFile.OpenReadStream()
. so i don't think this is a big deal.
but yeah, let's see your example api and i might be convinced.
from mongodb.entities.
we can however leave the current FileEntity.Data property alone and provide a new FileEntity.Stream property. which will return a new class called "DataStream" which you could add leaving the current "DataStreamer" class alone. so we would be not doing any breaking changes and providing a new stream interface for the api.
let me know your thoughts?
from mongodb.entities.
Just for sample:
public class MediaFile : FileEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
public MediaFile()
{
}
}
[HttpGet]
[Route("mediaContents/getFile/{fileId}")]
public HttpResponseMessage GetFile(Guid fileId)
{
MediaFile requiredFile;
HttpResponseMessage returnValue;
new DB("mediaContentStorage", "localhost", 27017);
requiredFile = DB.Queryable<MediaFile>().SingleOrDefault(mf => mf.Id == fileId);
if (requiredFile != default)
{
returnValue = Request.CreateResponse(HttpStatusCode.OK);
returnValue.Content = new StreamContent(requiredFile.Data);
returnValue.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
returnValue.Content.Headers.ContentDisposition.FileName = requiredFile.Name;
returnValue.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
}
else
{
returnValue = Request.CreateResponse(HttpStatusCode.NotFound);
}
return returnValue;
}
instead:
public async Task<HttpResponseMessage> GetFile(Guid fileId)
{
MediaFile requiredFile;
HttpResponseMessage returnValue;
new DB("mediaContentStorage", "localhost", 27017);
requiredFile = DB.Queryable<MediaFile>().SingleOrDefault(mf => mf.Id == fileId);
if (requiredFile != default)
{
//boilerplate code for stream preparation
var resultStream = new MemoryStream();
await requiredFile.DownloadAsync(resultStream);
//===
returnValue = Request.CreateResponse(HttpStatusCode.OK);
returnValue.Content = new StreamContent(resultStream);
returnValue.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
returnValue.Content.Headers.ContentDisposition.FileName = requiredFile.Name;
returnValue.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
}
else
{
returnValue = Request.CreateResponse(HttpStatusCode.NotFound);
}
return returnValue;
}
I did not compile first and second samples, that's why they can contain some lexical errors.
from mongodb.entities.
we can however leave the current FileEntity.Data property alone and provide a new FileEntity.Stream property. which will return a new class called "DataStream" which you could add leaving the current "DataStreamer" class alone. so we would be not doing any breaking changes and providing a new stream interface for the api.
let me know your thoughts?
Why you think, that inheriting DataStreamer from Stream will be breaking change? As I already wrote, I'll just mark DownloadAsync and UploadAsync by ObsoleteAttribute.
from mongodb.entities.
this is what i usually do without boilerplate:
public async Task<ActionResult> GetFile(Guid fileId)
{
new DB("mediaContentStorage", "localhost", 27017);
var requiredFile = DB.Queryable<MediaFile>().SingleOrDefault(mf => mf.Id == fileId);
if (requiredFile != default)
{
Response.StatusCode = (int)HttpStatusCode.OK;
Response.ContentType = "application/octet-stream";
await requiredFile.Data.DownloadAsync(Response.Body);
return new EmptyResult();
}
else
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return new EmptyResult();
}
}
but yeah all good. let's leave current functionality alone and add a new class called "DataStream" which inherits from "Stream" and new property called "FileEntity.Stream". if you do that as well as write tests for the new functionality i can accept your PR. in a future date, once the "DataStream" class is mature enough, we can deprecate the old functionality. by doing that, no existing projects need any refactoring.
deal?
PS: pls submit PR to the dev branch.
from mongodb.entities.
also, in my projects i cannot expose MongoDB.Entities classes or methods in my web api layer. my data access code is 2 layers down. so i need to pass down a stream from the web api layer down to the data access layer. here's an example.
from mongodb.entities.
closing due to inactivity. feel free to re-open.
from mongodb.entities.
Related Issues (20)
- DB.SaveAsync Error : Must contain at least 1 request HOT 3
- About the MongoDB.Driver.Linq.ExpressionNotSupportedException HOT 2
- Change Stream functionality doesn't work HOT 1
- Delete by lambda expression report errors HOT 4
- DBContext.DeleteAsync throwing exception starting from v21.0.2 HOT 2
- ReadName can only be called when State is Name, not when State is EndOfDocument. HOT 1
- DeleteAsync is Acknowledged but DeletedCount is always zero HOT 2
- Missing Parameterless constructor HOT 1
- The collection type 'MongoDB.Entities.Many' is abstract, an interface, or is read only, and could not be instantiated and populated HOT 1
- [Feature Request] Add property attribute to configure TTL HOT 1
- support soft delete HOT 3
- migrations run repeatly HOT 3
- I would like to ask how to write a query, can help me? HOT 8
- Q: Parallel integration tests with separate database per test class/collection HOT 1
- Nullable ID field in IEntity interface HOT 4
- Disallow saving objects with unregistered discriminator value HOT 3
- About Customize collection names HOT 1
- Does DB.Update lock row while update? HOT 3
- While updating how to add record if not found? HOT 1
- How to donate HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongodb.entities.