开发者

Entity Framework on different project - saving?

I have an established SQL Server database setup. I then generated an Entity Framework model in a Console Application to test some selecting, editing and adding to the database. All went well.

I am now moving more towards my final physical design of my WinForms and WebApp. So, I have decided to do the project in separate projects. I moved the Entity Framework to a Data project, I created a Services project, and I still have my console app as a test application (All in the same solution).

I have a ClassLib with data transfer objects to pass between my layers. So my GUI layer and Service layer don't know of the Entity Framework. I have helper methods in my EF project that convert the EF data into List etc...

Eg of a helper method:

using ClassLib;

namespace Data
{
    public class PayeeDAL : EntityBase
    {

    public static List<PayeeDto> GetPayees()
    {
        var payees = (from payee in Db.payees
                      select payee).ToList();

        List<PayeeDto> reply = new List<PayeeDto>();

        foreach (var pa in payees)
        {
            PayeeDto p = new PayeeDto
                             {
                                 PayeeId = pa.payee_id,
                                 Name = pa.name,
                                 Deleted = pa.deleted == null
                             };
            reply.Add(p);
        }
        return reply;

    }
}
}

And my data transfer object looks like this:

namespace ClassLib
{
    public class PayeeDto
    {
        public int PayeeId { get; set; }
        public string Name { get; set; }
        public bool Deleted { get; set; }
    }
}

So, my selection is working well with this design... but... have no idea how to handle saving.

In my console application, when the EF was available to me, I did this:

                        db.AddToaccount_transaction(ac);

                    account_transaction_line atl = new account_transaction_line
                    {
                        amount = amount,
                        cost_centre =
                            db.cost_centre.FirstOrDefault(
                                cc => cc.cost_centre_id == costcentreid),
                        sub_category =
                            db.sub_category.First(
                       开发者_开发知识库         sc => sc.sub_category_id == subcategoryId),
                        account_transaction = ac,
                        budget = db.budgets.FirstOrDefault(b => b.budget_id == budgetid)

                    };

                    db.AddToaccount_transaction_line(atl);


                }



                db.SaveChanges();

But now I don't have access to .AddTo.... and .SaveChanges... In my Console app, I'd create a parent object, and then add a few child objects... and then add the child objects to the parent object, and save.

But how would this be done in my new structure? I'm thinking I'd have a Save method in each of my helper classes... And then pass a List<> of the child objects, along with a single Parent class to the save method... and then transfor the Dtos to EF models, and then save it that way?

Is that an acceptable plan?


I only use DTO objects to transfer data from A to B. The updating, adding, removing etc., I always encapsulate in Commands (Command Pattern). Retrieving data I do similarily with "Helper" classes.

Example of command pattern:

The base classes:

namespace Busker.Data.Commands
{
    /// <summary>
    /// The 'Command' abstract class
    /// </summary>
    public abstract class Command
    {

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


        private bool success = false;
        public bool Success
        {
            get { return success; }
            set { success = value; }
        }

        private Validator validator = new Validator();
        public Validator Validator
        {
            get { return validator; }
            set { validator = value; }
        }

        private CommandStatusCode statusCode = CommandStatusCode.OK;
        public CommandStatusCode StatusCode
        {
            get { return statusCode; }
            set { statusCode = value; }
        }

        public LoggingLevel LoggingLevel = LoggingLevel.Debug;


        //public BuskerContext BuskerContext;


        public bool IsValid()
        {
            if (validator.Errors.Count > 0)
                return false;
            return true;

        }

        public abstract void Execute();

        public void FailedSubCommand(Command cmd) 
        {
            this.Success = cmd.Success;
            this.Message = cmd.message;
        }
    }
}


namespace Busker.Data.Commands
{
    public class Invoker
    {
        private Command command;

        public Command Command
        {
            get { return command; }
            set { command = value; }
        }


        public void SetCommand(Command command)
        {
            this.Command = command;
        }

        public virtual void ExecuteCommand()
        {
            if (command == null)
                throw new Exception("You forgot to set the command!!");

            try
            {
                log(this.command.GetType().Name + " starting execution ");
                command.Execute();
                if (!command.Success)
                {
                    log(this.command.GetType().Name + " completed execution but failed. Message: " + command.Message + " " + command.StatusCode.ToString());
                }
                else
                    log(this.command.GetType().Name + " completed execution. Success!");

            }
            catch (Exception ex)
            {
                command.StatusCode = CommandStatusCode.Error;
                Loggy.AddError("An unhandled error was caught in " + this.command.GetType().Name + ": " + ex.Message, ex);
                command.Message = ex.ToString();
                //throw;
            }
        }

        private void log(string msg)
        {
            switch (command.LoggingLevel)
            {
                case Busker.Data.Constants.LoggingLevel.Debug:
                    Loggy.Debug(msg);
                    break;
                case Busker.Data.Constants.LoggingLevel.Off:
                    break;
                default:
                    Loggy.Add(msg);
                    break;

            }

        }

        public virtual void ExecuteLinqCommand()
        {
            this.ExecuteCommand();
        }
    }
}

namespace Busker.Data.Commands
{
  public static class Extensions
  {
        /// <summary>
        /// Executes the command using the default invoker.
        /// </summary>
        /// <param name="aCommand"></param>
        public static void Invoke(this Command aCommand)
        {
            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
            System.Reflection.MethodBase m = stackTrace.GetFrame(1).GetMethod();
            String strMethodName = m.DeclaringType.Name + "." + m.Name;

            try
            {
                Invoker invoker = new Invoker();
                invoker.SetCommand(aCommand);
                invoker.ExecuteCommand();
            }
            catch (Exception ex)
            {
                Loggy.AddError("An error occured in Extensions.Invoke. + " + strMethodName ,ex);
                throw ex;
            }

        }
}

Implementation Example:

namespace Busker.Data.Commands.Message
{
    public class CreateMessageCommand :Command
    {


        public CreateMessageCommand (int from, int to, string title, string body)
        {
                // set private variable.. 
        }


        public override void Execute()
        {

            // Do your stuff here


            be.SaveChanges();
            this.Success = true;

        }
    }
}

Usage:

CreateMessageCommand  cmd = new CreateMessageCommand (...);
//Don't use the execute method of the command
//the invoker, because implemented as an extension can be exchange in different 
//environments
cmd.Invoke();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