design patterns - Pass "this" object by reference in C# -


this question has answer here:

i need pass object reference in c#. know not possible. have multi-tier application. in nutshell, dal getting data web service in json format. json data has converted in business-layer objects. thus, initialize business layer object , pass dal. dal convert data object. show example of code. first dal:

public stream  getsession ( ref businesslayer.session session) {     stream datastream;     // use servicemanager build send data services     // soaservicemanager sm = new soaservicemanager("http://www.test.da/authentication.json","",dataaccesslayer.httpmethod.post);      // add values      sm.addcriteriavalue ("name","demo");     sm.addcriteriavalue ("password","demo");      // it's show time , datastream full     datastream = sm.commitpost ();      datacontractjsonserializer ser = new datacontractjsonserializer(typeof(businesslayer.session));      session = (businesslayer.session)ser.readobject(datastream);      return datastream; } 

now business layer using dal class:

namespace businesslayer {     public class session     {         public bool success { get; set; }         public string name { get; set; }           public session ()         {             dataaccesslayer.session dal_session = new dataaccesslayer.session ();             dal_session.getsession ( ref this);         }     } } 

so problem is, not possible send "this" reference. solution see, create copy object , send dal , assign values object. not clever solution. there way solve in c#?

you should not creating new session object. instead replace datacontractjsonserializer newtonsoft.json (since former not expose such method) , use method reads json data , populates existing object:

using (var reader = new newtonsoft.json.jsontextreader(new streamreader(datastream))) {     var serializer = new newtonsoft.json.jsonserializer();     serializer.populate(reader, session); } 

alternatively not use constructor instead static factory method:

public class session {     private session() { }     public static create()     {         dataaccesslayer.session dal_session = new dataaccesslayer.session ();         var session = new session();         dal_session.getsession (ref session);         return session;     } } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -