Repository Pattern with Fluent Nhibernate -
hello i've implemented repository pattern fluentnhibernate have 1 concern abount exposeconfiguration when removed methods works fine when adds ,it resets tables in database. need take , give me notes implementation here unit of work class
public class unitofwork { public static isession nhibernatehelper() { isessionfactory _sessionfactory = fluently.configure() .database(mssqlconfiguration.mssql2012.connectionstring(@"data source=waail-pc\computerengineer;initial catalog=tsql2012;user id=sa;password=12345678ce").showsql()) .mappings(x => x.fluentmappings.addfromassemblyof<usersmap>()) //.exposeconfiguration(cfg => new schemaexport(cfg).create(false, true)) //when removed line doesn't //remove elements db .buildsessionfactory(); return _sessionfactory.opensession(); }}
and here repository pattern :
public class repository<t> :irepository<t> t :baseentity { public repository(/*iunitofwork unitofwork*/) { //_unitofwork = (unitofwork)unitofwork; } public list<t> getall() { using (isession session = unitofwork.nhibernatehelper()) { return session.query<t>().tolist(); } } public t getbyid(int id) { using (isession session = unitofwork.nhibernatehelper()) { return session.get<t>(id); } } public t insert(t entity) { try { using (isession session = unitofwork.nhibernatehelper()) { using (itransaction transaction = session.begintransaction()) { session.save(entity); transaction.commit(); } } return entity; } catch (exception) { throw; } } public void update(t entity) { using (isession session = unitofwork.nhibernatehelper()) { using (itransaction transaction = session.begintransaction()) { session.update(entity); transaction.commit(); } } } public void delete(int id) { using (isession session = unitofwork.nhibernatehelper()) { using (itransaction transaction = session.begintransaction()) { session.delete(session.load<t>(id)); transaction.commit(); } } } }
here usersservice
public class usersservice : iusersservice { private readonly irepository<users> _repository; #region constructor public usersservice(irepository<users> repository) { _repository = repository; } #endregion #region service implementation public list<users> getlistall() { return _repository.getall().tolist(); } public users getbyid(int id) { return _repository.getbyid(id); } public users insert(users user) { return _repository.insert(user); } public void update(users user) { _repository.update(user); } public void delete(int id) { _repository.delete(id); } //public int execute() //{ // return _repository.execute(); //} }
so need know why expose_configuration method causes tables reset... , secondly ,am moving in right way in implementation or not. if guys have update of improvements please tell me .. best regards.
the evil lies in details
you wrote:
... exposeconfiguration when removed methods works fine when add it, resets tables in database.
and that's pretty normal way of operations, since if nhiberate's schemaexport
classes create
method signature, reads:
public void create(action<string> scriptaction, bool execute)
it's documentation states create()
:
run schema creation script
and parameters:
scriptaction - action called each line of generated ddl.
execute - if ddl should executed against database.
and call as:
.exposeconfiguration(cfg => new schemaexport(cfg).create(false, true))
your problem lies in pass true
execute
parameter, -as stated in documentation- execute schema creation script, might include drop , rerecration of existing database objects.
why create demons?
about second idea, see no benefit of adding layer of indirection code, , that's summon unneeded demons.
the nhibernate orm is abstraction itself, why add 1 more generic 1 around it? repository<t>
calls implemented generic nhibernate methods... sort of acting proxy hide nhibernate, if that's intentional please pardon me, looks it's not.
on final note: usersservice
class first violates single responsibility principle , makes life harder maintaining , testing it. second, class not more proxy call repository return values from.
let me illustrate problem:
- client calls
usersservice.getbyid(id)
- service calls
_repository.getbyid(id)
- your generic abstraction repository calls nhibernates
session.get<t>(id)
do see problem?
how easier use pattern might in case, command query separation.
where commands responsible writes, , queries reads , both suits use-cases.
Comments
Post a Comment