c# - How to resolve Cross-Dependency -


i'm having hard times trying resolve cross-dependency between projects in 1 solution. solution it's asp.net application has several projects.

the web project consumes services of sessionwrapper class in sln.core project. declared like:

public sealed class sw {     public static tbluserrow user     {                 {             if (httpcontext.current == null) return null;             return httpcontext.current.session["dtuser"] == null ? null : (tblusersrow)(httpcontext.current.session["dtuser"] tblusers).rows[0];         }     }      public static void user_load(string userid)     {         tblusers users = new tblusers();         users.loadbyid(userid);         if (users.count != 0)         {            httpcontext.current.session["dtuser"] = users;         }     } } 

the tbluserrow part of model definition of users class (strong typed datatable) in separate models project. there other methods in sw class, aren't relevant. in sln.web can access user data "sw.user.name"

the models project composed structures (model classes) , db engine classes in 2 different namespaces.

as seen above, sw class depends on models declare user. here it's ok.

but fact model objects, need user, company, etc data when classes first created can default values. f.ex: if create new invoice, it's nice user (customer) assigned warehouse or payment type.

and per working requeriments, db engine needs company or user data making things getting db (one per company) or saving log entries user info.

until day objects passed on every call db engine classes or methods, i'm refactoring , thought cleaner , less memory consuming if info got directly on respective places sw.

but there cross-dependency between them. , sw members declared static, can persist indepentent session, can't make interface.

any suggestions?

update: there issue scope of user data solved, corrected here. i'll add more code better understanding. here vb, sorry dudes, that's nice model of diversity.

example of tbluser model in sln.models:

<designercategory("code"), system.serializableattribute()> partial public class tblusers     inherits typedtablebase(of tblusersrow)      <debuggernonusercodeattribute()>     public sub new()         mybase.new()         begininit()         initclass()         endinit()     end sub      <debuggernonusercodeattribute()>     private sub initclass()         tablename = table_name          columns             .add(new datacolumn(field_id, gettype(string)))             .add(new datacolumn(field_name, gettype(string)))             ...             'added last columns example             .add(new datacolumn(field_company, gettype(string)) {.defaultvalue=sw.company.id})             .add(new datacolumn(field_warehouse, gettype(string)) {.defaultvalue=sw.company.warehouse})         end          dim keys(1) datacolumn         keys(0) = columns(0)         primarykey = keys     end sub ...      <debuggernonusercodeattribute()>     public sub loadbyid(id string)         rows.clear()         merge(new sln.dbengine.generic(sw.company.connection, dolog:=false).executequery(command_loadby_id, id))     end sub ... end class partial public class tblusersrow     inherits datarow     <debuggernonusercodeattribute()>     friend sub new(byval builder datarowbuilder)         mybase.new(builder)     end sub      <debuggernonusercodeattribute()>     public property id() string                     return directcast(mybase.item(0), string)         end         set(value string)             mybase.item(0) = value         end set     end property      <debuggernonusercodeattribute()>     public property name() string                     return directcast(mybase.item(1), string)         end         set(value string)             mybase.item(1) = value         end set     end property     ... end class 

**model classes while solution on ef multiple db. plain datatables.

yes, there's model class named company used in sw provide company's data in session (multiple users, multiple companies logged onto) can see on user construction, default values retrieved sw. same other models invoice heads, per example. that's behaviour want. models default values, complete object parameter of new(). models want 1 field +25 flds object. use dbengine loading/querying/saving/deleting data.

in sln.web may seen (login):

sw.company_load(ddlcompany.text) sdescription = new generic(sw.company.connection,false).executequery("sp_warehouse_loadby_id",sw.user.warehouse).rows(0)("rdescription").tostring 

thaaats roough example.

so sln.core.sw needs sln.models.tblusersrow , sln.dbengine.generic

and sln.models.tblusers needs sln.dbengine sln.core.sw ...

sln.dbengine... needs sln.core.sw knows db point (some other things)

and sln.web need of them. (sigh!)

clear? (errr...)

it hard understand story clearly. get, model need current_user create objects (well, don't business logic why required), , web sln need model current_user.

basically, flow should this:

  • user class defined
  • current_user initiated
  • using current_user, model initiate other objects

there many ways available accomplish this, propose 2 solution:

  1. this implemenatation done assuming dependency injection. clean, , testable. design model solution.

    public interface iuserprovider {     user currentuser { get; } }  public class modelcreator {     public modelcreator(iuserprovider provider)     {         this.provider = provider;     }     iuserprovider provider;      public invoice get(){         user currentuser = provider.currentuser;         // other     } } 
  2. this implementation done without dependency injection. not clean, mockable , easy design. design model or entity solution.

    public static class userprovider {     private static func<user> currentuserdelegate = new func<user>(nulluser);     public static func<user> currentuserdelegate     {         set         {             currentuserdelegate = value;         }     }      private static user nulluser()     {         return null;     }     public static user currentuser     {                 {             return currentuserdelegate();         }     } } 

    the usage:

    public sealed class sw {     private static user _currentuser;     public static user getcurrentuser()     {         if (_currentuser == null)         {             tblusers users = new tblusers();             users.loadbyid(userid);             httpcontext.current.session["dtuser"] = users;             _currentuser = users[0];         }         return _currentuser;     }      public static void user_load(string userid)     {         userprovider.currentuserdelegate = new func<user>(getcurrentuser);     } } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -