Static wcf client to make multiple request with one object

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I want to make a static client to call a wcf because the first time I call the wcf it takes too long, even sometimes it throws time out.



I have the following class:



public class Repository : IRepository

protected static ServiceClient Client get; set;

public Repository()
Client.ChannelFactory.State != CommunicationState.Created)

Client = new ServiceClient();



public virtual async Task<Response> GetPersonsFromAddress(string address)

try

Request request = new Request();
request.Address = address;
Response response = await Client.GetPersonsAsync(request);

return response;

catch (Exception ex)

// ...
// Handle exception
// ...





And this class calls the method:



public class Conector : IConector

private IRepository Repository get;

public Conector()

Repository = new Repository();


public virtual async Task<Response> GetPersonsFromAddress(string address)

Response response = await Repository.GetPersonsFromAddress(address);
return response;




The object that calls the wcf service is static so I want to know if this is a good practice.



Also if there are a lot of people at the same time using this method, it will return the correct response for each request.







share|improve this question

















  • 1




    Is the variable Cliente and Client meant to be the same?
    – BKSpurgeon
    Jan 18 at 23:00










  • @BKSpurgeon Yes! Sorry for that, now it is correct
    – Sxntk
    Jan 19 at 13:36
















up vote
1
down vote

favorite












I want to make a static client to call a wcf because the first time I call the wcf it takes too long, even sometimes it throws time out.



I have the following class:



public class Repository : IRepository

protected static ServiceClient Client get; set;

public Repository()
Client.ChannelFactory.State != CommunicationState.Created)

Client = new ServiceClient();



public virtual async Task<Response> GetPersonsFromAddress(string address)

try

Request request = new Request();
request.Address = address;
Response response = await Client.GetPersonsAsync(request);

return response;

catch (Exception ex)

// ...
// Handle exception
// ...





And this class calls the method:



public class Conector : IConector

private IRepository Repository get;

public Conector()

Repository = new Repository();


public virtual async Task<Response> GetPersonsFromAddress(string address)

Response response = await Repository.GetPersonsFromAddress(address);
return response;




The object that calls the wcf service is static so I want to know if this is a good practice.



Also if there are a lot of people at the same time using this method, it will return the correct response for each request.







share|improve this question

















  • 1




    Is the variable Cliente and Client meant to be the same?
    – BKSpurgeon
    Jan 18 at 23:00










  • @BKSpurgeon Yes! Sorry for that, now it is correct
    – Sxntk
    Jan 19 at 13:36












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to make a static client to call a wcf because the first time I call the wcf it takes too long, even sometimes it throws time out.



I have the following class:



public class Repository : IRepository

protected static ServiceClient Client get; set;

public Repository()
Client.ChannelFactory.State != CommunicationState.Created)

Client = new ServiceClient();



public virtual async Task<Response> GetPersonsFromAddress(string address)

try

Request request = new Request();
request.Address = address;
Response response = await Client.GetPersonsAsync(request);

return response;

catch (Exception ex)

// ...
// Handle exception
// ...





And this class calls the method:



public class Conector : IConector

private IRepository Repository get;

public Conector()

Repository = new Repository();


public virtual async Task<Response> GetPersonsFromAddress(string address)

Response response = await Repository.GetPersonsFromAddress(address);
return response;




The object that calls the wcf service is static so I want to know if this is a good practice.



Also if there are a lot of people at the same time using this method, it will return the correct response for each request.







share|improve this question













I want to make a static client to call a wcf because the first time I call the wcf it takes too long, even sometimes it throws time out.



I have the following class:



public class Repository : IRepository

protected static ServiceClient Client get; set;

public Repository()
Client.ChannelFactory.State != CommunicationState.Created)

Client = new ServiceClient();



public virtual async Task<Response> GetPersonsFromAddress(string address)

try

Request request = new Request();
request.Address = address;
Response response = await Client.GetPersonsAsync(request);

return response;

catch (Exception ex)

// ...
// Handle exception
// ...





And this class calls the method:



public class Conector : IConector

private IRepository Repository get;

public Conector()

Repository = new Repository();


public virtual async Task<Response> GetPersonsFromAddress(string address)

Response response = await Repository.GetPersonsFromAddress(address);
return response;




The object that calls the wcf service is static so I want to know if this is a good practice.



Also if there are a lot of people at the same time using this method, it will return the correct response for each request.









share|improve this question












share|improve this question




share|improve this question








edited Jan 19 at 13:36
























asked Jan 18 at 20:44









Sxntk

1085




1085







  • 1




    Is the variable Cliente and Client meant to be the same?
    – BKSpurgeon
    Jan 18 at 23:00










  • @BKSpurgeon Yes! Sorry for that, now it is correct
    – Sxntk
    Jan 19 at 13:36












  • 1




    Is the variable Cliente and Client meant to be the same?
    – BKSpurgeon
    Jan 18 at 23:00










  • @BKSpurgeon Yes! Sorry for that, now it is correct
    – Sxntk
    Jan 19 at 13:36







