Posts Tagged ‘LINQ’

Beginner LINQ to SQL N-Tier Architecture Part 2

Friday, March 6th, 2009 by

In Part 1 I created a dbml file and I’ll be using this as a DAL in the following examples.

In Part 2 I will be creating the Business Objects.

When creating business objects I often find that your BO’s (DTO’s) would mostly match your db, but BO’s are a logical way of grouping your data and not specific to how it is stored in the db, in most cases tho, it will be the same.

Create a new class, I place these classes under the Business Objects folder just to keep things organised.
Use the prop code snippet within Visual Studio 2008

Product Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AdventureWorks.Business_Objects
{
    public class product
    {
        public int? productId { get; set; }
        public string name { get; set; }
        public string productNumber { get; set; }
        public bool makeFlag { get; set; }
        public bool finishedGoodsFlag { get; set; }
        public string? color { get; set; }
        public int safetyStockLevel { get; set; }
        public int reorderPoint { get; set; }
        public decimal standardCost { get; set; }
        public decimal listPrice { get; set; }
        public string? size { get; set; }
        public char? sizeUnitMeasureCode { get; set; }
        public char? weightUnitMeasureCode { get; set; }
        public decimal? weight { get; set; }
        public int daysToManufacture { get; set; }
        public char? productLine { get; set; }
        public char? _class { get; set; }
        public char? style { get; set; }
        public int? productSubcategory { get; set; }
        public int? productModelId { get; set; }
        public DateTime sellStartDate { get; set; }
        public DateTime? sellEndDate { get; set; }
        public DateTime? discontinuedDate { get; set; }
        public Guid rowGuid { get; set; }
        public DateTime modifiedDate { get; set; }
    }
}

ProductSubcategory Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AdventureWorks.Business_Objects
{
    public class productSubcategory
    {
        public int? productSubcategoryId { get; set; }
        public int? productCategorId { get; set; }
        public string name { get; set; }
        public Guid rowGuid { get; set; }
        public DateTime modifiedDate { get; set; }
    }
}

ProductCategory Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AdventureWorks.Business_Objects
{
    public class productCategory
    {
        public int? productCategoryId { get; set; }
        public string name { get; set; }
        public Guid rowGuid { get; set; }
        public DateTime modifiedDate { get; set; }
    }
}

As you can see I have included all the properties for each corresponding table, this isn’t necessary of course, only include properties that you require in your App.

Once your Business Objects have been created we can then move onto creating the Business Logic Layer that will hold all the data retrieval code.

In Part 3 I’ll be showing you how to create the BLL for Products.

Beginner LINQ to SQL N-Tier Architecture Part 1

Friday, March 6th, 2009 by

I have adopted the following N-Tier architecture using LINQ to SQL for all my ASP.net apps. I’m not saying this is the perfect way to do a N-Tier app but I have found that it works for me.

I’m a firm believer that the Linq to Sql Data Model is a perfect Data Access Layer to build the rest of our architecture off of.

In part 1 I’m going to concentrate on the creation and setup of the DAL.

I’ll be using the Adventure Works DB for all these samples.

Firstly I create a new Linq to Sql class item in a DAL folder

Next in the server explorer create a connection to the DB, in this case it’s the Adventure Works DB.

In these examples I’m going to use the Product, ProductSubcategory and ProductCategory tables, select these tables from the table list in Server Explorer and drag them to the Object Relational Designer. You should see something like this:

We are done with our DAL…As simple as that.

In Part 2 of this series I’ll be concentrating on creating the Business Objects (DTO’s).

Search
Categories