Entities in MVC and WCF - asp.net-mvc

I am working in an MVC web application. This application also use WCF service and Entity-framework.
In solution explorer I have two projects 1. MVC 2. WCF Service.
I am little bit confuse how should I use entities in MVC and WCF service.
For instance I have created Employee.cs class in Model folder in MVC.
And in controller I call a method of Get Employee() of WCF service which returns employee class type.
In WCF service how I will return as employee type because I added that class in Model folder of MVC project which is not accessible to MY WCF service project.
So in this case how I should use that entity in such way that it can be accessible to MVC as well to my WCF service.
Please let me know you need more clarification..

To share the same classes between the projects,create another class library and and the shared code in there.
Then reference the class library from both projects.

Related

Automapper Configuration and Dependency Injection in ASP.NET Core for mutliple Apps

Excuse me for this lengthy explanation, this to mention all details of my problem.
I am using .NET Core and want to know where is the best place I can put the AutoMapper Configuration? From where should I call it? And what is the best way to inject common services to several ASP.NET Core apps.
Here is the situation right now, I have the following:
Angular2 Application (client side)
Resources API (ASP.NET Core APP for APIs)
Authentication API (ASP.NET Core App for APIs)
Service Layer (Class Library)
Repository Layer (Class Library)
Core (Class Library)
In the repository layer, I have my Entity Framework classes (EntityModels Folder), and I have my implementation of Repositories where I use EF to get the data. The mapping is the happening at this level:
public User Get(int id)
{
using (var securityDb = new SecurityContext())
{
var record = securityDb.Users.Where(u => u.UserId == id).SingleOrDefault();
return AutoMapper.Mapper.Map<User>(record);
}
}
In the Core project, I have my domain and dto classes which are returned from Repository layer to the service layer.
Both Services and Repositories project reference the Core Class library.
Now, I should register all the dependencies, this is the "ConfigureServices" Startup Method in Authentication API Project where I am configuring AutoMapper by calling the Mapping class method.
services.AddTransient<SecurityCore.RepositoryContracts.IUserRepository, SecurityCore.Repositories.UserRepository>();
services.AddTransient<SecurityCore.ServiceContracts.IUserService, SecurityCore.Services.UserService>();
//Automapper configuration
SecurityCore.Repositories.EntityModels.Mappings.Configure();
I have the mappings class in the repositories project as in the image above:
public class Mappings
{
public static void Configure()
{
AutoMapper.Mapper.Initialize(cm => cm.CreateMap<EntityModels.Users, Models.User>());
}
}
This is some of my questions:
Should I create a separate project that bootstrap all the mapping configuration, but this means that this project should reference both Core and Repositories project (instead of having the mapping config in Repositories)
Should the automapping configuration be made at the ASP.NET Core
as in my case?
What about injecting dependencies code which is duplicated in both of ASP.NET Core projects, how I can separate the bootstrapping process? Should it be called once in every Application?
I cannot say that I am an architectural expert and I’m not sure what best practices are concerning how granular our projects should be in the “.NET Core era”.
However, here is a sample project in which I am in the process of porting existing code that implements AutoMapper and Autofac to a new ASP.NET Core MVC solution.
My sample code places AutoMapper mapping interfaces in business logic layer(BLL) libraries which includes the domain models. Implementations of those interfaces as well as configurations(AutoMapper Profiles) are in separate projects in the data access layer (DAL). This arrangement facilitates swapping out the mapping engine without touching the BLL.
The configurations are executed during AutoMapper initialization, which in an ASP.NET Core solution are called via Startup.ConfigureServices().
Please watch this post and my sample code for updates to my AutoMapper and Autofac implementations.
I assume that you are putting your repository interfaces into the same BLL library with your models and implementing those repository interfaces in your DAL. This is what I understand to be a “best practice” and will enable you to change object relational mappers (ORMs) without touching the BLL.
I suggest the following changes to your code:
refactor mapping logic from your repositories into a separate project in your DAL (Single Responsibility Principal)
configure mappings for each domain model in classes derived from AutoMapper.Profile
perform mapping between domain models and data transfer objects (DTOs) in your service classes
initialize and configure AutoMapper once, via your startup project
As for dependency injection (DI):
DI container is instantiated once via your startup project
Services are injected via class constructors
The following articles on DI are worth reviewing:
docs.asp.net/en/latest/fundamentals/dependency-injection.html
wildermuth.com/2016/08/07/ASP-NET-Core-Dependency-Injection
docs.autofac.org/en/latest/integration/aspnetcore.html

Host MVC and WCF in same appDomain

Is it possible to host in the same appDomain two different projects of WCF service and MVC application?
For example, I need to use some pulic static class from MVC application by my WCF service. But they are in different projects, so this class for them is in different appDomains.
It is quite common to host WCF services within an ASP.NET application (MVC or WebForms). From a coding perspective, the only thing you may need to configure is ASP.NET compatibility mode, if the code WCF is calling requires access to the current HTTP context.
Create your WCF service contracts, and implementations of those contracts, configure your services, bindings, and behaviors in the web.config, as you normally would. Here is another example, which you could create in virtually any ASP.NET application.

Using WCF with wsHttpBinding with Web Forms vs MVC

I have used WCF in the past with my Webforms so my solution was
MyWebformApp = WCF(Model+business) + Web Forms
So when I want to work with the MVC for presentation arch. How do u use WCF with ASP.net MVC ?
Are your data contracts a part of the model ? How do you register the datacontracts as properties?
Roughly here are a few steps:
Create a proxy of the service using svcutil.exe and include it in your application
Create an interface which will abstract all the necessary methods you need to call from the application (IRepository)
Implement this repository and call your WCF service (work with the generated client)
Inject the repository into the controller constructor
In the meantime think about the view models you would set and the mapping between the objects coming from the web service and those view models.

When Creating a WCF service

I have already a MVC-webapplication, I wounder should I create a new WCF Service Application or should I insert a service in same webapp under the folder "Model"?
What is the best solution?
If the WCF service is exposing models from your MVC project, I would stick it it the same project. If it has logic pertained to code outside the MVC solution, I would create a separate application.

Integrating ASP.NET-MVC with Silverlight using WCF and Ninject as IoC/DI

I have a prototype ASP.NET-MVC website which uses Ninject as a IoC container. All service-classes and repository-classes used by MVC Controllers are properly injected by Ninject. This is great.
The next thing I need to add there is Silverlight (version 3 to be more precise).
Silverlight will be connecting to my server using WCF service, hosted in ASP compatibility mode, to the same ASP.NET-MVC website.
What Silverlight needs is to 'download'/'get' a kind of ViewModel using WCF (the better name would be Client-Side Model). This is also possible - I imported WCF service and setup all security-related xml configuration files.
Here is the stuff I want to know....
Is that OK that model returned by WCF service is rather complex and includes arrays and inheritance (at array items' level)... or maybe there is another and better way to send it from server to client?
At Server-Side for regular asp-mvc stuff all service-classes used by controllers are injected by Ninject. How to inject services for WCF-service classes?
Do WCF service has an access to HttpContext.Current.Items? I need to grab from here logged User Id and a few profile-related data (regular forms auth. stuff).
EDIT
Ad 3. It's possible enabling AspNetCompatibilityRequirements
Has anybody ideas for point 2?
For the 1st Question.
Yes it is okay to return a complex structure, provided you have explicitly applied the '[DataMember]' attribute to each and every needed property of the Object/s.

Resources