1

I'm developing an Application with ASP.NET MVC 5, EF (5 or 6), Oracle 11g and Kendo UI.

Now, I want to layer (architecture) the application. So, Please give me guidance on how to layer the project and different ways of layering the projects.

I've read about Generic Repository Pattern and I have following questions,

  1. Is it good to use in my situation?
  2. Is it good to use EF Class in the View ? or Is it good to use Model class (Not the EF Class) and Automapper which converts EF Class to Model Class?
  3. Is it good to use Log4Net for Logging ?

Or Is there any other Pattern we can use it ?

I'm sorry, this might be simple question but I'm new to Layering (Architect) the Project. So, Please guide me how to layer the project ?

Thanks,

Prakash.

ps_prakash02
  • 543
  • 4
  • 18

2 Answers2

2
  1. A repository pattern could be used, but as Entity Framework's DbSet class itself is a repository, I do not see any real advantage to build a repository over a repository.

  2. No, I wouldn't use EF class in Views, because most of the time you do not display data exactly as they are stored in DB, so I prefer a Model class for each View that only exposes properties that are actually needed for the view. I would use Model classes but not Automapper (see for instance https://stackoverflow.com/a/32459232/870604), so basically I would manually map objects. I personally also don't like calling my business layer using entities as parameter, I generally use a POCO that only contains properties specified by the user.

  3. Log4net works fine.


So for example to create a new User I would use:

  • A business method UserEntity CreateUser(UserToCreate objectToCreate), UserEntity being your entity and UserToCreate a POCO that only exposes properties that are required for a new user to be created. This method would be located inside a Business assembly, and called from the controller
  • A Model object call UserCreateModel that is used by the UserCreate View and the Controller
  • Manual mapping between UserCreateModel and UserToCreate
  • Manual mapping between UserToCreate and UserEntity
  • UserEntity would have additional properties not used/related by/with user creation (and you do not want to expose those properties by the CreateUser method)
  • UserCreateModel could have additional properties purely related to how the View is designed (for example: you would have two Password properties so the user has to confirm the password within the Create view, but only one Password property in UserToCreate is required for user creation)
Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • Thanks for your information. I can have the Project like DAL, BAL and UI. DAL => Entity Framework BAL => Consumes the EF and returns data to Controller UI => MVC Is this fine or any change required ? – ps_prakash02 Sep 10 '15 at 14:28
  • @ps_prakash02 Seems like a good way to start. You could then also introduce some IoC (Inversion Of Control) such as Dependency Injection (constructor injection) to make your code more testable. See for instance http://www.asp.net/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-dependency-injection – ken2k Sep 10 '15 at 15:00
0

@ken2k pretty much rounded the bases, but I just wanted to point out that while it is inadvisable to utilize the repository pattern with EF, it's not a bad idea to still abstract away your use of Entity Framework. What I personally use is the strategy pattern, where I create an interface that essentially holds an API that my application can use, and then one or more implementations of that interface, depending on what kind of sources I'm dealing with (Entity Framework, Web Api, etc.). In your application, you only ever refer to the interface and then use dependency injection to substitute the appropriate implementation. This allows you to swap out "strategies" at a later point. For example, if you later find yourself wanting to switch from EF to something like Dapper, you need only create a new implementation for Dapper and then swap it in with your DI container. The rest of the application hums along none the wiser.

Also, I find it beneficial to have my entities implement various interfaces, which then allows me to create generic methods in my strategy implementations that can work with similar types of objects. For example, I might have something like:

public interface IPublishable
{
    PublishStatus Status { get; set; }
    DateTime? PublishDate { get; set; }
    DateTime? ExpireDate { get; set; }
}

Then, I can implement a method like:

public IEnumerable<TEntity> GetPublished<TEntity>()
   where TEntity : IPublishable
{
    ...
}

Inside that method, I can freely query my entities using Status, PublishDate, and ExpireDate, since the interface guarantees that any class eligible to use this method has those properties.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444