Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7702089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:08:48+00:00 2026-05-31T23:08:48+00:00

For an MVC3 application I want to create a reusable DAL that is accessed

  • 0

For an MVC3 application I want to create a reusable DAL that is accessed as a service, as this will be used for other projects in the future.

I created all the entities with TT templates and EFCodeFirst in a separate project, then consumed it in a RESTful WCF service.

The structure of this service seems a bit different from other WCF services I have written where I have specified RESTful signatures and optional JSON responses as method decorators in the service’s interface, ie:

    [WebGet(UriTemplate = "GetCollection")]
    public List<SampleItem> GetCollection()
    {
        // TODO: Replace the current implementation to return a collection of SampleItem instances
        return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
    }

    [WebInvoke(UriTemplate = "", Method = "POST")]
    public SampleItem Create(SampleItem instance)
    {
        // TODO: Add the new instance of SampleItem to the collection
        throw new NotImplementedException();
    }

Where this RESTful WCF service (created from the RESTful WCF option) differs is that there is no interface, and I can decorate the service methods with what I need – that’s fine. The service will expose methods like GetUserByID(int id), etc.

The issue is that I want to use this in a MVC3 application, and am not clear on how to hook up the models to my service and would like some direction in accomplishing this.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:08:50+00:00Added an answer on May 31, 2026 at 11:08 pm

    Assume you want to expose an entity called Person. The WCF REST service may look as follows:

    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]    
    public partial class PeopleWebService
    {
        [WebGet(UriTemplate = "")]
        public List<Person> GetCollection()
        {
            try
            {
                IPeopleRepository repository = ServiceLocator.GetInstance<IPeopleRepository>();
                var people = repository.GetPeople();
                // use automapper to map entities to Person resource
                var result = Mapper.Map<List<Person>>(people);
                return result;
            }
            catch (Exception exception)
            {
                // do logging etc
                throw new WebFaultException(HttpStatusCode.InternalError);
            }
        }
    
        /* other methods */
    }
    

    These services can be generated by T4 too.

    Notice that there is no need for an interface on the WCF service itself. I generally do not expose any database entities directly in WCF services as my services evolve differently than my database entities. Once an API is published it should pretty much remain the same. This prevents me from changing my database schema to fit new requirements.

    Instead I map my entities to resources. So Person may looks as follows:

    [DataContract]
    public class Person
    {
        [DataMember]
        public string GivenName { get; set; }
    
        / * more properties */
    }
    

    It may be a good thing to use T4 to generate these as well. Routing is defined something like this:

    public void Register(RouteCollection routes)
    {
        routes.AddService<WorkspaceWebService>("api/v1/people");
    }
    

    To consume it from the ASP.NET MVC project, you can share your resources (aka Person) as defined above as an assembly, or you can use T4 to generate a separate set of resources that are almost the same, but with some additional attributes needed for ASP.NET MVC, like those used for validation. I would generate it, because my ASP.NET MVC view models generally evolve independently to my REST resources.

    Lets assume your REST service runs at https://api.example.com/ and your MVC website is running at https://www.example.com/. Your PeopleController may look as follows.

    public class PeopleController : ControllerBase
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View(Get<List<Person>>(new Uri("https://api.example.com/api/v1/people")));
        }
    
        protected T Get<T>(Uri uri)
        {
            var request = (HttpWebRequest) WebRequest.Create(uri);
            request.Method = "GET";
            request.ContentType = "text/xml";
            using (var response = (HttpWebResponse) request.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    Debug.Assert(responseStream != null, "responseStream != null");
                    var serializer = new DataContractSerializer(typeof (T));
                    return (T) serializer.ReadObject(responseStream);
                }
            }
        }
    }
    

    From your question, I assume you want to use JSON. For that you just have to set the appropiate ContentType on the request and use the DataContractJsonSerializer rather than DataContractSeralizer. Note that there are some issues with dates and DataContractJsonSerializer. The WCF rest service will automatically return XML if the contenttype is “text/xml” and JSON if it is “application/json”.

    Notice that the MVC application has no knowledge of the database, the database entities or its database context. In fact there is no database logic in the MVC application. You will have to pay close attention to security, because the user context is missing from the WCF rest services. But, that is a whole different discussion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I simply want to create a fairly basic REST service, so that I can
Im want to create a data structure for using within my MVC3 application. The
I'm creating an AppHarbor MVC3 application and want to use the standard membership provider
We have a MVC3 application that we have created many small actions and views
In my MVC3 application, I have a view model that I Json encode so
I am implementing a web application using ASP.NET MVC3. In the application I want
I have an ASP.NET MVC 3 application that uses the Ninject.MVC3 extension to setup
I am writing unit tests for a spring mvc 3.1 application that is not
I am building an MVC-based e-commerce application with MVC3 based on nopCommerce. I am
I built a very simple MVC3 application to do a little demo, but I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.