How to share DbContext between different WebApps? - dbcontext

I've one missing link to understood how it could work to "connect" my EF core's DbContext to different applications in following scenario:
I've got one application project which contains the whole EF Core Code first datamodel (Entities, Business Logic, Enums etc).
Until now we're using a WebAPI application to share data with multiple endpoints.
So the data we're exchanging now are quite static at the moment. We create our DbContext and the API is using it for data reading and write operations.
Now there is customer request to also build a WebUI (Blazor). So we still want to use the WebAPI for data exchange in the UI, but further we need some more business logic elements from our data model (containing INotifyPropertyChanged event handling etc). We implemented the event handles in the entity classes. So we intend to build a session object for each user/client which contains the DbContext instance. But how can handover an instantiated DbContext from the Web API to my UI Application.
Would be nice to get some input. Thx for any advice.

You do not pass the DBContext object to "UI Application", such as a Blazor app.
You did not mention what flavor of Blazor you are using... However, the following may be applicable for both Blazor Server App and Blazor WebAssembly App... From what I've gather from your question you are exposing Web Api end points to retrieve data and save data. This is good. What is left for you to do is to create code in your Blazor that access those end points. You should use the HttpClient service to do this. If you use Blazor client side, this object is configured and made available for injection in your components and services. This type of HttpClient is based on the JavaScript Fetch Api.
If you are going to create a Balzor Server App, then you'll have to add the HttpClient service to the DI container and configure it accordingly, and only then you'd be able to inject it into your components and services. In this case you should use the IHttpClientFactory to provide the HttpClien onjects.
In principle, you have to query your Web Api methods via HttpClient.
Note that if you use Blazor Server App, you don't have to use Web Api. Instead you can create services that perform similar functionality to that provided by the Web Api.
Please, go to the docs and acquaint yourself how to use the HttpClient with Blazor apps.
Hope this helps...

Related

WCF Data Service vs WebAPI

I have been catching up with MVC4 Web API and WCF Data Service. From the surface, they both seem to be able to work with oData in the consuming client. I wonder which one is better for separation of concern (separate data service layer from UI layers). In my current solution, I have a plain MVC 3 style Intranet project and a MVC Data Service project. The 1st project has a Service Reference to the 2nd project. My goal is to write the data service once and make it available to all projects that would need to access underline database. When I read about Web API, it seems to me that the ApiControllers can return oData compliant result to the consuming client without MVC Data Service. My confusion is how I am going to expose this Web API MVC project as a service endpoint. Should I wrap it in WCF? Thanks.
You just create a controller inheriting ApiController and decorate one action with [Queryable] attribute, Rest depends on your route setup. Easy-Peasy.
Which one better? Web API. Since it has all the goodness of HTTP and you are not restricted to ATOM format.
You may use PocoHttp for seamless access of the data from your client.

Where to Use WCF in a Big Web Project

We are planning to develop a big application in web, our current plan is to create a WCF service for each class in the Business layer. Is this effective? I want to know where can we use WCF in a big web application and main advantages of using it
we have these projects
1) MVC3 with Razor that handles UI
2) Class Library Project that communicate with Database
3) WCF Project
Method we using
1)Creates UI and Model in MVC
2)Makes dll that communicate with UI (eg:Save function/Update function) using ClassLibrary Project
3)The Class Library Created is Added (Added to Bin) in WCF Project
4) Builds the WCF and Host It in Server
5) The Hosted Service is Used in MVC Project for Communication with DB
Our Technical lead is saying it is light weight and more secure but i am wondering why he is saying to use WCF for whole appication
First ask this question, why do you need a service layer, when it could be achieved by a separate application. If you want to achieve this for just loose coupling, then it does not makes sense.
In my opinion, the service layer would be useful for exposing your data (dal-layer) and not Business Layer. For example take twitter. Twitter exposes it data over Web Services. What you do with the data is completely your interpretation. The data could be consumed by many application over HTTP. So your client could be remotely situated and need not to be on the same server as your application. The business layer could change based upon interpretation of data. The consumer, then need to worry about latest changes and including latest DLL. It will just consume the webservice. Also mocking your service would be pretty easy and you could write your NUnits tests very easy on your Business layer then. I would also recommend you to have a look at ASP.Net Web API, which provides restful way of exposing your services and data(with inbuilt capablity of exposing it as JSON). The RestFull service has many advantages over WCF, like you then need not to worry about WSDL and etc. The interface always remains same. Consuming a RestFul service is very easy.
As again with the twitter example. The clients consuming twitter api's are not situated on same server. So it makes sense to expose it over HTTP.
But if you do not have such a requirement(Client is not remotely situated), then exposing your data over web services does not make sense. Then a separate application will give you loose coupling and that should be good enough for you. Also exposing data on HTTP will have some performance impact too.
UPDATE 2
I understand the scenario you are trying to implement and I think its perfectly allright. By light weight he means, from your WCF service he would do interaction with Database and then send light weight DTO's for each action and controller as per requirement. So basically your WCF would do nothing but invoke methods from your class library, would fetch some Business Object and Convert it back to DTO(as required by your action to render UI) and send it to Controller.
Also make sure, you use Dependency Injection for your Services, so that you could write NUnit tests for your controller with mock data and hence no requirement of Database for running your NUnits
My preference is to have the service layer be a completely independent application that can be consumed by many applications. It makes projects, especially large ones, much easier to scale, than if you were to create your service layer inside a single web application.
Unfortunatly to tell you. Using WCF is less effective. The main purpose to use WCF it to let your data can be accessed with any client application. not just web application. you may also have a Silverlight or winform. WCF has nothing to do with "A BIG" web app.

