c# - Implementing Unit Of Work in WinForms -


i have implemented repository pattern in winforms application:

unitofwork:

using rccgspp.core; using rccgspp.persistence.repositories; using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.data.entity;  namespace rccgspp.persistence {     //implements logic methods in iunitofwork interface     public class unitofwork     {         //our app contextclassname         private readonly sppcontext _context;         private dbcontext dbcontext;          //recieves our app contextclassname         public unitofwork(sppcontext context)         {             //stores our app contextname in _context             _context = context;             //then uses context initialise both repositories             persons = new personrepository(_context);             sundayservices = new sundayservicerepository(_context);             userpasses = new userrepository(_context);             newcomers = new newcomerrepository(_context);         }          public unitofwork(dbcontext dbcontext)         {             this.dbcontext = dbcontext;         }          //properties          public personrepository persons { get; private set; }         public sundayservicerepository sundayservices { get; private set; }         public userrepository userpasses { get; private set; }         public newcomerrepository newcomers { get; private set; }          //calls savechanges on context         public int complete()         {             return _context.savechanges();         }          //implementation of dispose method dispose context         public void dispose()         {             _context.dispose();         }     } } 

my form whwere want use unitofwork, have declared readonly property have included in contructor initialise since form load @ click of button "there no argument given corresponds required formal parameter 'unitofwork'"

form use unitofwork

    public partial class register : materialform     {         private readonly iunitofwork _unitofwork;         string username;         string psswrd;         string confirmpsswrd;          public register(iunitofwork unitofwork)         {             initializecomponent();             //set preferred colors &theme (material skin)             var materialskinmanager = materialskinmanager.instance;             materialskinmanager.addformtomanage(this);             materialskinmanager.theme = materialskinmanager.themes.dark;             materialskinmanager.colorscheme = new colorscheme(primary.blue400, primary.red900, primary.brown900, accent.lightblue200, textshade.black);             //prevent form resizing              sizable = false;              //unitofwork             _unitofwork = unitofwork;         }           private void register_load(object sender, eventargs e)         {          }          private void btnsubmit_click(object sender, eventargs e)         {             //get user email , password register in db             username = textemail.text;             psswrd = textpassword.text;             confirmpsswrd = textconfirmpassword.text;             //compare password             bool conres = comparepassword(psswrd, confirmpsswrd);             if (conres)             {                 //insert db using unitofwork                 userpass usertodb = new userpass                 {                     username = this.username,                     password = this.psswrd,                  };                  _unitofwork.userpasses.add(usertodb);                 //commit calling complete()                 _unitofwork.complete();                 //feedback registered sucessfull                 labelerrorpassword.text = "successful, login!";             }             else             {                 labelerrorpassword.backcolor = color.red;                 labelerrorpassword.text = "the passwords don't match!"; //show in label password not same             }         }           /**********method compare password**********************/         public bool comparepassword(string pss1, string pss2)         {             if (pss1.equals(pss2))             {                 return true;             }             else return false;         }     } 

loading form :

private void btnregister_click(object sender, eventargs e) {     //load register form,     register nform = new register();     nform.show(); } 

how can make use of unitofwork in winform application.

as @stuartd said, parameterless contructor form in winforms recommended. of course can add constructor arguments.

public register() {     initializecomponent();     //set preferred colors &theme (material skin)     var materialskinmanager = materialskinmanager.instance;     materialskinmanager.addformtomanage(this);     materialskinmanager.theme = materialskinmanager.themes.dark;     materialskinmanager.colorscheme = new colorscheme(primary.blue400, primary.red900, primary.brown900, accent.lightblue200, textshade.black);     //prevent form resizing      sizable = false;      //unitofwork, not initialized here     //_unitofwork = unitofwork; }      public register(iunitofwork unitofwork) : this() // call default constructor! {   _unitofwork = unitofwork; } 

i recommend leave auto-generated , custom initialization code in default (parameterless) constructor. so, winforms designer not stop working.

we use strategy in combination di containers in several projects without problem.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -