sqlite_orm icon indicating copy to clipboard operation
sqlite_orm copied to clipboard

Is it Possible To Provide a factory for sqlite_orm?

Open meghasemim1999 opened this issue 7 years ago • 12 comments

Hi I need to create the actual object in a factory with some pre Configurations then hand it over to sqlite_orm to feed it from database. is it possible to do such thing?

meghasemim1999 avatar Aug 27 '19 06:08 meghasemim1999

you have created a duplicate issue https://github.com/fnc12/sqlite_orm/issues/367 . Please close one of them

fnc12 avatar Aug 27 '19 08:08 fnc12

you have created a duplicate issue #367 . Please close one of them

Sorry ... my bad ... I closed the other one

meghasemim1999 avatar Aug 27 '19 09:08 meghasemim1999

I don't understand the issue. Could you provide more details please?

fnc12 avatar Aug 27 '19 09:08 fnc12

I don't understand the issue. Could you provide more details please?

i have some field in my entity that are not being stored in data base and it is safe for me to set those fields before ORM sets other fields for me

meghasemim1999 avatar Aug 27 '19 11:08 meghasemim1999

add default value to those fields

fnc12 avatar Aug 27 '19 11:08 fnc12

add default value to those fields

They Are Other Objects them selves I have them available at runtime

meghasemim1999 avatar Aug 27 '19 11:08 meghasemim1999

Can you provide an example? I still cannot understand clearly what you are talking about.

fnc12 avatar Aug 27 '19 11:08 fnc12

image

meghasemim1999 avatar Aug 27 '19 11:08 meghasemim1999

Ok, now I understand. These functionality is missing right now. I thought about this thing. The easiest solution is a sqlite_orm::factory<T> class which can be specialized:

namespace sqlite_orm {

    template<class T>
    struct factory {
        T make() const {
            return {};
        }
    };

}

And you specialize it like this:

namespace sqlite_orm {

    template<>
    struct factory<Entity> {
        Entity make() const {
            return {a, b}; // the question is where will you get those `a` and `b`
        }
    };

}

Is it ok for you?

fnc12 avatar Aug 27 '19 11:08 fnc12

it looks good but there will be some problems:

  • my code will be dependent on the specific version of your library and my project is a big one and if some day you change it my project could be corrupted

  • i need to store some fileds like "a" , "b" in my factory class to feed created entities, can i add fields to factory class in my specialization?

in fact i think it would be good if i could tell ORM to use one of my custom classes to create Entity

meghasemim1999 avatar Aug 28 '19 07:08 meghasemim1999

  1. your code depends on the lib anyway. If some code is merged in a release it means that it will be supported by further versions until major version is not changed. Code can be changed in dev branch between releases it is true. But this is the way development is performed.

  2. how do you create a and b? What dependencies do they have?

in fact i think it would be good if i could tell ORM to use one of my custom classes to create Entity

this is exactly what I suggested before: specialization is a custom class which is done by you in your code not sqlite_orm code. Just like it is done in enum_binding example https://github.com/fnc12/sqlite_orm/blob/master/examples/enum_binding.cpp

fnc12 avatar Aug 28 '19 08:08 fnc12

is the issue actual?

fnc12 avatar Aug 30 '19 09:08 fnc12