Implement Repository Pattern in Asp.Net MVC with SOA architecture

We have starting new project in our company. We finalize the architecture as follows

There are 5 different project as follows

1) BusinessEntities(Class Library) which contains DataContract as follows

  [DataContract]
    public class Cities
    {
        /// <summary>
        /// Gets or sets City Id
        /// </summary>
        [DataMember]
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets City name
        /// </summary>
        [DataMember]
        [Display(Name = "CityName", ResourceType = typeof(DisplayMessage))]
        [Required(ErrorMessageResourceName = "CityName", ErrorMessageResourceType = typeof(ErrorMessage))]
        [RegularExpression(@"[a-zA-Z ]*", ErrorMessageResourceName = "CityNameAlphabates", ErrorMessageResourceType = typeof(ErrorMessage))]
        [StringLength(50, ErrorMessageResourceName = "CityNameLength", ErrorMessageResourceType = typeof(ErrorMessage))]
        public string Name { get; set; }

      }

2) Interface which contains

 [ServiceContract]
    public interface ICity : IService<CityViewModel>
    {
         [OperationContract]
        Status Add(Cities entity);
    }

3) DAL which contains implementation of WCF service

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class City : ICity
{
    public Status Add(Cities entity)
    {
     //Insert Logic goes here
    }
}

4) Webcomponent which call the WCF service

public class City
{
    public static Status Add(Cities entity)
    {
        using (var service = new WcfServiceProvider<ICity>())
        {
            return service.GetProxy().Add(entity);
        }
    }
}

5) UI (Asp.Net MVC Project) which call webcomponent to access service

City.Add(entity);

Now we finalize this structure. But the problem is how to use Repository Pattern for Unit Testing? Is it possible to use repository pattern on this structure if yes how? Or is there any other pattern we have to use?

Answers


I recommend that you read about seperation of concerns. Right now you are using your business object as DTO and BO. That effectivly couples your WCF service with your domain layer AND with the UI layer.

That means that version control will be impossible. If you want to do any change in the UI or in the DL you have to make sure that all changes are made in both layers as the UI won't be able to talk with the BL otherwise.

It's much better to have dedicated DTOs since you then can handle versioning issues a lot easier (like default values or a newly introduced property etc).


Your naming does not make sense. Your Cities class contains ONE city, right? Why don't you name it City.


[ServiceContract]
public interface ICity : IService<CityViewModel>
{
    [OperationContract]
    Status Add(Cities entity);
}

Can you explain what the service definition is? I don't see the relation between the view model and the DTO. Same thing goes here. The name ICity is misleading. If it's a repository name it as such. Most of us use the City name to point out the object that we work with and use other names like ICityService or ICityRepository to point out the access technologies.


Now to the real question:

But the problem is how to use Repository Pattern for Unit Testing?

You don't.

The only responsibilty of repositories is to load and store data in the data source. You can of course mock the DbConnection etc. But that doesn't guarantee anything at all since the repositories is effectivly coupled to the data source. If you use mocks you'll still get failures from incorrect SQL queries, invalid column types, incorrect table relations etc.

Hence if you truly want to make sure that the repositories work you have to query a database.


Need Your Help

JavaScript - Trying to use two prompts, but they won't pop up

javascript ajax alert prompt

I know the prompts themselves work, but I'm trying to use one after another to validate a password change, and then run the ajax query to actually change the password and return either a success al...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.