Maintain Web APIs consistency in teams - c#

Please feel free for editing the caption of this question.
I was part of a team which develops web applications by using AngularJS and Web APIs. We developed Web APIs for each page and grab data by Angular.
after a while, We realized the most of web APIs are sames, because we could not realized which Web APIs already exists. In some cases, we have same APIs but different routes.
For example, in ShowOrderDetail.html we need to show the user's profile info. A developer implements a Web API to retrieve data from UserProfile ( only 3 fields). However, in other page another developer implements another API to retrieve 5 fields.
I would like to know if you have any other experiences in designing these kind of systems and how handle this issue. Is there other techniques or tools instead of managerial aspects (like setting name convention)?

It doesn't make a lot of sense to create a lot of separate services for the same project, and it's certainly excessive to create a completely separate Web API for every single individual page in the web site. This'll inevitably lead to a lot of redundancy and wasted effort regardless of other means you're using to organize your code (tools, naming conventions, etc.).
I'd suggest reorganizing your services in terms of the operations you're performing regularly, not in terms of one service per page. You can create separate controllers to organize your service into different types of operations (in fact, I'd recommend this - this article has a good description of different ways of organizing code) but again this should be organized in terms of reusable operations, not in terms of individual pages.

Related

Common Business Layer for a new Web API and existing ASP .NET Web site Solution

We have an active fully developed ASP.NET web application which allows users to create/update data for various screens/modules.
Now, There is a requirement to build a Web API that will automate the data creation for a particular module alone.
This API will receive data from source system and saves the posted data to our application database
(eventually removing the front-end option to create)
The Web Solution has it's own Business & Data Layer included in the solution and we are looking at ways to re-use the layers
in the API as well.
One way I can think of is to refer the Business layer as a dll in the Web API but this would
provide access to other classes & methods in the BL which is not required at this point
Can anyone please suggest what is the right approach?
splitting up a solution into multiple projects is not a bad thing.
One project for your BLL, another for your DAL is a good approach and I would definitely recommend going with that.
Yes, it does mean that if you add a reference to the BLL project into the API then yes, the API will have access to more than it needs for now, but that is not really an issue. How much you use is up to you.
Plus, if you start using the API you will probably find that more and more code will naturally converge towards that so having access to everything will make life easier, long term.
You could also break out the specific bit of code you are interested in into an extra project / dll and reference that in the API, if you see value in that, but I don't think that's particularly useful.
Another thing that I highly recommend is to have a separate project for your model classes, it makes things much easier when it comes to sharing them.

Using the ASP.NET MVC framework to create generalized framework for multiple similar sites

I have a fairly high level design question. I'd like to come up with a framework for creating multiple very similar ASP.NET MVC sites.
These sites would be for selling various consumer goods, with each goods category having its own site (this requirement is fixed). Different category sites would mostly be quite similar - essentially a series of static pages that allow drilling down into various subcategories to view product listings, with some simple forms for expressing interest in a listing (for example).
However, it's vague at this point exactly how similar these sites might be, so I need a degree of flexibility
I come from a React background- so my vague initial thought is that the best way to approach this would be creating a library of common components used across the sites - these would range from fairly low level ones for, for example, a table of specifications, to higher level ones that might cover an entire page. The library consumer could pass in the specific apis used for the components/other specific data. I don't have much experience with the ASP.NET MVC framework, so I'm not yet certain exactly how that would work.
My question is: does this sound like I'm on the right track with this approach?
Any alternate suggestions? Also, are there any particular ASP.NET MVC features that would be key to an approach like this? (Not sure yet whether I'd use the older .NET Framework MVC, or .NET Core- any features of either that would favor one or the other for this?)
Thanks so much for your time, I appreciate the help!
There's frankly all kinds of ways to do this, and you may choose one, some, or all. For common classes (entities, helpers, etc.) you can create class library projects. You can also share controller code this way as well. Making smart use of generics will allow you reuse greater amounts of code. Sharing views is a bit more complicated, but there are ways to do that, such as RazorGenerator or NuGet packages. NuGet packages should be used to share static resources, as well, such as images, JavaScript, stylesheets, etc. Alternatively, you can host these on a CDN or other centralized area and reference them from there (use configuration to set path/URL, so you don't hardcode static paths all over the place).
As far as framework choice goes, ASP.NET Core provide a bit more than MVC in the code reuse category. You can create custom middleware, view components, tag helpers, etc. ASP.NET MVC has somewhat similar capabilities (HTTP modules, child actions, and HTML helpers), but it's arguably a bit more difficult to reuse those components/create them in the first place. Other than that, though, choice of framework really doesn't matter.
As the Definition of MVC, what you say that "creating a library of common components used across the sites" are content in the model and controller part. MVC has nothing special from other OOP approaches, but just specify that you are going to divide your abstract classes into three different category: Model (which define the container of data), View (which define the appearance of the program) and Controller (which define the behaviour of the program).
MVC is just a pattern but not a technology. For example one of the web-application I worked on in the company, the MVC structure followed as: ExtJs frontend (it has it's own controller written in javascript to call our API, therefore all itself could already be called as a "MVC" program), C# API server backend which linked back to our database (both Oracle and Microsoft SQL). As you can see, all of these components can be replaced by something else (for instance, you can use Angular2 replace ExtJs, you can use java or php replace the C# API server, and so on).

How to make the framework and the dependent applications loosely coupled?

I have a specific case and I want to know the best practice way to handle it.
I make a specific .NET framework (web application). This web application acts like a platform or framework to many other web applications through the following methodology :
We create our dependent web applications (classes for the project business, rdlc reports) in a separate solutions then build them.
After that we add references to the resulted dll in the framework.
And create set of user controls (one for each dependent web application) and put them in a folder in the framework it self.
It works fine but any modification to a specific user control or any modification to any one of the dependent web applications. We have to add the references again and publish the whole framework !!
What I want to do is make those different web applications and the framework loosely coupled. So I could publish the framework one and only one and any modifications to the user controls or the different web applications just publish the updated part rather than the whole framework .
How to refactor my code so I can do this?
The most important thing is :
Never publish the whole framework if the change in any dependent application, just publish the updated part belongs to this application .
If loose coupling is what you are after, develop your "framework(web application)" to function as a WCF web service. Your client applications will pass requests to your web services and receive standard responses in the form of predefined objects.
If you take this route, I recommend that you implement an additional step: Do not use the objects passed to your client applications directly in your client code. Instead, create versions of these web service objects local to each client application and upon receiving your web service response objects, map them to their local counterparts. I tend to implement this with a facade project in my client solution. The facade handles all calls to my various web services, and does the mapping between client and service objects automatically with each call. It is very convenient.
The reason for this is that the day that you decide to modify the objects that your web service serves, you only have to change the mapping algorithms in your client applications... the internal code of each client solution remains unchanged. Do not underestimate how much work this can save you!
Developing WCF web services is quite a large subject. If you are interested, a book that I recommend is Programming WCF Services. It offers a pretty good introduction to WCF development for those who come from a .NET background.
I totally agree with levib, but I also have some tips:
As an alternative to WCF (with its crazy configuration needs), I would recommend ServiceStack. Like WCF it lets you receive requests and return responses in the form of predefined objects, but with NO code generation and minimal configuration. It supports all kinds of response formats, such as JSON, XML, JSV and CSV. This makes it much easier to consume from f.ex. JavaScript and even mobile apps. It even has binaries for MonoTouch and Mono for Android! It is also highly testable and blazing fast!
A great tool for the mapping part of your code is AutoMapper, it lets you set up all your mappings in a single place and map from one object type to another by calling a simple method.
Check them out! :)
Decades of experience says: avoid the framework and you won't have a problem to solve.
Frameworks evolve like cancer. The road to hell is paved with good intentions, and a good portion of those good intentions are embodied in a colossal tumour of a framework all in the name of potential re-use that never really happens.
Get some experience and knowledge when it comes to OO and design, and you'll find endless solutions to your technical problem, such as facades, and mementos, and what have you, but they are not solutions to your real problem.
Another thing, if you are using MS technology, don't bother with anything beyond what .NET offers. Stick with what the MS gods offer because as soon as you digress and become committed to some inhouse framework, your days are numbered.

Why doesn't PHP need a middle tier, where as other languages do

For web based applications, why doesn't PHP need middleware to run - yet languages like Java, C#, etc do?
UPDATE:
Re-worded: Why doesn't PHP need a middle tier, or business layer separating it from the database, whereas the others do.
Assuming that by the term "middleware" you mean "a middle tier", or "a business layer" the answer is that none of them need it.
For example, there is nothing to stop you in C# (or more correctly, on the .Net Framework "stack") from writing code in web pages that directly accesses the database. Indeed, lots of prototypes start out this way.
The issue here is more around good practice - it is generally considered A Bad Thing(tm) to write web pages (sticking with the same example) that directly access the database and the reasons for this are many. Testability, security, good decoupled code - all these require you to separate your code out, and having several tiers is a natural way to do this.
Why do you not see as much of this with PHP? I think Jeff's latest blog post covers this well :)
I'd go as far as to say that C# (the language), .Net (the Framework), ASP.NET (especially ASP.NET MVC) and much of the documentation and tutorials encourage you to do the right thing and not punch a whole from the web page through to the database.
But there isn't actually anything stopping you from doing it.
The use case that can appear is the following:
You have multiple web apps serving data that is stored in a database. Let's say the web apps are:
Front-end for your desktop web experience
Front-end for your mobile web experience
Front-end for your APIs that you expose to developers
Let's say each front-end accesses the database directly. Then, your eng team decides that the current database should be replaced. Now you have the problem of rewriting code in every front-end.
If you had a middle tier to abstract the actual data store, you'd only re-write one layer of code. Additionally, having unit tests for that middle tier would ensure that the way the middle tier exposes data remains uniform, regardless of the data storage platform.

consuming web services - how to develop ui pulling results from many sources

I´m looking for books, tutorials or videos displaying best practices for consuming webservices. My main idea is to learn how to manage a user interface were results are pulled from many sources (eg: ebay developer, yahoo shopping xml, etc) and displayed to customers in a list of results. many years ago a website called www.mpire.com used to work in that way, displaying results on demand.
I´m developing with C#, razor, ef 4, sql server.
thanks in advance.
Here is an overview. With this you can start searching more concepts on google.
Learn how to connect to the various API's and retrieve data. You can do this in your controller but it would be considered best practice to create a c# api wrapper for each of the api's you are connecting to. Your application then can use the wrapper to simplify and seperate concerns. For many popular api's .net wrappers are already created and are available on open source sites such as codeplex or github. The documentation for each api is a good place to start. Generally they will reference a wrapper for the language you are working in or may have developed there own you can download.
Once you are retrieving data, then you have to consider if you are going to store the data in your app or if it is always going to call to the api to get the data. Depending on the situation, you can store the data in you database, thus making things faster and reducing calls to the external api. This doesn't work / isn't allowed in all situations but just depends on your use case. If you are going to save the data, you will need to learn about database persistance. Linq2sql is a good place to start because it is so easy. There are good examples on www.asp.net/mvc
Depending on if you are retrieving the data from your database or from the API directly, you will then need to create custom view models for your views. In your controller, you can gather the data from the various sources and combine it into a single object called a view model. From there you pass the view model to your view and then display the data on the page. I would stay away from the asynchronous controllers until you get everything working properly and go into performance tuning. These will add complexity that you don't need as you are learning.
Because invoking remote services is an I/O intensive tasks it would be beneficial to use asynchronous controllers. The documentation contains a couple of examples of howto put them in practice.

Categories