A
A
Anton Starostin2014-07-10 20:54:38
Programming
Anton Starostin, 2014-07-10 20:54:38

How to properly build an N-Tier/N-Layer architecture for an ASP.NET project?

Hello. For a long time I can not find a kosher way to build N-Tier / N-Layer architecture.
From what I found, you need to create a Solution in the Visual Studio environment, in which create at least 2 projects of the ClassLibrary type (DAL and BLL layers) and one ASP.NET project (Web layer).
Upon closer examination, I learned that one more layer is needed - Domain Transfer Object (DTO).
The result is the following structure:
0) The DTO layer, in which the object classes are located. They will map properties from Entity Framework objects on the DAL layer.
1) DAL layer. Here is all the work with the base. The actual model from EntityFramework, and the repository class that serves as a wrapper over each table (It has a set of properties for operating with tables. One repository class for each of the tables).
2) BLL layer. Here are all the business processes and classes, which I call services. For example, ProductService, which internally initializes a field of the ProductRepository type and calls its methods, and returns the results of the method above, to the Web layer.
3) Web layer. There are controllers, views and more.
Since I want to make the layers loosely connected, I'm trying to use the Dependency Injection mechanism. To do this, you have to create interfaces for the repository classes and service classes (IProductRepository and IProductService). At the moment, I define these interfaces in the DTO layer, in the same place as the container objects.
Such a structure is quite efficient, but I would like to do everything kosher, beautiful and like a professional. Please help with advice, what am I doing wrong and how can I fix / improve it? The main question is how to correctly access the repository of the DAL layer from controllers in the Web layer (is it advisable to use services), and also in which layer is it correct to define interfaces (service interfaces in BLL, and repository interfaces in DAL)?
Or give me a link where I can read it. Thanks a lot for your help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Gagarin, 2014-07-10
@Lixoradka

The topic is quite deep, personally I am now studying it myself. For now, I’ll just give you the link you need:
blog.byndyu.ru/2014/05/blog-post.html
Read, Alexander has a lot of interesting things written on this topic.
If you talk specifically about your message, then in general you describe everything correctly. Those. you can have a DAL layer to access the database and get objects. At the same time, DAL does not contain business logic, it only returns objects. Moreover, it is desirable to return lists of objects as IEnumerable, not IQueryable.
Further, as you rightly said, there is a layer of business logic. Again, as correctly noted, the business logic layer keeps a reference to the DAL interface and accesses it to get objects. The specific DAL is specified via DI.
As for accessing the DAL from the controller, I would still recommend using the service. In general, I asked Alexander almost exactly the same question - what if I just need to get a list of objects from the DAL. He gave me a link to this article blog.byndyu.ru/2011/08/repository.html Read it, it's about that.
Regarding which assemblies it is more correct to store interfaces - I, unfortunately, do not know for sure yet, since I have not read enough literature yet.
And in the final, I’ll say that Alexander himself recommends, if possible, using not service layers, but CQRS. About what it is - look in the search engine. I hope the answer was helpful.

V
Vyacheslav Zolotov, 2014-07-11
@SZolotov

I would still advise you to try to understand what is the difference between a multi-tier application architecture and breaking code into layers, and then move on to writing code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question