WCF, Linq To Sql and SQL Image Type

February 4, 2009 Leave a comment
Today I’m working on an endpoint for a Northwind demo.  The endpoint is UpdateEmployee and the problem I’m running into is that the Employee Table in Northwind has a field called Photo defined as an Image.
 
Since the constructor for System.Data.Linq.Binary can take a Byte[], I created my data contract as follows.
 

[DataContract(Name = "Employee")]

public class Employee

{

private

int? _employeeId;;

private

byte[] _photo;

[

DataMember(IsRequired = false, Name = "Photo", Order = 70)]

[

DataMember(IsRequired = false, Name = "EmployeeId", Order = 0)]

public int? EmployeeId

{

get { return _employeeId; }

set { _employeeId = value; }

}

public byte[] Photo

{

get { return _photo; }

set { _photo = value; }

}

Then in my CRUD function

if (employee.Photo != null && employee.Photo.Length > 0)

{

dalEmployee.Photo =

new System.Data.Linq.Binary(employee.Photo);

}

There are some other things I’m sure I’m going to have to account for but this should be a good starting point for anybody having trouble.

Categories: Software Development

Linq to Sql and Self-Referencing tables

January 28, 2009 Leave a comment
Today I ran into an issue of Eager loading a self referencing Sql table.  I searched the internet to see what others are doing and everything I found was intricate Linq statements that were generating large sql statements.  My goal was to try a different route based on two database queries.
 
Here’s the code I came up with.  This is an excerpt for a sample I’m working on.  In this example I’m also allowing my end point to get all employees or filter by employeeIds.  Obviously this is going against the Northwind database.
 
One other bit of information is this endpoint is in the BLL of a WCF service.  I have my own Employee Datacontract and you will see calls so the Employee Assembler

EmployeeAssembler<Employee>.AssembleContract.  You can add your own or do something else here.

public IEnumerable<Employee> GetEmployees(IEnumerable<int> employeeIds)

{

List<Employee> employees = new List<Employee>();

using (DAL.NWDBDataContext db = new DAL.NWDBDataContext(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString))

{

IQueryable<DAL.Employee> dalEmployees;

if (employeeIds != null && employeeIds.Count() > 0)

{

dalEmployees =

from e in db.Employees.Where(e => employeeIds.Contains(e.EmployeeID))

select e;

}

else

{

dalEmployees =

from e in db.Employees

select e;

}

// L2S doesn’t support eager loading with LoadOptions so the ReportsTo needs to be extracted.

// Here we got the employees from above, for each one looping through and getting the managers

// as well.

Collection<int> reportToIds = new Collection<int>();

List<DAL.Employee> listEmployees = dalEmployees.ToList();

foreach(DAL.Employee emp in listEmployees)

{

if (emp.ReportsTo.HasValue)

{

if(reportToIds.IndexOf(emp.ReportsTo.Value) < 0)

{

reportToIds.Add(emp.ReportsTo.Value);

}

}

}

IQueryable<DAL.Employee> managers = default(IQueryable<DAL.Employee>);

List<DAL.Employee> listManagers = new List<DAL.Employee>();

if (reportToIds.Count > 0)

{

managers =

from e in db.Employees.Where(e => reportToIds.Contains(e.EmployeeID))

select e;

}

if (managers != null && managers.Count() > 0)

{

listManagers.AddRange(managers.ToList());

}

foreach (DAL.Employee emp in listEmployees)

{

Employee employee = (EmployeeAssembler<Employee>.AssembleContract(emp));

if(emp.ReportsTo.HasValue)

{

DAL.

Employee manager = listManagers.Find(lm => lm.EmployeeID == emp.ReportsTo.Value);

if(manager != null)

{

employee.Manager =

EmployeeAssembler<Employee>.AssembleContract(manager);

}

}

employees.Add(employee);

}

}

return employees;

}

 

 
Categories: Software Development

What do you consider good software development practices?

January 7, 2009 Leave a comment
It seems developers best practices they follow are all over the place.  Some good, some bad but most fall somewhere in the middle.  I think language and learning styles; a lot of the time has different developers arguing about the best way to do things but really they are talking about the same things.
 
Also we seem to be creatures of habit that don’t always take well to change or a different way of doing things.  I find this leads to a lot of animosity and people making snide remarks about others people work when in fact it’s just a case they don’t understand and it appears that is the popular mechanism to comfort ones self.
 
Past that, what I find as the more experience I gain and the more I rely on patterns, the principles I try to adhere to are "Loosely Coupled" and "High Cohesion".  (I have to throw a bone the way of Dino Espisto and his latest Architectul book listed in my book list for completely clearing this up for me.)
 
When you come up with your base principles everything is golden, right?  There is nothing that can stand in your way.  Wrong.  There is no such thing as a black and white flow chart that you can create to write "good code".  However, I feel that if you develop a couple of good sound principles and then start with them on your work and then analyze each situation when you may have to deviate, it gives you the best chance of success while also at the same time allowing you to get product out the door.  When you veer too far to the left, your productivity in the short term increases but at the expense of flexible code and a lot of times you pay the price down the road exponentially as the product grows.  If you veer too far to the right, then you can start suffering from paralysis by analysis and your productivity drops when you try to prepare for everything down the road.
 
Now that we have some ground work laid, lets start a topic that hopefully will spark a passionate discussion for all of us to benefit from.  I find myself most working with layered applications. (N-Tiered).
 
I remember when I started working on my first apps I would get a couple of ideas, and dive right in.  I would probably create my database, then build my endpoints to interface with the database and then I would call those functions directly from my application pages and as long as "things worked" I thought I was golden.  After all, getting things out the door as quickly as possible that worked is the goal right?
 
As you mature as a developer, or at least in my experience, just getting something out the door is just the beginning of the journey.  Things change, improvements are made, additional features are requested etc.  Please, a raise of hands out there who have been asked to work on an app, whether they designed it or inherited and add features or fixed problems.  The more you got into it and accessed what needed to change, the more you realized it would take less time to rewrite the entire app from scratch???  Come on don’t be shy.
 
The more I run into these situations the more determined it makes me to spend more time planning and trying to come up with "flexible" code with not only the goal of getting a great original product out the door.  But also a product that provides the most flexibility within the time given to finish the product.
 
So now the LOB (Line of Business) apps I create, whether I use WCF for the middle tier or not I try to follow a couple of patterns.  I usually try to do this no matter the size of the app because as the saying goes, most large apps started out as small ones.  (Or something like that.)  The only times I try to deviate is if I’m writing a throw away app.  Say something for a demo or something.
 
