Asp.Net Mvc service layer - c#

I work on a MVC 5 web application. We have two projects, in one project there is a domain model, and everything else is in the other (service layer, controllers, views, etc).
That worked fine until we made a new WebApi project to expose some functionalities.
Now when we need some services from main project in our WebApi we need to copy paste service from main project to new web api project so we have duplicated code.
What I want to know is, is it a good practice to move whole service layer to the new project(dll) and then include it in main application and web api? Should we do this?

Avoid copying/past code, it will not help you in the future if you need maintain this project.
What I want to know is, is it a good practice to move whole service
layer to the new project(dll) and then include it in main application
and web api? Should we do this?
Yes, it is a good pratice and you could create a new assembly (a class library project) in your solution and reference and use it on the web and web api, or more projects you need it (windows, wpf, windows service ... for sample)
Look the image bellow, you could do something like this, separating you Domain Model, the Data Access Layer, Busines Layes (probably your service layer) and UI:

Related

Application Service code in WebAPI

We are starting a new project and trying to implement some concepts from Domain driven design. We are planning to have following layers:
Web Interface (WebAPI)
Application Services (library)
Domain Services (library)
Data Access Services (Library)
We are thinking about merging Web interface and Application service together. So, our webAPI will be talking to repositories, domain model and domain services.
Is this fine or should we have separate project form application services and WebAPI should only communicate with Application services?
Thanks
HTTP should be seen as one of potentially many access ports to reach your application services. If you could be entirely sure that you will never have to speak to your application through another communication channel than HTTP then I'd say it's perfectly not to have a seperate application layer.
However, I'd also say that it's very hard to predict how application needs will evolve and since adding an additionnal layer of indirection to segregate the application layer right away shouldn't be very costly (it's just delegation) that's what I'd do.

Add a WCF service to ASP.NET MVC app and get advantage of existing classes

I'll try to explain the situation in the best way possible, I hope someone out there can guide me a bit.
We have a working .NET MVC 4 app where we've added a service reference to consume a WCF external service.
We've created several classes and methods to translate our data model to service's one, so we can call it's different methods from within our app using our data model.
Now we have a new requisite: to add our own service, which can be accessed from outside our environment, and who has to replicate some of the methods of the original service.
The idea is that users will post info to our environment through our service, we'll apply several treatments to the info, and then post it to the original service.
So my question here is: How do I create and attach the new service to my MVC app so I can get advantage of the classes and methods that are already developed and the data model of the original service?

Prism + Modules use WCF as dataaccess layer

I am writing an application ( WPF ) where i use the prism framework. I have a client with the shellview.
I have some module's. So that are different projects. Each module is a sepperate project.
The modules are loaded by Unity.
I have the following
Solution
Client
Modules
Client ( app )
Common
Entities
Server
Data
Web
So you can see i have an Data Project under the server folder. There you can find the edmx.
The Web project is empty, the Entities project is also empty.
The Client (app) has a shellview with its bootstrapper.
The client works. Only the data access layer must be integrated.
What is the best solution to do the data access? WCF, something else?
What template do we need to use for the entities. How can we use the service in the modules where al the code (View/ViewModel) is?
Pff, i am looking and read so much.
Found some topics on stackoverflow. But none of the topics start's from the begin.
Hope someone can help me.
I suggest you to generate POCO or STE on .edmx and use the Repository Pattern for database access. Create a Separate WCF Service Project and call these repositories.
Your Prism Solution will have a Proxy Project with Static Class to return Service Object and your Client (Prism) App will call the WCF Services.
May be you can have a look at Calcium SDK (http://calcium.codeplex.com/) which leverages Prism modular app development along with support for WCF services.
If you are familliar with WCF and EntityFramework on the server side then this is the way to go.
In order to reuse the generated proxy classes from services you can keep your service references in a common assembly that you reference from all modules. Something like "Infrastructure" is a good naming convention.
In case one of your modules needs a unique functionality then you put the service reference on that project.
In case of WPF clent: Use entity framework code first and seperate your Model classes in a seperate assembly so you can reuse them in your main client and modules.
In case of silveright client:
If duplicate model classes and namespaces when generating the service proxy is an issue for you you can checkout WCF RIA Services. Always keep in mind that you can link two assemblys a silverlight and a .net one using RIA Link in Visual Studio project file properties. Then any server side code file you chose can be reused on the client. This is possible using the "shared" suffix in your filename (ex: enums.shared.cs) regardless if you are using WCF services or not. You can find out more here and here
Hope this helped.

wcf with asp.NET application

What is the best experience of combining wcf services in an asp.NET application?
Should I add wcf services files to the asp.NET project?
Should I create a separate project for the wcf services and refer somehow to them in the asp.NET application?
I presume you have an ASP.NET application that also hosts a few WCF services that can be accessed from the client. There is nothing wrong with this and it works perfectly well.
The three things you should consider doing are:
separate the interfaces for the WCF services into their own assembly
keep the data objects that you use in yet another assembly
separate the implementation of the interfaces into their own assembly
This may seem like overkill, and it's not always necessary to do it this way, but i would do at least steps 1 & 2. Keeping the data objects and contracts (interfaces) in their own separate assemblies is important as it helps when sharing (referencing) the data objects from different projects (i.e. if you have a Silverlight or Windows Forms/WPF client that can consume those assemblies).
Separating out the implementation (step 3) is good, but not the most important thing to do. If you do follow this step, you can still publish the WCF endpoints through your ASP.NET application.

Can an asp.net mvc application also have a web service?

I have an asp.net mvc application and now I need to add a web service to go along with it. What is the best solution for this? Just create a new web service project and add it to my solution then deploy it to the same web server using a different url? I would like it to be a part of the mvc application only because I have all my database code in there.
Any suggestions would be appreciated. Thanks.
There's no reason not to add a web service project.
You state that all your database code is in your MVC project. I strongly recommend you remove it from there into a separate class library project. This third project would be used both by the web service and by the MVC application.
I also strongly recommend that you not use ASMX web services for any new development.
Use WCF only, unless you have no choice at all. There's a misconception that WCF services don't do SOAP - they do, and WCF has replaced ASMX.
Web service could mean a soap based web service or a RESTful web service. I can't think of any reason why you would not be able to simply add an asmx file to your project and be in business. That is the soap based route. If you want to be really cool though you can simple return xml from a controller action and implement a RESTful solution right over the MVC framework.
If you want to use a regular ASP.NET asmx web service, it's certainly possible. Here's an example from Scott Hanselman that does just what you are asking about and it throws in some other ASP.NET technologies for good measure.
All you have to do is File -> New Item -> Web Service and it should work like a regular ASP.NET application in your Mvc project.
i think there's a couple of things here.
you can indeed add a web service to an MVC application. you may even consider identifying the web service(s) as a script service to make REST like operations easier to perform via javascript. this may not be necessary due to your circumstances.
i think there is a stronger question as to the underlying architecture. If you are placing the web service withing your mvc application, because, your database code is already there...should it be? it might be a good time to abstract your data layer out a little. However, if you're dealing with a relatively small project and don't need the flexibilty, then certainly, add a web service right in. i guess what it really boils down to is addressing the true needs of your application.
MVC is built on the asp.net framework. You should be able to include a web service within the same project. I haven't done it but I know that you can combine asp.net forms applications with MVC applications in the same project. The same should go for web services.
Unless your application is very small I would recommend you create different projects for each logical part of the application. A good staring point is having a project for each of these:
Domain objects
Data access
Web Services
UI (your ASP.NET MVC app)
This provides a clean separation corresponding to your architecture and supports reuse.

Categories

Resources