.net - Is it possible to create a method which returns a key of entity by reference in C# with Entity Framework? -


let assume there 2 assemblies - core , infrastructure. in first 1 there interfaces , models. represents domain model , contains business logic. models built on abstractions - interface methods used.

for instance:

public interface inotificationservice {    void notify(user user, int modelid); }  public interface iauthorizationservice {    void isauthorized(user user); }  public interface ipersistenceservice {    int addsomeentity(somemodel model);    void savechanges(); }  public interface isomemodelmanagementservice {    void addsomemodelandnotify(user user, somemodel model); }  public class somemodelmanagementservice : isomemodelmanagementservice {    //here constructor injection of inotificationservice, iauthorizationservice , ipersistenceservice     void addsomemodelandnotify(user user, somemodel model)    {       if(authorizationservice.isauthorized(user))       {          int id = persistenceservice.addsomeentity(model);          persistenceservice.savechanges();          notificationservice.notify(user,id);       }       else       {          throw new unauthorizedexception();       }    } } 

all of code above reside in core assembly , have no reference implementation of interfaces (except isomemodelmanagementservice).

in infrastructure assembly on other hand implementation of infrastructure dependent concerns reside. there example code responsible sending e-mails emailnotificationservice or mapping , persisting core models in database dbpersistenceservice.

the problem i'm facing method:

int addsomeentity(somemodel model); 

i want achieve batch savechanges , not implement saving entities right after each command (insert/update/delete queries). problem inserting new entities entity framework. ef update model , return primary key after savechanges method executed on dbcontext.

i've tried pass parameter reference, it's not updated after savechanges.

it's possible dynamic type returned instead of int, insert commands / add methods return database model (not core model) , there's possibility id of after savechanges execution, it's dirty solution.

is possible reference entity framework key returned method?

is there possibility update returned id after savechanges execution?

int id = persistenceservice.addsomeentity(model); persistenceservice.savechanges(); //now i'd have id updated database key value 

one solution return wrapper id. since wrapper reference type should update id after savechanges().

// interface entities contains id. public interface iidentifiableentity {     int id { get; } }  public interface inotificationservice {    void notify(user user, iidentifiableentity entity); }  public interface iauthorizationservice {    void isauthorized(user user); }  public interface ipersistenceservice {    // return entity interface here...    iidentifiableentity addsomeentity(somemodel model);    void savechanges(); }  public interface isomemodelmanagementservice {    void addsomemodelandnotify(user user, somemodel model); }  public class somemodelmanagementservice : isomemodelmanagementservice {    //here constructor injection of inotificationservice, iauthorizationservice , ipersistenceservice     void addsomemodelandnotify(user user, somemodel model)    {       if(authorizationservice.isauthorized(user))       {          iidentifiableentity entity = persistenceservice.addsomeentity(model);          persistenceservice.savechanges();          // pass reference entity notify.          notificationservice.notify(user,entity);       }       else       {          throw new unauthorizedexception();       }    } } 

then entities have implement interface iidentifiableentity. think more elegant solution using dynamic. positive thing it's small interface , doesn't expose entity (except id). downside entity leaves scope of ipersistenceservice, may not want.

a sample implementation be:

public class persistentcustomerservice : ipersistenceservice {    public iidentifiableentity addsomeentity(customer model)    {       var result = _context.customers.add(model);       return result;    } }  public class customer : iidentifiableentity {     public int id {get;set;}     public string name {get;set;} } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -