MVC :: How Should Use The Service/repository Pattern

Dec 8, 2010

i'm new to asp.net MVC, and I'm trying to understand the service/repository pattern and how to best implement it.

I've followed the MVC Music store tutorial, and it suggests that the public partial class ShoppingCart implements an AddToCart method looking like this:

[Code]....

Now if I would like to use the service/repository pattern in a correct way, should I just replace the row "storeDB.AddToCarts(cartItem)" with something like cartService.AddToCarts(cartItem) and then just save the added row by calling cartService.Save() instead of shopDB.Save()? The methods AddToCart(...) and Save() in cartService then calls the repository that does the actual saving.

View 3 Replies


Similar Messages:

The Model-Repository-Service-Validator-View-ViewModel-Controller Design Pattern

Jan 19, 2010

When I first heard about ASP.NET MVC, I was thinking that would mean applications with three parts: model, view, and controller.

Then I read NerdDinner and learned the ways of repositories and view-models. Next, I read this tutorial and soon became sold on the virtues of a service layer. Finally, I read the Fluent Validation documentation, and I'll be darned if I didn't end up writing a bunch of validators.

Tonight, I took a step back and thought about what had become of my project. It seems to have become the victim of the design pattern equivalent of "feature creep". Somehow I'd gone from Model-View-Controller to Model-Repository-Service-Validator-View-ViewModel-Controller. You want loosely coupled and DRY? We got your loosely coupled and DRY right here! But I'm wondering if this could be a case of too much of a good thing.

Am I right to be concerned? Or is this actually not as crazy as it sounds? On one hand, it seems crazy to have so many layers. On the other hand, every layer has a clearly defined purpose that makes sense to me. Have your MVC applications turned into MRSVVVMC apps too? If not, what do they look like? Where's that right balance?

View 2 Replies

Pattern For Retrieving Complex Object Graphs With Repository Pattern With Entity Framework

Oct 13, 2010

We have an ASP.NET MVC site that uses Entity Framework abstractions with Repository and UnitOfWork patterns. What I'm wondering is how others have implemented navigation of complex object graphs with these patterns. Let me give an example from one of our controllers:

[code]....

It's a registration process and pretty much everything hangs off the POCO class Person. In this case we're caching the person through the registration process. I've now started implementing the latter part of the registration process which requires access to data deeper in the object graph. Specifically DPA data which hangs off Legal inside Country.

The code above is just mapping out the model information into a simpler format for the ViewModel. My question is do you consider this fairly deep navigation of the graph good practice or would you abstract out the retrieval of the objects further down the graph into repositories?

View 2 Replies

Can The Repository Pattern Work Well With Webforms

Mar 8, 2011

I've been using MVC for the last year and unfortunately I am stuck adding features to an existing web forms site. The site makes heavy use of inline SQL and it is kind of all over the place. Using an ORM is not going to happen either and wouldn't address the problem of keeping queries all in one place.

Can the Repository Pattern and Service layers also work well with classic asp.net web forms?

View 2 Replies

Splitting Out Membership And Using The Repository Pattern

Feb 15, 2010

I am building an application using asp.net mvc, DI, IoC, TDD as a bit of a learning exercise.

For my data access I am using the repository pattern. Now I am looking at membership and how this can work with the repository pattern. I am currently using a Linq to Sql repository but don't want to be tied to SQL Server for membership.

Secondly, I am looking to split out membership into a number of services:

AuthenticationService - identify the user
AuthorizationService - what can they do
PersonalizationService - profile

The personalization service will be what really defines a "customer" in my application and each customer will have a unique id/username that ties back to the AuthenticationService - thus, allowing me to use the default ASP.NET Membership provider, roll my own or use something like Open ID.

Is this a good approach? I don't want to reinvent the wheel but would rather these important parts of my application follow the same patterns as the rest.

View 2 Replies

C# - Class Encapsulation With Repository Pattern?

Oct 18, 2010

I am sort of using a repository pattern to extract information from a database. I have two classes, report and reportRepository.

The trouble I have is that since reportReposity has to fill in all the details for the report object, all the members in the report have to be publicly accessible.

Is there a way so that I can ensure that only the repository class can access some of the methods of the report class and only it can set some of the properties that other classes cannot, sort of like what friend does in c++. Or is there a completely different way of handling this situation?

I am using C# in ASP.NET 2.0

View 2 Replies

Architecture :: Repository Pattern - Pros And Cons?

Jul 8, 2010

This is possibly the worst kind of religious debate -- a religious debate with practical consequences. But it's one that needs to be had, and I can't seem to fit it in my tiny head. Here are the pros and cons of the pattern as I know them:

Pros:

-Encourages DRY (don't repeat yourself) design in that identical queries are written only once per set of query conditions
-Facilitates unit testing by allowing itself to be abstracted into an interface
-Creates an opportunity for business-level validation

Cons:

-Breaks DRY philosophy in that you're generally repeating your database schema
-In a sense breaks separation of concerns, because the query concerns of the controller and view frequently become the concerns of whoever is maintaining the repository
-Determining what should be a repository and what should be returned as a raw associated ORM entity becomes an ambiguous art

To me it seems like all this stuff should be done at the ORM level, but Entity Framework has much fewer hooks than Linq to Sql does, yet Entity Framework tends to be regarded as being more robust, so it seems that this is by design, and that the designers of EF are in fact encouraging another layer. Are there any tools or anything that I could be using for this? Am I missing something?

View 6 Replies

Architecture :: Data Validation In Repository Pattern?

Dec 21, 2010

I am currently using the 3-tier Repository pattern in my application. Actually it's the first time for me to implement a design pattern at all! i used to put all my code in the so called now presentation layer.

i want to implement data validation, for example, password should not be more than 10 characters and have to contain special characters. Should i put this code in the data access layer? but my data access layer contains methods that take the DTO as a parameter for example

[Code]....

and the same is for other CRUD operations (DELETE and UPDATE), so implementing such validation on the DAL would make me duplicate the code in each and every method that accepts the DataObject as a paramter. Same holds for the business logic layer where i am using it as a proxy between the presentation and the data access layers.

Eventually it has to use the same Data Objects as parameters. This only leaves me with one option which is to do the validation on the Data Object part. But i think this is not the essence of the respository pattern which states that the Data object class should only be a "container" class with no behavior.

View 1 Replies

MVC2 LINQ - Repository Pattern, Where Should The Pagination Code Go

Nov 2, 2010

I'm working on adding an HtmlHelper for pagination, but I am unsure where the proper and/or most beneficial place to put certain parts of the pagination code from a performance and maintainability standpoint.

I am unsure if the Skip(), Take() and Count() portions of Linq to SQL data manipulation should live within the repository or the controller.

I am also unsure if their order and where they are used affects performance in any way.

If they live within the repository from my understanding this is how it would work:

1. I would pass the pageIndex and pageSize as arguments to the repository's method that grabs the data from the database.
2. Then grab the full data set from the database.
3. Then store the count of TotalItems of that full data set in a variable.
4. Then apply the Skip() and Take() so the data set retains only the page I need.
5. Display the partial data set as a single page in the view.

If they live in the controller from my understanding this is how it would work:

1. I would grab the full data set from the repository and store it into a variable inside of the controller.
2. Then get the count of TotalItems for the full data set.
3. Then apply the Skip() and Take() so the data set retains only the page I need.
4. Display the partial data set as a single page in the view.

Inside the controller (I realize I will incorrectly get the page count here and not TotalItems):

[code]....

View 2 Replies

Using Multiple ObjectContexts In Entity Framework 4 With The Repository/uow Pattern

Jan 17, 2011

i am using EF4 and StructureMap in an asp.net web application. I am using the repository/unit of work patterns as detailed in this post. In the code, there is a line that delegates the setup of an ObjectContext in global.asax.

EntityUnitOfWorkFactory.SetObjectContext(() => new MyObjectContext());

On the web page code-behind, you can create a generic repository interface like so ...

IRepository<MyPocoObject> ds = ObjectFactory.GetInstance<IRepository<MyPocoObject>>();

My question is what is a good approach to refactoring this code so that I can use more than one ObjectContext and differentiate between them in the code-behind? Basically i have two databases/entity models in my application and need to query them both on the same page.

View 1 Replies

Architecture :: Provider Model Versus Repository Pattern

Feb 4, 2011

I am trying to understand the fundamental differences between the Provider Model and Repository Pattern.

I have used the Provider Model in many many situations and am confident with it when designing applications. However, the more examples I encounter on the internet and asp.net evolution I keep coming across "Repository" Interfaces for classes that look like a Provider Model.

I have dug around a bit but all I can see is that they kinda do the same thing, or closely overlap by enforcing an inheriting class to adhere to a "contract" of implemented / abstracted methods etc...is there more to it?

View 1 Replies

Architecture :: Should The Unit Of Work Point To The Service Layer Or The Repository And Then The Repository Point To The Service Layer

Jan 26, 2011

Just wondering, in an ASP.NET MVC3 environnement with entity framework. Should the Unit of Work point to the service layer or the repository (and then the repository point to the service layer) ?

Ive saw two example:

* One where the unit of work and repository both have an instance to the service layer..

Link: Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable

Doesn't use a service layer but its obvious that one could be use in that case.

* Second where the unit of work have an instance to the repository which have an instance to the service layer..

[URL]

What would be better ?

View 11 Replies

Should The Unit Of Work By Used By The Service Layer Or The Repository

Jan 26, 2011

In an ASP.NET MVC3 environnement with entity framework. Should the Unit of Work point to the service layer or the repository (and then the repository point to the service layer) ?

Ive saw two example:

* One where the unit of work and repository both have an instance to the service layer..

Link: Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable

Doesn't use a service layer but its obvious that one could be use in that case.

* Second where the unit of work have an instance to the repository which have an instance to the service layer..

[URL]

View 16 Replies

Design Pattern For Adding A Web Service In MVC3

Mar 4, 2011

Firstly - I'm not asking this question How to include a web service in MVC3? I know where the button is :-)

I'm trying to find an example of best practices of how to use the new DI / Common Service Locator framework in order to make web service calls (and code dependent on web service calls) testable. I've no experience of using NInject or the like - is that the way to go?

View 2 Replies

Using Jquery To Call A WCF Data Service From The UI Violating The MVC Pattern?

May 26, 2010

I'm fairly new to ASP.Net MVC 2 and understand the MVC pattern in itself. But my question is what's the best way to populate dropdownlists in the UI sticking to the MVC pattern. Should I be going through the controller? Every article I've seen to do this shows how to do it using javascript and jquery. I have a test application that I'm re-writing in MVC2 I have my dropdowns working with jquery basically calling a WCF Data Service that returns JSON which populates the dropdowns. Seems to me though that this is bypassing the controller and going straight to the model therefore strictly violating the MVC pattern. Or am I missing something obvious here. You thoughts or best practices would be greatly welcome here.

View 3 Replies

MVC :: AsyncController Gripes / Working Pattern For Converting A GET-POST-GET Pattern To Asny?

Feb 23, 2010

Does anyone have a working pattern for converting a GET-POST-GET pattern to asny?

I'm encountering the following issues:

1. You cannot mix Sync and Async action methods SubmitForm(), SubmitFormAsync(bool? confirm), SubmitFormCompleted() ... (because the resolver gets all confused ... it doesn't use the HTTP verb to decide who to target. BTW: I think that's poor design, or a bug)

2. Renaming the get method name to something else eg: SubmitFormConfirmation(), SubmitFormAsync(bool? confirm), SubmitFormCompleted() would be very awkward if it works ... because you have to doctor the <form markup to specify an action name.

3. You cannot give them all async names SubmitFormAsync(), SubmitFormAsync(bool? confirm), submitFormCompleted(), because the call just keeps malfunctioning. It sometime even behaves as if you are requesting a delete of something.

Can someone give an insight from an actually working sample.

View 5 Replies

Best Design Pattern For Associating Subdomain With Area And PRG Pattern?

Aug 21, 2010

Now that the next version of ASP.NET MVC is being prototyped and previewed (ASP.NET MVC 3 Preview 1 came out a couple of weeks ago), I wonder if we should call the attention of the Core Dev team (S Hanselman, Phil Haack and all) to this "feature."there a easy/non tacky way of associating subdomains &#8594; areas?Something like:
[URL]Also, whats the best accepted design pattern in implementing PRG pattern in ASP.NET MVC? I guess it should also get some official loving in MVC 3.

View 2 Replies

MVC - BLL And DAL To Repository Design?

Feb 8, 2011

We are moving from ASP.NET Web Forms to MVC 2.0. In most of our projects we have a typical setup to communicate with a database.

Common (objects/entities like 'SiteMenu' and 'Users')

Business Logic Layer (with calls to de Data Access Layer)

Data Access Layer

The DAL has a DatabaseHelper with common database operation, an OdbcHelper with database specific operations (eg MySQL) and a StoredProcedure class with all the stored procedures.

How is this design translated into a repository design? We want to use our own database helpers instead of NHibernate etc.

View 1 Replies

MVC :: Using More Than One Repository In The Same Controller?

Nov 21, 2010

I have 2 repository that I'm trying to use in the same controller and cant find out how

This is what I'm tried :

[Code]....

for the first one it was working(VortRepository but I cant add the second )

View 5 Replies

ADO.NET :: Trying To Create A Data Repository And Using L2S For The ORM?

Feb 7, 2011

i am trying to create a data repository and i am using L2S for the ORM.

I have created a stored procedure called sp7DayAnalysisByStock which accepts a string parameter and returns a recordset of data rows. iThe result is a set of PriceList objects which is already available in the dbml.

What i now want to is create a data repository class with the signature below;

public IQueryable<PriceLists> Get7DayStockAnalysis(string stockname)
{
}

it seems what is being returned is ISingleResult..How can i return EITHER IQueryable<PriceLists> or any IList?

View 5 Replies

MVC :: Setting Up Repository Without LINQ?

Mar 4, 2010

I'm used to working directly with SQL and do NOT want to use Linq.

Pretty much I always create a class like the one below and have all the functions/methods necessary to perform a task.

How would I set up a repository without using LINQ??

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;

[Code]....

View 5 Replies

Single Repository Or Multiple Repositories?

May 1, 2010

In a typical business application, a session is started and persisted across several pages. It is commited only when the transaction is complete. A good example of this is a tax preparation application. Once the session is started, it is persisted through session, cookies, or both. Nothing is written to file, or database, until the entire profile is complete and the refund/return is calculated. In such an ennvironment, is makes a great deal of sense to work with the structure imposed by domain driven design, and using a single repository to simple commit the session. However, there are times when this doesn't translate correctly, even when domain driven design is used.

An example of this is my forum project. While the entities themselves are good targets for domain driven design, I am not sure about the repositories. The basic structure is that a Category has many Forums, a Forum has many Threads, and a Thread has many posts. There are other things in there, but that's enough to describe what I want to get across. If a user has navigated to /Thread/Edit/42, and they have rights to edit it, all I am concerned about is fetching that record and displaying it. On postback, all I want is to be able to save it..................

View 2 Replies

Trying To Create A Repository Class For Each Table

Dec 17, 2010

I am trying to create a repository class for each table. For example I have TableA, TableB and TableC. TableB and TableC has Foreign key to TableA. I created an interface for TableA, TableB and TableC with SaveData() and ListData(). I have MVC form which inserts the data into these tables. When implementing these interface methods do I have to create a seperate class for each interface? Please let me if I am doing right.

[code]....

View 2 Replies

Architecture :: MVC Linq To Sql Repository And Search

Apr 15, 2010

I need some guidance for an application I am working on. I have searched the web and the forum and found parts that answer my questions but I want to do this correctly. My solution uses LINQ to SQL to model the db and I have a repository that is querying my db everytime a controller needs to get some data for the view. I am initially anticipating a maximum of only about a few hundreds hits per day (if that). The website is a directory, with listings and requires a search functionality.

1. What options should I consider to avoid having my repository query the db everytime

2. The site contains a 'search' option, how can I implement this against my repository?

I guess I'm really looking for someone to point me in the right direction. So far I've looked at 'caching' the LINQ to SQL model and Lucene.NET but both seem overkill or am I missing the point?

View 3 Replies

MVC :: Custom Validation Against Repository With Dataannotations

Jan 11, 2010

I'm having problems with getting my custom dataannotations to work, I'm trying to add a validation-attribute that validates that the UsergroupName for a Customer (CustomerID) is unique.

[Code]....

the IsValid should return false if the "count >0". How do I fix this one so it works. GetUsergroups() returns IQueryable<Usergroup>.

View 8 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved