Need suggestions in help making a conditional parameter initialize a class with DI, C#, and Web API 2.0
I am using Unity dependency injection and Web API 2.0.
I have a controller which i am injecting my unit of work:
public ProductController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
I then have a method in that controller:
public HttpResponseMessage UpdateProducts(string website)
{
ProductAPI productAPI;
Enum.TryParse(website, out Website website);
productAPI = new ProductAPI(_unitOfWork, website);
productAPI.UpdateProducts();
}
The method takes in a parameter website which is a enum. It has 3 different values (website1, website2, website3).
This is the ProductAPI constructor that takes in UOW and the website enum:
public ProductAPI(IUnitOfWork unitOfWork, Website website)
{
_unitOfWork = unitOfWork;
_website = website;
_httpClient = CommonAPI.GetHttpClient(website);
}
The CommonAPI class is a static class that returns the httpclient based on the website.
public static HttpClient GetHttpClient(Website website)
{
HttpClient httpClient = new HttpClient();
if (website == Website.1)
{
//return httpclient for website 1
}
else if (website == Website.2)
{
//return httpclient for website 2
}
else if (website == Website.3)
{
//return httpclient for website 3
}
return httpClient;
}
The ProductAPI class also has calls within its methods that use the UOW but also use the website enum to filter based on the website value:
List<ProductViewModel> products = _unitOfWork.ProductRepository.GetProducts(_website.ToString());
Is there a better and cleaner way to set this code up based on that website enum?
c# api unity3d web
add a comment |
I am using Unity dependency injection and Web API 2.0.
I have a controller which i am injecting my unit of work:
public ProductController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
I then have a method in that controller:
public HttpResponseMessage UpdateProducts(string website)
{
ProductAPI productAPI;
Enum.TryParse(website, out Website website);
productAPI = new ProductAPI(_unitOfWork, website);
productAPI.UpdateProducts();
}
The method takes in a parameter website which is a enum. It has 3 different values (website1, website2, website3).
This is the ProductAPI constructor that takes in UOW and the website enum:
public ProductAPI(IUnitOfWork unitOfWork, Website website)
{
_unitOfWork = unitOfWork;
_website = website;
_httpClient = CommonAPI.GetHttpClient(website);
}
The CommonAPI class is a static class that returns the httpclient based on the website.
public static HttpClient GetHttpClient(Website website)
{
HttpClient httpClient = new HttpClient();
if (website == Website.1)
{
//return httpclient for website 1
}
else if (website == Website.2)
{
//return httpclient for website 2
}
else if (website == Website.3)
{
//return httpclient for website 3
}
return httpClient;
}
The ProductAPI class also has calls within its methods that use the UOW but also use the website enum to filter based on the website value:
List<ProductViewModel> products = _unitOfWork.ProductRepository.GetProducts(_website.ToString());
Is there a better and cleaner way to set this code up based on that website enum?
c# api unity3d web
So what exactly is the problem? If this is a basically working code you only want to improve I'ld say it should rather go to Code Review
– derHugo
Jan 2 at 8:00
add a comment |
I am using Unity dependency injection and Web API 2.0.
I have a controller which i am injecting my unit of work:
public ProductController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
I then have a method in that controller:
public HttpResponseMessage UpdateProducts(string website)
{
ProductAPI productAPI;
Enum.TryParse(website, out Website website);
productAPI = new ProductAPI(_unitOfWork, website);
productAPI.UpdateProducts();
}
The method takes in a parameter website which is a enum. It has 3 different values (website1, website2, website3).
This is the ProductAPI constructor that takes in UOW and the website enum:
public ProductAPI(IUnitOfWork unitOfWork, Website website)
{
_unitOfWork = unitOfWork;
_website = website;
_httpClient = CommonAPI.GetHttpClient(website);
}
The CommonAPI class is a static class that returns the httpclient based on the website.
public static HttpClient GetHttpClient(Website website)
{
HttpClient httpClient = new HttpClient();
if (website == Website.1)
{
//return httpclient for website 1
}
else if (website == Website.2)
{
//return httpclient for website 2
}
else if (website == Website.3)
{
//return httpclient for website 3
}
return httpClient;
}
The ProductAPI class also has calls within its methods that use the UOW but also use the website enum to filter based on the website value:
List<ProductViewModel> products = _unitOfWork.ProductRepository.GetProducts(_website.ToString());
Is there a better and cleaner way to set this code up based on that website enum?
c# api unity3d web
I am using Unity dependency injection and Web API 2.0.
I have a controller which i am injecting my unit of work:
public ProductController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
I then have a method in that controller:
public HttpResponseMessage UpdateProducts(string website)
{
ProductAPI productAPI;
Enum.TryParse(website, out Website website);
productAPI = new ProductAPI(_unitOfWork, website);
productAPI.UpdateProducts();
}
The method takes in a parameter website which is a enum. It has 3 different values (website1, website2, website3).
This is the ProductAPI constructor that takes in UOW and the website enum:
public ProductAPI(IUnitOfWork unitOfWork, Website website)
{
_unitOfWork = unitOfWork;
_website = website;
_httpClient = CommonAPI.GetHttpClient(website);
}
The CommonAPI class is a static class that returns the httpclient based on the website.
public static HttpClient GetHttpClient(Website website)
{
HttpClient httpClient = new HttpClient();
if (website == Website.1)
{
//return httpclient for website 1
}
else if (website == Website.2)
{
//return httpclient for website 2
}
else if (website == Website.3)
{
//return httpclient for website 3
}
return httpClient;
}
The ProductAPI class also has calls within its methods that use the UOW but also use the website enum to filter based on the website value:
List<ProductViewModel> products = _unitOfWork.ProductRepository.GetProducts(_website.ToString());
Is there a better and cleaner way to set this code up based on that website enum?
c# api unity3d web
c# api unity3d web
asked Jan 1 at 9:15
michaelmichael
11
11
So what exactly is the problem? If this is a basically working code you only want to improve I'ld say it should rather go to Code Review
– derHugo
Jan 2 at 8:00
add a comment |
So what exactly is the problem? If this is a basically working code you only want to improve I'ld say it should rather go to Code Review
– derHugo
Jan 2 at 8:00
So what exactly is the problem? If this is a basically working code you only want to improve I'ld say it should rather go to Code Review
– derHugo
Jan 2 at 8:00
So what exactly is the problem? If this is a basically working code you only want to improve I'ld say it should rather go to Code Review
– derHugo
Jan 2 at 8:00
add a comment |
1 Answer
1
active
oldest
votes
With your static method, basically you are using the factory method pattern. It's a common pattern, but if you want more flexibility you could use an abstract factory as described here.
The caller is defining what kind of website is used to update the products So, placing that logic in the controller makes perfect sense.
In general; there could be some higher level of abstraction but the code seems fine just as it is.
But if you want to get your hands dirty: you could abstract the WebClient.
E.g.:
Define a common interface:
public interface IYourTypicalHandler{ /*def here */}
And implement it:
public class Web1Client : IYourTypicalHandler, HttpClient { /*logic here */ }
public class Web2Client : IYourTypicalHandler, HttpClient { /*logic here */ }
public class Web3Client : IYourTypicalHandler, HttpClient { /*logic here */ }
Now you have encapsulated and isolated your specific web behavior in different classes. This can be used as implementation strategy for your ProductAPI
.
First create an interface for it:
public interface IProductApi { /* def here */ }
The signature of the implementation will be:
public class ProductAPI : IProductApi {
public ProductAPI(IUnitOfWork unitOfWork, IYourTypicalHandler)
}
Now you can use the IYourTypicalHandler
, to invoke the actual call. These modifications split up implementation details and makes the code easier to unit-test, and most likely, easier to use in a DI driven environment.
Of course you need to rewire your factory:
//note: not bound to http anymore ;-)
public static IYourTypicalHandler GetHttpClient(Website website)
At this point you don't have access to _website.ToString()
in:
var products = _unitOfWork.ProductRepository.GetProducts(_website.ToString());
which might complicate things, and might be targeted as well. So, it's up to you if it's worth it ;-)
Hope this helps.
add a comment |
Your Answer
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: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994270%2fneed-suggestions-in-help-making-a-conditional-parameter-initialize-a-class-with%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
With your static method, basically you are using the factory method pattern. It's a common pattern, but if you want more flexibility you could use an abstract factory as described here.
The caller is defining what kind of website is used to update the products So, placing that logic in the controller makes perfect sense.
In general; there could be some higher level of abstraction but the code seems fine just as it is.
But if you want to get your hands dirty: you could abstract the WebClient.
E.g.:
Define a common interface:
public interface IYourTypicalHandler{ /*def here */}
And implement it:
public class Web1Client : IYourTypicalHandler, HttpClient { /*logic here */ }
public class Web2Client : IYourTypicalHandler, HttpClient { /*logic here */ }
public class Web3Client : IYourTypicalHandler, HttpClient { /*logic here */ }
Now you have encapsulated and isolated your specific web behavior in different classes. This can be used as implementation strategy for your ProductAPI
.
First create an interface for it:
public interface IProductApi { /* def here */ }
The signature of the implementation will be:
public class ProductAPI : IProductApi {
public ProductAPI(IUnitOfWork unitOfWork, IYourTypicalHandler)
}
Now you can use the IYourTypicalHandler
, to invoke the actual call. These modifications split up implementation details and makes the code easier to unit-test, and most likely, easier to use in a DI driven environment.
Of course you need to rewire your factory:
//note: not bound to http anymore ;-)
public static IYourTypicalHandler GetHttpClient(Website website)
At this point you don't have access to _website.ToString()
in:
var products = _unitOfWork.ProductRepository.GetProducts(_website.ToString());
which might complicate things, and might be targeted as well. So, it's up to you if it's worth it ;-)
Hope this helps.
add a comment |
With your static method, basically you are using the factory method pattern. It's a common pattern, but if you want more flexibility you could use an abstract factory as described here.
The caller is defining what kind of website is used to update the products So, placing that logic in the controller makes perfect sense.
In general; there could be some higher level of abstraction but the code seems fine just as it is.
But if you want to get your hands dirty: you could abstract the WebClient.
E.g.:
Define a common interface:
public interface IYourTypicalHandler{ /*def here */}
And implement it:
public class Web1Client : IYourTypicalHandler, HttpClient { /*logic here */ }
public class Web2Client : IYourTypicalHandler, HttpClient { /*logic here */ }
public class Web3Client : IYourTypicalHandler, HttpClient { /*logic here */ }
Now you have encapsulated and isolated your specific web behavior in different classes. This can be used as implementation strategy for your ProductAPI
.
First create an interface for it:
public interface IProductApi { /* def here */ }
The signature of the implementation will be:
public class ProductAPI : IProductApi {
public ProductAPI(IUnitOfWork unitOfWork, IYourTypicalHandler)
}
Now you can use the IYourTypicalHandler
, to invoke the actual call. These modifications split up implementation details and makes the code easier to unit-test, and most likely, easier to use in a DI driven environment.
Of course you need to rewire your factory:
//note: not bound to http anymore ;-)
public static IYourTypicalHandler GetHttpClient(Website website)
At this point you don't have access to _website.ToString()
in:
var products = _unitOfWork.ProductRepository.GetProducts(_website.ToString());
which might complicate things, and might be targeted as well. So, it's up to you if it's worth it ;-)
Hope this helps.
add a comment |
With your static method, basically you are using the factory method pattern. It's a common pattern, but if you want more flexibility you could use an abstract factory as described here.
The caller is defining what kind of website is used to update the products So, placing that logic in the controller makes perfect sense.
In general; there could be some higher level of abstraction but the code seems fine just as it is.
But if you want to get your hands dirty: you could abstract the WebClient.
E.g.:
Define a common interface:
public interface IYourTypicalHandler{ /*def here */}
And implement it:
public class Web1Client : IYourTypicalHandler, HttpClient { /*logic here */ }
public class Web2Client : IYourTypicalHandler, HttpClient { /*logic here */ }
public class Web3Client : IYourTypicalHandler, HttpClient { /*logic here */ }
Now you have encapsulated and isolated your specific web behavior in different classes. This can be used as implementation strategy for your ProductAPI
.
First create an interface for it:
public interface IProductApi { /* def here */ }
The signature of the implementation will be:
public class ProductAPI : IProductApi {
public ProductAPI(IUnitOfWork unitOfWork, IYourTypicalHandler)
}
Now you can use the IYourTypicalHandler
, to invoke the actual call. These modifications split up implementation details and makes the code easier to unit-test, and most likely, easier to use in a DI driven environment.
Of course you need to rewire your factory:
//note: not bound to http anymore ;-)
public static IYourTypicalHandler GetHttpClient(Website website)
At this point you don't have access to _website.ToString()
in:
var products = _unitOfWork.ProductRepository.GetProducts(_website.ToString());
which might complicate things, and might be targeted as well. So, it's up to you if it's worth it ;-)
Hope this helps.
With your static method, basically you are using the factory method pattern. It's a common pattern, but if you want more flexibility you could use an abstract factory as described here.
The caller is defining what kind of website is used to update the products So, placing that logic in the controller makes perfect sense.
In general; there could be some higher level of abstraction but the code seems fine just as it is.
But if you want to get your hands dirty: you could abstract the WebClient.
E.g.:
Define a common interface:
public interface IYourTypicalHandler{ /*def here */}
And implement it:
public class Web1Client : IYourTypicalHandler, HttpClient { /*logic here */ }
public class Web2Client : IYourTypicalHandler, HttpClient { /*logic here */ }
public class Web3Client : IYourTypicalHandler, HttpClient { /*logic here */ }
Now you have encapsulated and isolated your specific web behavior in different classes. This can be used as implementation strategy for your ProductAPI
.
First create an interface for it:
public interface IProductApi { /* def here */ }
The signature of the implementation will be:
public class ProductAPI : IProductApi {
public ProductAPI(IUnitOfWork unitOfWork, IYourTypicalHandler)
}
Now you can use the IYourTypicalHandler
, to invoke the actual call. These modifications split up implementation details and makes the code easier to unit-test, and most likely, easier to use in a DI driven environment.
Of course you need to rewire your factory:
//note: not bound to http anymore ;-)
public static IYourTypicalHandler GetHttpClient(Website website)
At this point you don't have access to _website.ToString()
in:
var products = _unitOfWork.ProductRepository.GetProducts(_website.ToString());
which might complicate things, and might be targeted as well. So, it's up to you if it's worth it ;-)
Hope this helps.
edited Jan 1 at 9:52
answered Jan 1 at 9:29


StefanStefan
8,53373761
8,53373761
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994270%2fneed-suggestions-in-help-making-a-conditional-parameter-initialize-a-class-with%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So what exactly is the problem? If this is a basically working code you only want to improve I'ld say it should rather go to Code Review
– derHugo
Jan 2 at 8:00