views:

200

answers:

5

I am using layered architecture in dotnet (mostly I work on web projects). I am confuse what layers should I use ?

I have small idea that there should be the following layers.

  1. user interface
  2. customer types (custom entities)
  3. business logic layer
  4. data access layer

My purpose is sure quality of work and maximum re-usability of code.

some one suggested to add common types layer in it. Please guide me what should be layers ? and in each layer what part should go ?

+2  A: 

Layering a web application is a bit tricky. A lot of operations just pass through the business layer, so you tend to feel that it's pretty useless.

One of the main purposes of layering is to keep the user interface isolated from the data storage. In theory you should be able to change the data storage solution without having to make any changes to the user interface. In my experience this very rarely happens, but just having the abstraction gives you other advantages, like keeping details from the data storage from showing up in the user interface design.

Typically three layers are used:

  1. user interface
  2. business logic
  3. data access

Data classes / entities are not a layer of their own, but part of the interface between layers. Typically they are exposed by the business layer to be used by the user interface.

Guffa
ok, Guffa do you mean total 4 layers ? Is my layering fine ? or can be improved.Where should place business logic, validations ?should there be cuomman types as layer ?
haansi
Sounds like "MVC" to me.
Mark
@haansi: No, just three layers. Having custom or common types as a layer would in practive mean that you stack two business layers or top of each other. It's common to have some validation in the user interface, but the final validation goes in the business layer.
Guffa
Guffa plz explain do you mean two business layers (custom types and common types) should be or not ?
haansi
@haansi: If you define the custom entities as a layer, that means that the user interface only communicates with the custom entities, and never with the business layer directly.
Guffa
@downvoter: Why the downvote? If you don't say what it is that you don't like, it can't improve the answer.
Guffa
+1  A: 

It all depends on the requirements, particularly non-functional requirements.

The answer for a single user interactive application is likely to be very different to a web application needing to scale to support thousands of users.

In general KISS, but avoid hardcoding dependencies throughout the code. Designing for (unit) testability is a good starting point.

If you have no good immediate answer, maybe the answer is not very important (i.e. don't over engineer, and YAGTNI).

Richard
can you plz explain over engineer an YAGTNI ?
haansi
@haansi: YAGTNI = "you ain't going to need it": i.e. don't design for things you think you might need. You will probably guess wrong and have wasted time.
Richard
+1  A: 

I'd suggest you to look at DDD (Evans) first of all and Fowler's app patterns. This will show you the big picture. Number of layers that you have in your project can vary: it can can be 3 or maybe 5. It depends on complexity of your project, your experience and so on. So there is no clear answer how many layers should be. The main purpose of layers is separation of responsibilities: presentation layer is responsible for UI and UI logic, domain model layer is responsible for your business logic, data access is responsible for CRUD operations on your objects and so forth.

Voice
thanks Voice,Please guide as well where should be validations and functions like hashpassword, getIP and send email etc ?Where and how I can look DDD Evans and Fowler's patterns ?
haansi
Well, I can say that you'd better keep your validation logic in one place - Domain Model (of course in case if you have it in your project). Such frameworks as ASP.NET MVC support this kind of architecture. DDD- is Domain Driven Design and the best book on it is written by Eric Evans. It's a bit theoretic but you can also read "Applying Domain-Driven Design and Patterns: With Examples in C# and .NET " which has more exaples in C# but less theory. http://domaindrivendesign.org/books
Voice
And here is Fowler's best seller on Enterprise patterns http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420/ref=pd_sim_b_1
Voice
+1  A: 

It depends on what data access technology you are using.

If you are using NHibernate, I would strongly recommend Repository-pattern along with some dependency Injection.

If you are using Linq-to-sql, you should be using Active Data Record-pattern. In this case you may or may not use dependency injection.

In case of Entity Framework, Unit-of-work-pattern can be used.

Generally I arrange my VS2005/2008 - solution like this:

alt text

And, I arrange my codes like this:

namespace MySolution.Entity
{
    public interface IMyInterface
    {        
        int Save(MyClass obj);
    }
}

namespace MySolution.Entity
{
    public class MyClass
    {
        IMyInterface _myDa;

        public MyClass(IMyInterface myDa)
        {
            _myDa = myDa;
        }

        private string _message;
        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }

        public int Save()
        {
            return _myDa.Save(this);
        }
    }
}

using MySolution.Entity;
namespace MySolution.Service
{
    public class MyClassService : IMyInterface
    {
        public int Save(MyClass obj)
        {
            Console.WriteLine(obj.Message);

            return 1;
        }
    }
}

using MySolution.Entity;
using MySolution.Service;
namespace MySolution.UI
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass myobj = new MyClass(new MyClassService());
            myobj.Message = "Goodbye Circular Dependency!";
            myobj.Save();

            Console.ReadLine();
        }
    }
}

You can put IMyInterface.cs in a separate project named MySolution.Contracs. And, then add a reference of it to the respective assembly.

Please note that, this is called layered-design, not tiered-design.

You can also employ a simple framework for your business-entities like the one used in this example.

And finally employ MVC pattern in your Winforms UI layer. You can get the example here.

And I am not providing any link for ASP.NET MVC, coz there are numerous in the net.

JMSA
Thanks JMSA,I am using DAAB with stored procedures for database communication. Plz guide on it.
haansi
No matter which technology you use, you can use the same arrangement in the solution. Just plug the persistance code in the MySolution.Service - layer and you are on! See this example and plug the code: http://www.codersource.net/asp-net/application-blocks/using-microsoft-data-access-application-block.aspx
JMSA
A: 

Thanks everyone.

As for as I came to decide is there are different architecture available. Selecting an architect is a decision depending on your team, company and planning. Similarly all architectures has some good and bad points.

thanks

haansi