Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Silas Reinagel
    @SilasReinagel
    Telegram, to me, doesn't seem very nice
    vivek poddar
    @vivekimsit
    @SilasReinagel +1
    Ix
    @ixmanuel
    @SilasReinagel :+1:
    VytasGit
    @VytasGit
    @SilasReinagel +1
    yohanmishkin
    @yohanmishkin

    Any thoughts on what an "elegant" approach to returning an object's encapsulated data as a response to query made to a JSON REST API might look like?

    More concretely, imagine you had an /api/fruit/1 endpoint that returned the colour and type properties of a Fruit object in json. It doesn't make sense to me that a Fruit object would have a toString() or print(Media) method. I can't imagine a domain in which a Fruit object could "print" itself. How would you expose those data without mapping to some kind of DTO or getter?

    Victor Noël
    @victornoel
    @yohanmishkin the question is maybe: why do you need this data? maybe it is the object that needs it that should do the query and then deal with the json return, not a Fruit object.
    What I do with json retrieved data is that I have inner classes (in the object that needs the information) that are jackson annotated and I deserialize it from within the parent object. Then I use the annotated class as pure data holder, or sometimes I add extra methods to them if needed, but I avoid to expose those method outside of the real object
    ha, I think I misunderstood the question, it's not about reading the json, it's about outputing it (when implementing a REST API)
    in that case, exposing a proper rest API is not really an elegant object oriented approach :) rest is resource oriented, so it's very data oriented...but what I do currently is that I have objects that provide a Object json() method and the object they return is actually a DTO annotated with jackson. There is not so much choice there I think...
    (and I pass this object to the jackson ObjectMapper that takes care of serializing it)
    Silas Reinagel
    @SilasReinagel
    @yohanmishkin What doesn't make sense about having an Object print his information to a String/Media?
    yohanmishkin
    @yohanmishkin

    @victornoel Yeah, resource-orientation vs object-orientation was exactly what I was getting at. Thanks for sharing your approach!

    @SilasReinagel It just doesn't make sense to me to give certain objects printing behavior. Fruit might have ripen(Days),Shirt might have buttonUp() or rollSleeves(), Person might have run() or walk() or laugh(Volume) behavior. I can't, in any of these cases imagine those objects having print() or toJson() behavior alongside the behaviors we're trying to model.

    Silas Reinagel
    @SilasReinagel
    The domain behaviors are different than the computer-science behaviors that are needed, its true
    That said, I don't know if there is a better solution
    It seems that you can serialize data traits in the following ways:
    1) Reflection based serialization (violates encapsulation)
    2) Public Properites/Getters (violates encapsulation)
    3) Non-invented access modifiers such as friend
    4) Objects printing themselves to the desired media
    Of those, the fourth one does seem the nicest to me. You can look at a class and see how the object expresses himself in a serialized form. If you change the object, you change the serialized representation. That seems elegant to me.
    Can you think of any other ways to get Json with the data traits of an Object?
    This was an Object that I created yesterday:
        public sealed class TransactionRequest
        {
            private string AccountId { get; set; }
            private string Description { get; set; }
            private decimal Amount { get; set; }
    
            public TransactionRequest(string accountId, string description, decimal amount)
            {
                AccountId = accountId;
                Description = description;
                Amount = amount;
            }
    
            public Event ToEvent()
            {
                var timestamp = Clock.UnixUtcNow;
                var json = new JsonObjectString()
                    .With(nameof(Description), Description)
                    .With(nameof(Amount), Amount)
                    .With(nameof(timestamp), timestamp)
                    .ToString();
                return new Event(AccountId, nameof(Transaction), 1, json, timestamp);
            }
        }
    yohanmishkin
    @yohanmishkin

    @SilasReinagel thanks for sharing! You covered all the options I can think of (we need that friend modifier! I wanna say I saw a @yegor256 post suggesting something similar trust).

    I remember a while back reading a pertinent post from Mark Seeman that still seems to hold true here. @yegor256, please weigh in if we're missing something here.

    Victor Noël
    @victornoel
    @SilasReinagel I agree with you, even with the pattern I proposed, I could have used a printer like you do.
    I also think that the fact that we use webframework that potentially provides the serialization via things like Jackson makes it hard to intellectually think of the printer approach: I just want at the same time my object to be properly encapsulated but I want to let jackson do its work: obviously there is something paradoxical in this (as you highlighted in ways 1 and 2)
    yohanmishkin
    @yohanmishkin

    Not sure if this has already been undertaken or at least considered, but I'm starting a project to make an Elegant Objects static analyzer for .Net.

    Here's where I'll be working on it if anyone is interested: https://github.com/yohanmishkin/mandatory-elegance

    I started entering the chapters of the two volumes as Github issues
    Once I'm done entering the issues I'll start knocking them out!
    goqp
    @goqp
    Anyone around?
    yohanmishkin
    @yohanmishkin
    @goqp howdy!
    goqp
    @goqp
    Ah some people are still coming around here!
    Zsolt Sandor
    @sz332
    hello
    yohanmishkin
    @yohanmishkin
    Anyone know if Zerocracy is doing singularly java work?
    goqp
    @goqp
    @ Sarah Mei. This is pattently untrue.

    Hey all, as the Yegor research group project (this chat) is ending now, I am making a new research group for OO enthusiasts.
    if you are especially interested in this project please message me.
    Otherwise I will be posting the invite generally in public chat.
    If I dont see you again on the new chat or here, then its been really fun and thanks.
    goqp
    @goqp
    FYI, as I understand it, the new Yegor chat on Telegram is a business oriented chat for professionals. Theres OO dev along the Yegor paradigm, everything is geared towards work and business needs, but its light hearted and you can goof around too. You can hook up on projects and get paid, get help with applying OO to your code, and so on.
    It is not, however, focused on research or experiments in OO. My chat is for this purpose only and will not be satisfying the sorts of needs as Yegor's telegram chat.
    Zsolt Sandor
    @sz332
    @goqp how can I message you? I am looking forward joining the new chat.
    Victor Noël
    @victornoel
    @goqp just advertise it here, no?
    Stian Soiland-Reyes
    @stain
    @goqp looking forward to invite
    Stian Soiland-Reyes
    @stain
    will there be money for opinions? :)
    Josh Schwartzberg
    @dotjosh
    I just read both books -- anyone have any non-hello-world github repo examples of fully adopting the oop practices mentioned?
    Victor Noël
    @victornoel
    @dotjosh check cactoos and other projects by yegor256 on github
    goqp
    @goqp
    email me at goqp@outlook.com
    i added you guys to a list of people I am inviting personally
    @dotjosh Check out my github for an original OOP approach to Yegor's Hangman. Its beta. I will commit a reformatted version a little later.
    If you want Yegor style then I think one or two of the submissions on his challenge site fit pretty well, you can browse.
    Here is my approach: https://github.com/goqp/Hangman
    goqp
    @goqp
    Here are other people's submittals attempting the Yegor (EO) style: https://github.com/yegor256/hangman
    yohanmishkin
    @yohanmishkin
    @goqp could I bother you to add me to the list too?
    goqp
    @goqp
    I did : )
    I will msg you all when Ive got it up