Jstor.Domain.dc.ComputeIdentity C# (CSharp) Method

ComputeIdentity() public method

public ComputeIdentity ( ) : object
return object
        public override object ComputeIdentity()
        {
            if (Id != null) return Id;

            Func<string, string> RemoveWhitespace = (@string) =>
                @string.Split(',', ' ', '.').AsEnumerable().Fold("",
                    (idSubstr, state) =>
                    {
                        state += idSubstr;
                        return state;
                    });

            Func<string, string> ChopWhitespace = (@string) =>
                @string.Substring(0, @string.IndexOfAny(new[] { ' ', ',' }));

            var prepTitle = RemoveWhitespace(title ?? "");

            var prepCreator =
                creator.AsEnumerable().Fold("",
                    (creatorItem, stringSoFar) =>
                    {
                        stringSoFar += ChopWhitespace(creatorItem.Value ?? "");
                        return stringSoFar;
                    });

            var prepCoverage = RemoveWhitespace(coverage ?? "");

            var wholeId = prepTitle + prepCreator + prepCoverage;
            if (wholeId.Length > 255) wholeId = wholeId.Substring(0, 255);
            return wholeId;
        }

Usage Example

Example #1
0
        public void Save(dc item)
        {
            lock (SyncRoot)
            {
                var id = item.ComputeIdentity() as string;
                var result = (from record in _session.Linq<dc>()
                              where record.Id == id
                              select record).FirstOrDefault();

                if (result == null)
                    _session.TransactedSave(item);
                else
                {
                    result.MergeWith(item);
                    _session.TransactedUpdate(result);
                }
            }
        }