  • Of course there will be the UI layer. 
  •  If I need dynamic configurability, I usually add a configuration assembly utilizing ConfigurationSections. 
  • Next I always create a Business Layer.  Lately I label my classes as Providers.  I follow the provider module and implement a factory pattern to where I define an interface with all the functionality a provider should have.  Next I create a ProviderFactory and using generics and reflections, any code that needs to use the BLL goes through the ProviderFactory.CreateProvider method.  I define the provider that can be used in the appSettings of the config file.  I feel this gives me a way to change my providers to use any type of data interfacing technology I choose to use or change to down the road with minimal effort and in most cases no major code changes in the production environment.
  • Next there is the Data Access Layer.  My preference right now is to use ORM’s as they give me the functionality and performance I need and cut down on development time exponentially.  Plus I’m a fan of having strongly typed data objects for interfacing into the database.  I’m also a fan of separating development objects from database objects.  Or at least having the flexibility of doing so.  Presently I use LLBL Gen Pro.  I’m a fan of Linq to SQL but there are limitations I find with the product right now so I don’t use it in large applications right now.  But I do hope Linq or something like it does eventually take off.  I’m a proponent that developers exist to provide solutions to problems and our job is not just about sitting around trying to figure out every technology there is out there.  As much as I would love that to be my job it’s not the reality and I applaud technologies that give us great options to be productive and take more of our focus off of technology so we can focus more on the businesses needs write for.
  • Data Contracts – I’m not a fan of accessing my database objects directly.  There’s obvious reasons like you need to change the table names etc.  Other reasons though now I use ORM’s and LLBL Gen, I still want the flexibility change to any technology I need to with minimal effort.  Creating data contracts with translators to translate the DAL objects to your data contracts and use those in the layers above the BLL is my preference.  With the use of macros to generate code, it’s not that much more work.  The ability to change an ORM also ties into the ProviderFactory as I can create new providers that utilize different data access technologies and then plug the new assemblies into production code seamlessly.

I’m out of time for now.  I would love to hear thoughts and feedback from others on their best practices.

 
 
Categories: Software Development

List of recommended development books

January 7, 2009 Leave a comment
When I find the time, I plan to start posting reviews of books I run across that I find insightful.  Please check it out and provide comments.
 
Categories: Books