Should we use a WCF service as our service layer facade in nTier application

So we've decided to rebuild an application in our business since it's been sitting in Sharepoint for no apparent reason other than to make use of its document indexing feature.
We've decided to create our new app in ASP.NET MVC3 using C#. We're trying to decide on the overall architecture.
I was thinking something like the following:
Core - domain objects (poco's)
Data - Entity Framework (Code First) or nHibernate exposed as Repositories
Service - This layer would encapsulate any business logic and act as a facade. This could be broken down into further modules.
UI (MVC) - Controllers and Views.
This would all be tied together using a DI container such as Autofac.
We also want to be able to write unit tests so we need to be able to mock our service layer and data repositories for testing our controllers etc.
So - does the above sound like a good overall architectural pattern for a pretty standard business application?
The idea being that data, service, ui can reference Core but the UI would only really talk to the service level components and not know about the implementation details of data etc.
My next question is that at some point we're going to want to expose some functionality outside our application i.e. WCF Services/ASP.NET Web API.
What, in your view, would be the best option. Build the service layer in WCF and call this from our Controllers in MVC? If so would this be testable or would we need to write a wrapper around the web service? Could this be time consuming?
OR
Continue writing a service layer (i.e. Service1.CreateObject(object obj);) in C# classes and create a web service as a separate entity exposing just the functionality we need that calls our service layer?
Any thoughts would be really helpful as I don't know what best route would be.
Should we use a WCF service as our service layer facade in nTier application
Depends on if any other application than the MVC application is going to talk to the service.
MVC3 is the only app: You aint gonno need it
Other apps too: Sure. Do it.
What, in your view, would be the best option. Build the service layer in WCF and call this from our Controllers in MVC? If so would this be testable or would we need to write a wrapper around the web service? Could this be time consuming
Don't use the concrete service classes. Use the service interfaces. Problem solved.
My next question is that at some point we're going to want to expose some functionality outside our application i.e. WCF Services/ASP.NET Web API
I hope that you mean the same WCF service.
Continue writing a service layer (i.e. Service1.CreateObject(object obj);) in C# classes and create a web service as a separate entity exposing just the functionality we need that calls our service layer?
ehh. What kind of method is Service1.CreateObject(object obj)? That looks just wrong.
Using WCF Service is the right approach. (As you need to host this as web-api and web-api is over http).
In your mvc application you can consume the service by endpoint url.
Time factor depends on the connectivity between your servers(WebServer for MVC app and the Server to host WCFservices).ideally it should not be a bottle neck.
Still you can do unit testing of MVC code (as service layer call can be mocked.(using Moq/NMock?RhinoMock...))

Are WSDL's like spring dispatch servlet?

im trying to understand where WSDL's fit in, in a typical web service backend application. i am coming from a Spring background and in my experience so far, in Spring, each url request gets mapped to a specific controller class via a dispate servlet running in the web container. you can specify which url matches a given controller via xml config or from annotations.
is using a WSDL the same thing as using an xml config file to map url requests to java objects?
Thanks in advance. im moving from Spring to standard j2ee/EJB3.
WSDL is just a description of Web Service interface, most Web Service systems generate those descriptions on fly like for example when you create asmx web services you can generate WSDL on fly by typing http://yourhost/yourwebcontext/yourwebservicename.asmx?wsdl and it will return you the description of that web service. Then you can use a tools that generate stub proxies for coding using those descriptions automatically, for example in Visual Studio when you add an Web Service Reference those operations are done automatically
No, WSDLs are not like a dispatch servlet.
A WSDL file is a description of a web service (SOAP, REST, etc.). A WSDL can (theoretically) be used by anyone to generate executable code which consumes the web service described by that WSDL.
From the WSDL tag info:
"WSDL" stands for "Web Services Description Language." It is an XML language used to describe a web service to code that wishes to consume it. It describes the messages sent and received, the possible faults, and the communication and security requirements.
From WSDL Essentials:
In a nutshell, WSDL represents a contract between the service requestor and the service provider, in much the same way that a Java interface represents a contract between client code and the actual Java object. The crucial difference is that WSDL is platform- and language-independent and is used primarily (although not exclusively) to describe SOAP services.
Using WSDL, a client can locate a web service and invoke any of its publicly available functions. With WSDL-aware tools, you can also automate this process, enabling applications to easily integrate new services with little or no manual code. WSDL therefore represents a cornerstone of the web service architecture, because it provides a common language for describing services and a platform for automatically integrating those services.

When to use WCF and when to use ADO data services in ajax websites?

I am really confused about when to use WCF and when to use ADO data services in my website ajax calls.
I always make my javascript code call a webservice to get data from server "ajax".
But while reading in both WCF and ADO data service, i am not sure when to use each and when not use?, and if they replace each other in my case? or can live side by side?
Can anyone make me understand in points when to use which in ajax websites?
ADO.net Data Services is a library to expose a datasource thru Wcf.
As such the functionality exposed by an Ado.net Data Service is for reading, updating, creating and deleting records in that DataSource.
With Wcf you can expose any kind of functionality (that's why an ADO.net Data Service is a WCF service)
So to answer your question, if your client application needs direct access to the datasource then Ado.net Data Services will provide that functionality out of the box.
If the client needs to talk to a Business Layer which in turn will access that Data Access Layer then you will expose that Business Layer as Wcf Services.
You could well use both approaches in the same application:
Exposing some tables directly using ADO.net Data Services
Exposing Business Logic with a WCF service
In a banking application for example you would not expose the Account table because you want to enforce some business rules.
In a simple ToDo list application you could expose the Tasks table using ADO.net data services because there is no business logic to be applied (note that I said SIMPLE ToDo list app)

Resources