Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 4th, 2014, 11:54 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Default 3 layer setup

Hello all,
can anybody please tell me that how do I setup 3 layer project (Business, Database and UI)

I often prefer this architecture, but sometimes I stuck with data retrieval or saving. In that case I have no option other than creating object of Database layer in UI layer, which I do not want to do so.
I want to call only Business layer object in UI layer, and keep Database layer object only within Business layer.

So please tell me how do I setup such architecture.
I prefer EF model for Database layer.
Will I need to create all the classes created by EF in Database layer into Business layer also or it can be automated?
Please tell me, I am totally confused at this point.

Thanks for any help
 
Old April 9th, 2014, 02:47 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Default

any idea please..
 
Old April 10th, 2014, 03:31 AM
Registered User
 
Join Date: Sep 2013
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Default 3 Layer Setup

Hi Sophia,

You can set 3 Layer Setup by following these simple steps:

1. Goto File => New Project => Web => select ASP.NET or MVC as per your requirement and name it as MyProject.UI

2. Once this is done, Goto File => Add => New Project => Select Visual C# or Windows => Now select Class Library. Name it as per your project. e.g., MyProject.Business and then click OK.

3. Now again follow step 2. This time change the name as MyProject.Data and click OK.

4. You are now done with the 3 Layer Setup. Next you need to add references. Goto the Solution Explorer, select MyProject.UI => Right Click on References => Click Add References => Click on Projects Tab => select MyProject.Business => Click OK. This way a reference to Business Layer is being added to UI Layer.

5. Now follow step 4 for adding reference to Data Layer into the Business Layer.

6. If you are using EF, then your classes are automatically available to your Data or Business provided you created model context properly.

Hope this is useful for you and solve your query.

Thanks and Regards.
Happy Coding.
 
Old April 14th, 2014, 07:43 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

While mvc is a 3 tier application it is not the only project type you can use. It can very well be done using ASP.net web application. A 3 tier application is a architecture design paradigm. Meaning you structure your application in such a manner that you have 3 layers. Typically these layers are a front end(UI), middleware(business logic) and a data layer ( data persistence, typically a data base but does not have to be).
 
Old April 18th, 2014, 11:56 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Default

Hello,

Thanks for replying, but I think answers are not to the question.
I wanted to ask that how do I create classes so that I do not have to reference of Data Access layer in UI as I asked in my question.

Quote:
I often prefer this architecture, but sometimes I stuck with data retrieval or saving. In that case I have no option other than creating object of Database layer in UI layer, which I do not want to do so.
I want to call only Business layer object in UI layer, and keep Database layer object only within Business layer.

So please tell me how do I setup such architecture.
I prefer EF model for Database layer.
Will I need to create all the classes created by EF in Database layer into Business layer also or it can be automated?
Please tell me how to do this.

Thanks
 
Old April 19th, 2014, 12:16 AM
JudithLehman
Guest
 
Posts: n/a
Default Cleanse Pure Premium- Make Your Stomach Toxin Free!

Cleanse Pure Premium is a colon cleanser formula that works to clean your stomach from toxins and undigested food debris to make you stop falling ill with several infectious diseases and protects you from stomach discomforts.
click for more info:- http://pages.rediff.com/clean-your-colon-from-parasites/2006347
Received Infraction
 
Old April 19th, 2014, 10:43 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You may want to take a look at my article series on N-Layers: http://imar.spaanjaars.com/587/new-a...e-for-purchase

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old April 19th, 2014, 05:26 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

I classify my entity models as business objects and the dbcontext as the data access layer. Then I typically write a set of wrapper classes (business layer classes) that consume the dbcontext and expose the operations that are available to my frontend. i.e. Retrieve, Search, Add, Update and Delete on the model entities.

If you dont like this approach then you could write a set of converters in your business layer that convert between business entities and data access entities. Since code first entities are of the poco flavor I tend not to do this. I find it pointless to convert from one poco class to another that contain the same properties.

Another route would be to define a set of business layer interfaces that your model entities would inherit. This allows you the freedom to hide properties at the model layer that you do not want exposed in the UI layer. Your bussiness layer wrappers would then return the model entities as bussiness layer interfaces. This would be my approach, if i were not gpoing to use the ef model entities as bussiness objects, as it is simpler than creating a whole set of converters, and this is what interfaces are for.

example

EF model generate Poco class
Code:
name space ModelEntity
{
     public partial class Entity1
     {
          public Property1 {get;set;}
          public Property2 { get;set;}  // lets say we dont want property 2 expose at the UI layer
      }
}
Custom Partial class to match Ef model poco class, typically i store these in a File name like such Entity1.Custom.cs
Code:
namespace ModelEntity
{
        public partial class Entity1 : Bussiness.Layer.Interface.Entity1
        {
         // nothing to code here unless you have custom logic 
         // this was mearly to inherit the bussiness layer interface 
        // with out affecting code generation
        }
}
Bussiness layer interface and Data service
Code:
namespace Bussiness.Layer.Interface 
{
     public interface Entity1
     {
            string Prorperty1 {get; set;}  // this will be the only property we expose to UI layer with this interface
     }
}

namespace Bussiness.Layer.DataServices
{
      public Entity1DataSvc
      {
            Bussiness.Layer.Interface.Entity1[] Search( SearchCriteria criteria )
            {
                    // code here for db context a fetch operation
                    ModelEntity.Entity1[] rVal = null;
            
                    // we can directly cast ModelEntity to Bussiness layer entity because of inheritance
                    // we also hide the property2 that we dont want to expose to the UI layer
                    return rVal != null ? (Bussiness.Layer.Interface.Entity1[])rVal : null;              
            }
      }
}

Last edited by mmorgan30; April 27th, 2014 at 10:39 PM..
 
Old April 20th, 2014, 02:17 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Default

thanks Imar & morgan,

I will look at your ways, and then will come back here..

Thanks again





Similar Threads
Thread Thread Starter Forum Replies Last Post
Handle error from database layer to applican layer khatu_jec ASP.NET 2.0 Basics 1 November 9th, 2008 03:51 PM
Menu layer wdwright BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 January 18th, 2007 08:11 PM
Layer Problem socoolbrewster CSS Cascading Style Sheets 4 August 2nd, 2006 12:33 PM
onClick outside a layer markp Javascript How-To 1 July 8th, 2004 08:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.