1




1




Is the variable Cliente and Client meant to be the same?
– BKSpurgeon
Jan 18 at 23:00




Is the variable Cliente and Client meant to be the same?
– BKSpurgeon
Jan 18 at 23:00












@BKSpurgeon Yes! Sorry for that, now it is correct
– Sxntk
Jan 19 at 13:36




@BKSpurgeon Yes! Sorry for that, now it is correct
– Sxntk
Jan 19 at 13:36










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










I think you should consider inverting control and moving ServiceClient instantiation outside the repository class. Whether or not ServiceClient is a singleton (in single-instance sense) or not is not something that repository should manage or care about:



public class Conector : IConector

private IRepository Repository get;

public Conector(IRepository repository)

Repository = repository;

...


public class Repository : IRepository

protected ServiceClient Client get;

public Repository(ServiceClient client)

Client = client;


...






share|improve this answer





















  • Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Like public Conector() in the question and public Conector(IRepository repository) like yours?
    – Sxntk
    Jan 19 at 13:43










  • @Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance of ServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
    – Nikita B
    Jan 22 at 10:08










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185428%2fstatic-wcf-client-to-make-multiple-request-with-one-object%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










I think you should consider inverting control and moving ServiceClient instantiation outside the repository class. Whether or not ServiceClient is a singleton (in single-instance sense) or not is not something that repository should manage or care about:



public class Conector : IConector

private IRepository Repository get;

public Conector(IRepository repository)

Repository = repository;

...


public class Repository : IRepository

protected ServiceClient Client get;

public Repository(ServiceClient client)

Client = client;


...






share|improve this answer





















  • Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Like public Conector() in the question and public Conector(IRepository repository) like yours?
    – Sxntk
    Jan 19 at 13:43










  • @Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance of ServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
    – Nikita B
    Jan 22 at 10:08














up vote
2
down vote



accepted










I think you should consider inverting control and moving ServiceClient instantiation outside the repository class. Whether or not ServiceClient is a singleton (in single-instance sense) or not is not something that repository should manage or care about:



public class Conector : IConector

private IRepository Repository get;

public Conector(IRepository repository)

Repository = repository;

...


public class Repository : IRepository

protected ServiceClient Client get;

public Repository(ServiceClient client)

Client = client;


...






share|improve this answer





















  • Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Like public Conector() in the question and public Conector(IRepository repository) like yours?
    – Sxntk
    Jan 19 at 13:43










  • @Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance of ServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
    – Nikita B
    Jan 22 at 10:08












up vote
2
down vote



accepted







up vote
2
down vote



accepted






I think you should consider inverting control and moving ServiceClient instantiation outside the repository class. Whether or not ServiceClient is a singleton (in single-instance sense) or not is not something that repository should manage or care about:



public class Conector : IConector

private IRepository Repository get;

public Conector(IRepository repository)

Repository = repository;

...


public class Repository : IRepository

protected ServiceClient Client get;

public Repository(ServiceClient client)

Client = client;


...






share|improve this answer













I think you should consider inverting control and moving ServiceClient instantiation outside the repository class. Whether or not ServiceClient is a singleton (in single-instance sense) or not is not something that repository should manage or care about:



public class Conector : IConector

private IRepository Repository get;

public Conector(IRepository repository)

Repository = repository;

...


public class Repository : IRepository

protected ServiceClient Client get;

public Repository(ServiceClient client)

Client = client;


...







share|improve this answer













share|improve this answer



share|improve this answer











answered Jan 19 at 11:24









Nikita B

12.3k11652




12.3k11652











  • Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Like public Conector() in the question and public Conector(IRepository repository) like yours?
    – Sxntk
    Jan 19 at 13:43










  • @Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance of ServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
    – Nikita B
    Jan 22 at 10:08
















  • Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Like public Conector() in the question and public Conector(IRepository repository) like yours?
    – Sxntk
    Jan 19 at 13:43










  • @Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance of ServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
    – Nikita B
    Jan 22 at 10:08















Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Like public Conector() in the question and public Conector(IRepository repository) like yours?
– Sxntk
Jan 19 at 13:43




Is it ok to have 2 constructors? One for the dependency injection and the other one for a default inialization of the inyected class? Like public Conector() in the question and public Conector(IRepository repository) like yours?
– Sxntk
Jan 19 at 13:43












@Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance of ServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
– Nikita B
Jan 22 at 10:08




@Sxntk it depends on what default constructor would actually do. If it is going to create and manage some static instance of ServiceClientclass then it is hardly an improvement. You would just move the problem from one class to another.
– Nikita B
Jan 22 at 10:08












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185428%2fstatic-wcf-client-to-make-multiple-request-with-one-object%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods